Skip to content

Commit 4f68351

Browse files
committed
adding more tricky questions
1 parent 32ad09e commit 4f68351

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Question: Why `.1+.2 != .3`
2+
3+
**Answer:** This is not a javascript only limitation, it applies to all floating point calculations. The problem is that 0.1 and 0.2 and 0.3 are not exactly representable as javascript (or C or Java etc) floats. Thus the output you are seeing is due to that inaccuracy.
4+
5+
In particular only certain sums of powers of two are exactly representable. 0.5 = =0.1b = 2^(-1), 0.25=0.01b=(2^-2), 0.75=0.11b = (2^-1 + 2^-2) are all OK. But 1/10 = 0.000110001100011..b can only be expressed as an infinite sum of powers of 2, which the language chops off at some point. Its this chopping that is causing these slight errors.
6+
7+
---
8+
9+
### Why is 0.1 + 0.2 Not Equal to 0.3 in Most Programming Languages?

0 commit comments

Comments
 (0)