SlideShare a Scribd company logo
@littledan
Decimal: Avoid Rounding
Errors on in JavaScript
Daniel Ehrenberg
Igalia
Node.TLV 2020
@littledan@littledan
‫אני‬ ‫מי‬
● Daniel Ehrenberg
● @littledan
● Delegate in TC39
@littledan@littledan
‫שלי‬ ‫הבית‬
Les Roquetes del Garraf, Europe
@littledan@littledan
‫שלי‬ ‫ה‬ ָ‫בר‬ֶ‫ח‬
Embedded WebKit and Chromium development
Mesa and GStreamer drivers
CSS, ARIA, WebAssembly, MathML, JavaScript
standards+implementation in web browsers+Node.js
@littledan@littledan
@littledan@littledan
Who is TC39?
● A committee of Ecma, with…
● JS developers
● JavaScript engines
● Transpilers
● Frameworks, libraries
● Academics
● Node.js collaborators
● Some big websites/app platforms
● (No Israeli companies ☹)
@littledan@littledan
@littledan@littledan
@littledan@littledan
Meetings
● Every two months
● For three days
● Discuss language changes
● Seek consensus on proposal
Stage advancement
@littledan@littledan
TC39 stages
● Stage 1: An idea under discussion
● Stage 2: We want to do this, and we have a first draft
● Stage 3: Basically final draft; ready to go
● Stage 4: 2+ implementations, tests ⇒ standard
@littledan@littledan
Consensus-based decision-making
● TC39 doesn't vote on what the language will be
● TC39 is consensus-seeking
○ We work together to meet everyone's goals
○ "Does anyone object to stage advancement?"
○ Objections must have rationale, appropriate to stage
● Goals: Listen to everyone engaging in process;
Don't choose one stakeholder over another as the "decider";
Avoid standardizing things which aren't ready yet
@littledan
BigInt: Stage 4!
@littledan@littledan
const x = 2 ** 53;
⇒ x === 9007199254740992
const y = x + 1;
⇒ y === 9007199254740992
@littledan
@littledan@littledan
const x = 2n ** 53n;
⇒ x === 9007199254740992n
const y = x + 1n;
⇒ y === 9007199254740993n
@littledan
@littledan@littledan
const x = 2n ** 53n;
⇒ x === 9007199254740992n
const y = x + 1n;
⇒ y === 9007199254740993n
nstands for BigInt
@littledan
@littledan@littledan
67 🚢 68 🚢
Flag 🏗
79 🚢
10.4 🚢
@littledan@littledan
Igalia's role in BigInt
● Wrote BigInt specification and some tests
● Standardized BigInt in TC39
● Implemented BigInt in SpiderMonkey (shipping)
and JSC (in progress)
@littledan
Private fields and
methods:
Stage 3
@littledan@littledan
Why?
● Private methods encapsulate
behavior
● You can access private fields inside
private methods
class Counter extends HTMLElement {
#x = 0;
connectedCallback() {
this.#render();
}
#render() {
this.textContent =
this.#x.toString();
}
}
@littledan@littledan
#
is the new
_
for strong encapsulation
@littledan@littledan
class PrivateCounter {
#x = 0;
}
let p = new PrivateCounter();
console.log(p.#x); // SyntaxError
class PublicCounter {
_x = 0;
}
let c = new PublicCounter();
console.log(c._x); // 0
Stage 3
@littledan
@littledan@littledan
Why not private keyword?
● In languages with types:
obj.x can check whether x is private by looking at the type of obj
● JavaScript is dynamically typed
● ⇒ private vs public distinction needed at access point,
not just definition
● # as part of the name was the cleanest, simplest solution we found
● We thought about many alternatives over 20 years; ask me later about any
further possibilities
@littledan
Stage 2 features
We want to do this, and we have a first draft
@littledan
Decorators: Stage 2
Yehuda Katz
Ron Buckton
@littledan@littledan
// Salesforce abstraction
import { api } from '@salesforce';
class InputAddress extends HTMLElement {
@api address = '';
}
Using decorators to track mutations and rehydrate
a web component when needed.
Syntax abstraction for attrs/props in Web Components
// Polymer abstraction
class XCustom extends PolymerElement {
@property({ type: String, reflect: true
})
address = '';
}
Example of using decorators to improve ergonomics.
@littledan
Stage 1 features
An idea under discussion
@littledan
Decimal!
@littledan@littledan
Problem and solution (?)
// Number (binary 64-bit floating point)
js> 0.1 + 0.2
=> 0.30000000000000004
// BigDecimal or Decimal128 (???)
js> 0.1m + 0.2m
=> 0.3m
@littledan@littledan
What's wrong with Number?
● Number is an IEEE 754 64-bit float
@littledan@littledan
Number: an IEEE 754 64-bit float
From Wikimedia Commons, by Codekaizen, GFDL
@littledan@littledan
What's wrong with Number?
● Number is an IEEE 754 64-bit float
● Base 2 fractions can't represent .1 or .2 exactly!
> .1.toPrecision(70)
<< "0.1000000000000000055511151231257827021181583404541015625000000000000000"
> .2.toPrecision(70)
<< "0.2000000000000000111022302462515654042363166809082031250000000000000000"
● ⅓ in base 10: closest we can get is .3333333...,
but eventually we have to cut it off
@littledan@littledan
Use cases and requirements for decimal
Primarily: Dealing with human-written decimal quantities, like money
● Represent typical quantities precisely
● Arithmetic operations: + - * /
● Rounding w/ configurable precision, mode
● Serialize/deserialize with strings
● Display to users
● Easy to use correctly
@littledan@littledan
Why add this to JavaScript now?
We got by without JS decimal
● Server in another language
● JS fronts a thin client
● Don't trust the client anyway (still true!)
What changed?
● Richer clients doing local calculations
(definitively calculated on server, but should avoid ephemeral inaccuracies)
● Server-side in JS with serverless and Node.js!
@littledan
How JS decimal
would work
@littledan@littledan
Basic usage
● Literal syntax:
123.456m
● Operator overloading:
.1m + .2m === .3m
@littledan@littledan
Serialization: JSON.stringify() and toString()
● toJSON like BigInt: Throws by default :(
○ Sorry, changing JSON would break compatibility
○ JSON.stringify takes second parameter to customize
JSON.stringify(123.456m) // ⇒ TypeError
● toString like Number and BigInt:
○ Excludes the m suffix
(123.456m).toString() // ⇒ "123.456"
@littledan@littledan
Presentation to the user: Intl.NumberFormat
@littledan@littledan
Presentation to the user: Intl.NumberFormat
● Intl.NumberFormat.prototype.format, formatToParts:
overloaded for BigDecimal
(like BigInt)
● BigDecimal.prototype.toLocaleString works too
(123123.456m).toLocaleString("de-DE") // ⇒ "123.123,456"
@littledan@littledan
Rounding: the .round(decimal, options) method
● roundingMode: e.g., "up", "down", "half-up", "half-even" (more?). Required
explicitly
● Precision options (names matching Intl.NumberFormat, only one permitted):
○ maximumFractionDigits
○ maximumSignificantDigits
BigDecimal.round(3.25m, { roundingMode: "half-up",
maximumFractionDigits: 1 })
====> 3.3m
@littledan
The data model. Options:
● Fraction
● BigDecimal
● Decimal128
@littledan@littledan
Rationals, i.e., Fractions
Common Lisp, Scheme, OCaml, Ruby, Perl6 etc.
● 2.53m => 253/100
● ⅓ represented exactly
● No need to round for any arithmetic!
numerator
denominator
BigInt…………………...
BigInt…………………...
@littledan@littledan
In this proposal, we're not pursuing rationals
Core operations for the primary use case are not a good fit
● Rounding to a certain base-10 precision, with a rounding mode
○ "Round up to the nearest cent after adding the tax"
● Conversion to a localized, human-readable string
○ "And display that to the user"
● Fractions may be proposed separately
○ E.g., Python and Ruby have both fractions and decimal
○ Just different use cases, no one subsumes the other
@littledan@littledan
Fixed-size decimal, e.g., Decimal128
Python, C#, IEEE 754-2008, Swift, Decimal.js
● Pick a maximum number of digits
● Floating point, only in base 10
● Rounds if there isn't enough space
e.g. 32.5 ->
Value: sign * mantissa * 10exponent
sign (1 or -1)
exponent base 10
mantissa: decimal fraction
0
2
.325
@littledan@littledan
Arbitrary-size decimal, i.e., BigDecimal
Ruby, Java, Big.js
● Number of digits grows with the number
● Represent (almost) any decimal exactly
● Avoid rounding*
Value: sign * mantissa * 10exponent
sign
exponent
mantissa BigInt…………………...
@littledan@littledan
Troubles with precision and BigDecimal
● What should 1m/3m be?
● How long should the decimal go?
● Option: Cut it off at an arbitrary point (Ruby)
○ .33333333333333m
● Option: Force a rounding options parameter (Java)
○ BigDecimal.div(1m, 3m, { roundingMode: "half-up",
maximumFractionDigits: 2 })
@littledan@littledan
Plan: Why not both?
● There are pros and cons of BigDecimal and Decimal128
● Our plan: Try both and gather feedback
○ Implement two polyfills and write full documentation
○ Encourage people to try it out in their programs
○ Collect feedback through surveys, GitHub issues
● Results ⇒ Stage 2 proposal
@littledan
Participating in TC39
@littledan@littledan
Helping with existing proposals
● Often highly appreciated!
● What you want may already be a proposal
○ You might not be the only person with this problem
How you can help: participating on GitHub, with a proposal at a stage...
● Stage 0/1: Help determine use cases, very high level design
● Stage 2: Help work out all the details, early toy implementations
● Stage 3: Implement, document and test
● Stage 4: It's already done
@littledan@littledan
Participating internationally
● Many TC39 members are in a similar situation to you all:
○ English is a second language
■ You can participate with mostly written communication
○ Live outside the US
■ Many delegates live in Europe
○ Unable to attend most TC39 meetings in person
■ Instead, join by video call
● TC39's code of conduct prohibits discrimination on nationality
● Many TC39 members want to increase international participation
● TC39 participants represent ideas and organizations, not countries
@littledan@littledan
Participating asynchronously on GitHub
● Most tasks don't take place in meetings:
Most important technical work happens on GitHub
● Work on GitHub:
○ Some of the discussion about design
○ Specification text
○ Documentation
○ Tests
○ Implementations
● Non-members can contribute on GitHub! We encourage it.
○ Just sign non-member IPR form
● Meeting notes are published to GitHub
● Only members can take part in meetings and consensus process
@littledan@littledan
Joining TC39
● Join TC39 by joining Ecma
● Joining Ecma requires:
○ Signing IPR forms
■ Typical Ecma RAND policy
■ TC39-specific royalty-free agreement
○ Paying membership fee
○ Technically, a vote (typically a formality)
● TC39 delegates represent member organizations
● Companies considering joining can provisionally attend TC39 as
"prospective members"
@littledan
TC39 changes over time
@littledan@littledan
Older TC39 mode
● Specification: Big MS Word doc
● Communication: Meetings and es-discuss list
● Large, occasional specification; no stages
@littledan@littledan
@littledan@littledan
@littledan@littledan
@littledan@littledan
@littledan@littledan
@littledan@littledan
@littledan@littledan
@littledan@littledan
@littledan@littledan
JS Decimal's future in this context
● Aim to be participatory, rigorous and experience-driven
● Prototyping multiple alternatives: BigDecimal and Decimal128
● Developing proposals to generalize decimal:
Operator overloading, extended numerical literals
● This is still early, and we could use your help
@littledan
!‫תודה‬
Follow up on Decimal:
github.com/tc39/proposal-decimal
Tell us your Decimal use cases:
bit.ly/2PFDT2o
Get involved in TC39:
tc39.es
Stay in touch with me:
@littledan on Twitter (DMs open)
littledan@igalia.com
Learn more about Igalia: igalia.com

More Related Content

PDF
Dconf2015 d2 t3
PDF
Dconf2015 d2 t4
PDF
Blockchain and smart contracts, what they are and why you should really care ...
PDF
Big Decimal: Avoid Rounding Errors on Decimals in JavaScript
PDF
TC39: How we work, what we are working on, and how you can get involved (dotJ...
PDF
Top Tips Every Notes Developer Needs To Know
PDF
How to build TiDB
PDF
Feature engineering pipelines
Dconf2015 d2 t3
Dconf2015 d2 t4
Blockchain and smart contracts, what they are and why you should really care ...
Big Decimal: Avoid Rounding Errors on Decimals in JavaScript
TC39: How we work, what we are working on, and how you can get involved (dotJ...
Top Tips Every Notes Developer Needs To Know
How to build TiDB
Feature engineering pipelines

Similar to BigDecimal: Avoid rounding errors on decimals in JavaScript (Node.TLV 2020) (20)

PPT
Chapter12.ppt
PPT
Object-Oriented Design CS177 Python Programming
PPT
Chapter12 python object oriented concepts.ppt
PDF
Software maintenance PyConUK 2016
ODP
Cloud accounting software uk
PPTX
Break up the Monolith: Testing Microservices
PDF
Deep MIML Network
PDF
Dirty Data? Clean it up! - Rocky Mountain DataCon 2016
PDF
In the DOM, no one will hear you scream
PDF
Transitioning to Native
PDF
Blueprints: Introduction to Python programming
PDF
Sv big datascience_cliffclick_5_2_2013
PPTX
Clojure through the eyes of a Java Nut | [Mixed Nuts] at Pramati Technologies
PDF
Software maintenance PyConPL 2016
PDF
Odessa .NET User Group - 10.11.2011 - Applied Code Generation
PDF
Processing Terabytes of data every day … and sleeping at night (infiniteConf ...
PDF
Go at Skroutz
PDF
Validating big data jobs - Spark AI Summit EU
PDF
Dirty data? Clean it up! - Datapalooza Denver 2016
ODP
Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
Chapter12.ppt
Object-Oriented Design CS177 Python Programming
Chapter12 python object oriented concepts.ppt
Software maintenance PyConUK 2016
Cloud accounting software uk
Break up the Monolith: Testing Microservices
Deep MIML Network
Dirty Data? Clean it up! - Rocky Mountain DataCon 2016
In the DOM, no one will hear you scream
Transitioning to Native
Blueprints: Introduction to Python programming
Sv big datascience_cliffclick_5_2_2013
Clojure through the eyes of a Java Nut | [Mixed Nuts] at Pramati Technologies
Software maintenance PyConPL 2016
Odessa .NET User Group - 10.11.2011 - Applied Code Generation
Processing Terabytes of data every day … and sleeping at night (infiniteConf ...
Go at Skroutz
Validating big data jobs - Spark AI Summit EU
Dirty data? Clean it up! - Datapalooza Denver 2016
Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
Ad

More from Igalia (20)

PDF
Life of a Kernel Bug Fix
PDF
Unlocking the Full Potential of WPE to Build a Successful Embedded Product
PDF
Advancing WebDriver BiDi support in WebKit
PDF
Jumping Over the Garden Wall - WPE WebKit on Android
PDF
Collective Funding, Governance and Prioritiation of Browser Engine Projects
PDF
Don't let your motivation go, save time with kworkflow
PDF
Solving the world’s (localization) problems
PDF
The Whippet Embeddable Garbage Collection Library
PDF
Nobody asks "How is JavaScript?"
PDF
Getting more juice out from your Raspberry Pi GPU
PDF
WebRTC support in WebKitGTK and WPEWebKit with GStreamer: Status update
PDF
Demystifying Temporal: A Deep Dive into JavaScript New Temporal API
PDF
CSS :has() Unlimited Power
PDF
Device-Generated Commands in Vulkan
PDF
Current state of Lavapipe: Mesa's software renderer for Vulkan
PDF
Vulkan Video is Open: Application showcase
PDF
Scheme on WebAssembly: It is happening!
PDF
EBC - A new backend compiler for etnaviv
PDF
RISC-V LLVM State of the Union
PDF
Device-Generated Commands in Vulkan
Life of a Kernel Bug Fix
Unlocking the Full Potential of WPE to Build a Successful Embedded Product
Advancing WebDriver BiDi support in WebKit
Jumping Over the Garden Wall - WPE WebKit on Android
Collective Funding, Governance and Prioritiation of Browser Engine Projects
Don't let your motivation go, save time with kworkflow
Solving the world’s (localization) problems
The Whippet Embeddable Garbage Collection Library
Nobody asks "How is JavaScript?"
Getting more juice out from your Raspberry Pi GPU
WebRTC support in WebKitGTK and WPEWebKit with GStreamer: Status update
Demystifying Temporal: A Deep Dive into JavaScript New Temporal API
CSS :has() Unlimited Power
Device-Generated Commands in Vulkan
Current state of Lavapipe: Mesa's software renderer for Vulkan
Vulkan Video is Open: Application showcase
Scheme on WebAssembly: It is happening!
EBC - A new backend compiler for etnaviv
RISC-V LLVM State of the Union
Device-Generated Commands in Vulkan
Ad

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Cloud computing and distributed systems.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
Encapsulation_ Review paper, used for researhc scholars
Per capita expenditure prediction using model stacking based on satellite ima...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
MIND Revenue Release Quarter 2 2025 Press Release
Mobile App Security Testing_ A Comprehensive Guide.pdf
Cloud computing and distributed systems.
Understanding_Digital_Forensics_Presentation.pptx
Programs and apps: productivity, graphics, security and other tools
Review of recent advances in non-invasive hemoglobin estimation
Diabetes mellitus diagnosis method based random forest with bat algorithm
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
sap open course for s4hana steps from ECC to s4
Digital-Transformation-Roadmap-for-Companies.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced methodologies resolving dimensionality complications for autism neur...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

BigDecimal: Avoid rounding errors on decimals in JavaScript (Node.TLV 2020)