forked from erikwiffin/0.30000000000000004
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
492 lines (473 loc) · 11.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>0.30000000000000004 - Floating Point Math</title>
<meta name="description" content="Some examples of sending .1 + .2 to standard output in a variety of languages." />
<meta name="keywords" content="floating point, math, php, mysql, erlang, ruby, python, javascript, java, haskell, ghc, hugs, bc, gforth, dc, racket, scheme, lisp, go, powershell, prolog" />
<link rel="canonical" href="http://0.30000000000000004.com/" />
<link rel="stylesheet" type="text/css"
href="/styles.css" />
</head>
<body>
<div>
<h1>Floating Point Math</h1>
<p>
Your language isn't broken, it's doing floating point math.
Computers can only natively store integers,
so they need some way of representing decimal numbers.
This representation comes with some degree of inaccuracy.
That's why, more often than not, <code>.1 + .2 != .3</code>.
</p>
<h3>Why does this happen?</h3>
<p>
It's actually pretty simple. When you have a base 10 system (like ours),
it can only express fractions that use a prime factor of the base.
The prime factors of 10 are 2 and 5. So 1/2, 1/4, 1/5, 1/8, and 1/10
can all be expressed cleanly because the denominators all use prime factors of 10.
In contrast, 1/3, 1/6, and 1/7 are all repeating decimals because their denominators use a prime factor of 3 or 7.
In binary (or base 2), the only prime factor is 2. So you can only express fractions cleanly which only contain 2 as a prime factor.
In binary, 1/2, 1/4, 1/8 would all be expressed cleanly as decimals.
While, 1/5 or 1/10 would be repeating decimals.
So 0.1 and 0.2 (1/10 and 1/5) while clean decimals in a base 10 system, are repeating decimals in the base 2 system the computer is operating in.
When you do math on these repeating decimals, you end up with leftovers which carry
over when you convert the computer's base 2 (binary) number into a more human readable base 10 number.
</p>
<p>
Below are some examples of sending <code>.1 + .2</code>
to standard output in a variety of languages.
</p>
<p align="center">
read more:
|
<a href="http://en.wikipedia.org/wiki/Floating_point">wikipedia</a>
|
<a href="http://grouper.ieee.org/groups/754/">IEEE 754</a>
|
<a href="http://stackoverflow.com/questions/588004/is-javascripts-math-broken/588014">Stack Overflow</a>
</p>
</div>
<table cellpadding="0" cellspacing="5">
<tr>
<th>Language</th>
<th>Code</th>
<th>Result</th>
</tr>
<!-- C -->
<tr>
<td>C</td>
<td><code><pre>
#include<stdio.h>
int main(int argc, char** argv) {
printf("%.17f\n", .1+.2);
return 0;
}</pre></code></td>
<td>0.30000000000000004</td>
</tr>
<!-- C++ -->
<tr>
<td>C++</td>
<td><code><pre>#include <iomanip>
std::cout << setprecision(17) << 0.1 + 0.2 << std.endl;</pre></code></td>
<td>0.30000000000000004</td>
</tr>
<!-- PHP -->
<tr>
<td>PHP</td>
<td><code>echo .1 + .2;</code></td>
<td>0.3</td>
</tr>
<tr>
<td colspan="3" class="comment">
PHP converts 0.30000000000000004 to a string and shortens it to "0.3". To achieve the desired floating point result, adjust the precision ini setting: ini_set("precision", 17).
</td>
</tr>
<!-- MySQL -->
<tr>
<td>MySQL</td>
<td><code>SELECT .1 + .2;</code></td>
<td>0.3</td>
</tr>
<!-- Delphi XE5 -->
<tr>
<td>Delphi XE5</td>
<td><code>writeln(0.1 + 0.2);</code></td>
<td> 3.00000000000000E-0001</td>
</tr>
<!-- Erlang -->
<tr>
<td>Erlang</td>
<td><code>io:format("~w~n", [0.1 + 0.2]).</code></td>
<td>0.30000000000000004</td>
</tr>
<!-- Elixir -->
<tr>
<td>Elixir</td>
<td><code>IO.puts(0.1 + 0.2)</code></td>
<td>0.30000000000000004</td>
</tr>
<!-- Ruby -->
<tr>
<td>Ruby</td>
<td>
<code>puts 0.1 + 0.2</code><br />
And<br />
<code>puts 1/10r + 2/10r</code>
</td>
<td>0.30000000000000004<br />And<br />3/10</td>
</tr>
<tr>
<td colspan="3" class="comment">
Ruby supports rational numbers in syntax with version 2.1 and newer directly. For older versions use
<a href="http://ruby-doc.org/core/classes/Rational.html">Rational</a>.
<br />
Ruby also has a library specifically for decimals:
<a href="http://ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/index.html">BigDecimal</a>.
</td>
</tr>
<!-- Python 2 -->
<tr>
<td>Python 2</td>
<td>
<code>print(.1 + .2)</code><br />
And<br />
<code>float(decimal.Decimal(".1") + decimal.Decimal(".2"))</code>
And<br />
<code>.1 + .2</code>
</td>
<td>0.3<br />And<br />0.3<br/>And<br />0.30000000000000004</td>
</tr>
<tr>
<td colspan="3" class="comment">
Python 2's "print" statement converts 0.30000000000000004 to a string and shortens it to "0.3". To achieve the desired floating point result, use print(repr(.1 + .2)). This was fixed in Python 3 (see below).
</td>
</tr>
<!-- Python 3 -->
<tr>
<td>Python 3</td>
<td>
<code>print(.1 + .2)</code><br />
And<br />
<code>.1 + .2</code>
</td>
<td>0.30000000000000004<br />And<br />0.30000000000000004</td>
</tr>
<!-- Lua -->
<tr>
<td>Lua</td>
<td><code>print(.1 + .2)<br />
print(string.format("%0.17f", 0.1 + 0.2))</code></td>
<td>0.3<br />0.30000000000000004</td>
</tr>
<!-- JavaScript -->
<tr>
<td>JavaScript</td>
<td><code>document.writeln(.1 + .2);</code></td>
<td>0.30000000000000004</td>
</tr>
<!-- Java -->
<tr>
<td>Java</td>
<td>
<code>System.out.println(.1 + .2);</code><br />
And<br />
<code>System.out.println(.1F + .2F);</code>
</td>
<td>0.30000000000000004<br />And<br />0.3</td>
</tr>
<!-- Julia -->
<tr>
<td>Julia</td>
<td><code>.1 + .2</code></td>
<td>0.30000000000000004</td>
</tr>
<tr>
<td colspan="3" class="comment">
Julia has built-in
<a href="http://docs.julialang.org/en/release-0.4/manual/complex-and-rational-numbers/#rational-numbers">rational numbers support</a>
and also a built-in
<a href="http://docs.julialang.org/en/release-0.4/manual/integers-and-floating-point-numbers/#arbitrary-precision-arithmetic">arbitrary-precision BigFloat</a> data type.
To get the math right,
<code>1//10 + 2//10</code> returns <code>3//10</code>.
</td>
</tr>
<!-- Clojure -->
<tr>
<td>Clojure</td>
<td><code>(+ 0.1 0.2)</code></td>
<td>0.30000000000000004</td>
</tr>
<tr>
<td colspan="3" class="comment">
Clojure supports arbitrary precision and ratios.
<code>(+ 0.1M 0.2M)</code> returns <code>0.3M</code>, while
<code>(+ 1/10 2/10)</code> returns <code>3/10</code>.
</td>
</tr>
<!-- С# -->
<tr>
<td>C#</td>
<td><code>Console.WriteLine("{0:R}", .1 + .2);</code></td>
<td>0.30000000000000004</td>
</tr>
<!-- GHC (Haskell) -->
<tr>
<td>GHC (Haskell)</td>
<td>
<code>0.1 + 0.2</code>
</td>
<td>
0.30000000000000004
</td>
</tr>
<tr>
<td colspan="3" class="comment">
Haskell supports rational numbers. To get the math right,
<code>(1 % 10) + (2 % 10)</code> returns <code>3 % 10</code>.
</td>
</tr>
<!-- Hugs (Haskell) -->
<tr>
<td>Hugs (Haskell)</td>
<td><code>0.1 + 0.2</code></td>
<td>0.3</td>
</tr>
<!-- bc -->
<tr>
<td>bc</td>
<td><code>0.1 + 0.2</code></td>
<td>0.3</td>
</tr>
<!--Nim -->
<tr>
<td>Nim</td>
<td><code>echo(0.1 + 0.2)</code></td>
<td>0.3</td>
</tr>
<!-- Gforth -->
<tr>
<td>Gforth</td>
<td><code>0.1e 0.2e f+ f.</code></td>
<td>0.3</td>
</tr>
<!-- dc -->
<tr>
<td>dc</td>
<td><code>0.1 0.2 + p</code></td>
<td>.3</td>
</tr>
<!-- Racket (PLT Scheme) -->
<tr>
<td>Racket (PLT Scheme)</td>
<td>
<code>(+ .1 .2)</code><br />
And<br />
<code>(+ 1/10 2/10)</code>
</td>
<td>
0.30000000000000004<br />
And<br />
3/10
</td>
</tr>
<!-- Rust -->
<tr>
<td>Rust</td>
<td>
<code><pre>
fn main() {
println!(.1+.2);
}</pre></code>
</td>
<td>
0.30000000000000004
</td>
</tr>
<!-- Emacs Lisp -->
<tr>
<td>Emacs Lisp</td>
<td><code>(+ .1 .2)</code></td>
<td>0.30000000000000004</td>
</tr>
<!-- Turbo Pascal 7.0 -->
<tr>
<td>Turbo Pascal 7.0</td>
<td><code>writeln(0.1 + 0.2);</code></td>
<td> 3.0000000000E-01</td>
</tr>
<!-- Common Lisp -->
<tr>
<td>Common Lisp</td>
<td>
<code>* (+ .1 .2)</code><br />
And<br />
<code>* (+ 1/10 2/10)</code>
</td>
<td>
0.3<br />
And<br />
3/10
</td>
</tr>
<!-- Go -->
<tr>
<td>Go</td>
<td>
<code><pre>
package main
import "fmt"
func main() {
fmt.Println(.1 + .2)
var a float64 = .1
var b float64 = .2
fmt.Println(a + b)
}</pre></code>
</td>
<td>
0.3<br />
0.30000000000000004
</td>
</tr>
<tr>
<td colspan="3" class="comment">
<a href="http://blog.golang.org/constants#TOC_8.">Go numeric constants have arbitrary precision</a>.
</td>
</tr>
<!-- Objective-C -->
<tr>
<td>Objective-C</td>
<td><code>0.1 + 0.2;</code></td>
<td>0.300000012</td>
</tr>
<!-- OCaml -->
<tr>
<td>OCaml</td>
<td>
<code>0.1 +. 0.2;;</code>
<td>float = 0.300000000000000044</td>
</td>
</tr>
<!-- Powershell -->
<tr>
<td>Powershell</td>
<td><code>PS C:\>0.1 + 0.2</code></td>
<td>0.3</td>
</tr>
<!-- Prolog -->
<tr>
<td>Prolog (SWI-Prolog)</td>
<td><code>?- X is 0.1 + 0.2.</code></td>
<td>X = 0.30000000000000004.</td>
</tr>
<!-- Perl 5 -->
<tr>
<td>Perl 5</td>
<td>
<code>perl -E 'say 0.1+0.2'</code><br />
<code>perl -e 'printf q{%.17f}, 0.1+0.2'</code>
</td>
<td>
0.3<br />
0.30000000000000004
</td>
</tr>
<!-- Perl 6 -->
<tr>
<td>Perl 6</td>
<td>
<code>perl6 -e 'say 0.1+0.2'</code><br />
<code>perl6 -e 'say sprintf(q{%.17f}, 0.1+0.2)'</code><br />
<code>perl6 -e 'say 1/10+2/10'</code>
</td>
<td>
0.3<br />
0.30000000000000000<br />
0.3
</td>
</tr>
<tr>
<td colspan="3" class="comment">
<a href="http://doc.perl6.org/type/Rational">Perl 6, unlike Perl 5, uses rationals by default, so .1 is stored something like { numerator => 1, denominator => 10 }.</a>.
</td>
</tr>
<!-- R -->
<tr>
<td>R</td>
<td><code>print(.1+.2)<br />print(.1+.2, digits=18)</code></td>
<td>0.3<br />0.300000000000000044</td>
</tr>
<!-- Scala -->
<tr>
<td>scala</td>
<td>
<code>scala -e 'println(0.1 + 0.2)'</code><br />
And<br />
<code>scala -e 'println(0.1F + 0.2F)'</code>
And<br />
<code>scala -e 'println(BigDecimal("0.1") + BigDecimal("0.2"))'</code>
</td>
<td>
0.30000000000000004<br />
And<br />
0.3<br />
And<br />
0.3
</td>
</tr>
<!-- Smalltalk -->
<tr>
<td>Smalltalk</td>
<td><code>0.1 + 0.2.</code></td>
<td>0.30000000000000004</td>
</tr>
<!-- Swift -->
<tr>
<td>Swift</td>
<td><code>0.1 + 0.2</code></td>
<td>0.3</td>
</tr>
<!-- D -->
<tr>
<td>D</td>
<td><code><pre>
import std.stdio;
void main(string[] args) {
writefln("%.17f", .1+.2);
writefln("%.17f", .1f+.2f);
writefln("%.17f", .1L+.2L);
}
</pre></code></td>
<td>
0.29999999999999999<br>
0.30000001192092896<br>
0.30000000000000000
</td>
</tr>
</table>
<div>
<p>
I am Erik Wiffin.
You can contact me at:
<a href="http://erik.wiffin.com">erik.wiffin.com</a>
or
<a href="mailto:[email protected]">[email protected]</a>.
</p>
<p>
This project is on <a href="https://github.com/erikwiffin/0.30000000000000004">github</a>.
If you think this page could be improved, send me a pull request.
</p>
</div>
<!-- Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-8267311-11']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>