Solution to JavaScript's 0.1 + 0.2 = 0.30000000000000004 problem?
*(cross posted from a The Hub [discussion][1])* Open your JavaScript console and try this > 0.1 + 0.2 == 0.3 false > 0.1 + 0.2 0.30000000000000004 You may ask yourself [Is floating point math broken?][2]. The answer is that JavaScript uses floating point math based on the [IEEE 754 standard][3], same as the Java's double. If you want the details on what that means, either quickly read [The Floating-Point Guide - What Every Programmer Should Know About Floating-Point Arithmetic][4] or fully digest [What Every Computer Scientist Should Know About Floating-Point Arithmetic][5]. But if I really want 0.1 + 0.2 == 0.3 then what do I do? The [Java BigDecima][6]l class solved this problem, and there similar solutions for JavaScript: [GWT compiled to JavaScript][7] version; a [JavaScript translation][8] of the ICU4J's com.ibm.icu.math.BigDecimal. There are also other JavaScript libraries like [big.js][9]. Here is big.js in action: > Big(0.1).plus(0.2).eq(0.3) true > Big(0.1).plus(0.2).toString() "0.3" How have other teams solved this problem? Is there a de facto JavaScript library to use that has been well tested? [1]:
https://thehub.thomsonreuters.com/thread/110833 [2]:
http://stackoverflow.com/questions/588004/is-floating-point-math-broken [3]:
https://en.wikipedia.org/wiki/IEEE_754#Basic_formats [4]:
http://floating-point-gui.de/ [5]:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html [6]:
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html [7]:
https://github.com/iriscouch/bigdecimal.js [8]:
https://github.com/dtrebbien/BigDecimal.js [9]: http://mikemcl.github.io/big.js/
https://thehub.thomsonreuters.com/thread/110833 [2]:
http://stackoverflow.com/questions/588004/is-floating-point-math-broken [3]:
https://en.wikipedia.org/wiki/IEEE_754#Basic_formats [4]:
http://floating-point-gui.de/ [5]:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html [6]:
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html [7]:
https://github.com/iriscouch/bigdecimal.js [8]:
https://github.com/dtrebbien/BigDecimal.js [9]: http://mikemcl.github.io/big.js/
Tagged:
5
Answers
-
While I agree that BigDecimal has solved the problem in terms of accuracy of the computation, beware if you are writing a block of code which is latency critical... Also, BigDecimal has some instance fields (at least 5 at a quick glance). If you plan on your application having a lot of instances of BigDecimal, the memory footprint will be much larger than if you stuck with a double.0
-
If it is monetary values try something dumb like:
> (10 + 20) / 100
0.3
i.e. convert dollars to cents and use integer math, convert back to dollars for display.0 -
It's an old and pretty well-known problem, one of the Awful Parts according to Crockford. The most commonly-used solution/workaround I've seen is scaling: `1 + 2 === 3`. Depending on your application domain you can choose a scale factor that will result in numbers always being integers when performing arithmetic operations. For example, when using money, use a factor of 100 (cents). And when you actually need to present the number to the user, scale it back.1
-
When come to comparing floating point number in computer, I always think about [`epsilon`](
http://floating-point-gui.de/errors/comparison/) approach i.e. as long as 2 numbers are different insignificantly, they can be considered equal. In short, `Real Number` in mathematics is continuous i.e. **indefinite**, while floating-point number in computer which represented by number of **definite** bits is [discrete](
http://en.wikipedia.org/wiki/Discrete_mathematics) and hence the numbers they can be represented are also **definite**.1 -
Hello, try the following to get the value of your sum function numberOfDecimals(decimalNumber){ return (decimalNumber.split('.')[1] || []).length; } function totalSum(a, b) { var precisionA = numberOfDecimals(a.toString()); var precisionB = numberOfDecimals(b.toString()); var precision = precisionA>precisionB?precisionA:precisionB; var x = Math.pow(10, precision || 2); return (Math.round(a * x) + Math.round(b * x)) / x; }1
-
Crockford is pitching his DEC64 format (a decimal floating point format for the next generation of application programming languages.)
https://github.com/douglascrockford/DEC640
Categories
- All Categories
- 6 AHS
- 37 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛