floating point
TRANSCRIPT
![Page 1: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/1.jpg)
Floating point precision problem
![Page 2: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/2.jpg)
Guess the answer
(0.125 + 0.125) * 10;
![Page 3: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/3.jpg)
Guess the answer
(0.125 + 0.125) * 10;
mkotsur@n-racoon:~$ python -iPython 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2Type "help", "copyright", "credits" or "license" for more information.
>>> (0.125 + 0.125) * 10;2.5
![Page 4: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/4.jpg)
Guess the answer
(0.1 + 0.7) * 10;
![Page 5: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/5.jpg)
Guess the answer
(0.1 + 0.7) * 10;
mkotsur@n-racoon:~$ python -iPython 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2Type "help", "copyright", "credits" or "license" for more information.
>>> (0.1 + 0.7) * 10;7.9999999999999991
>>> int((0.1 + 0.7) * 10);7
![Page 6: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/6.jpg)
Guess the answer
(0.1 + 0.1) * 10
![Page 7: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/7.jpg)
Guess the answer
(0.1 + 0.1) * 10;
mkotsur@n-racoon:~$ python -iPython 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2Type "help", "copyright", "credits" or "license" for more information.
>>> (0.1 + 0.1) * 10;2.0
![Page 8: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/8.jpg)
WTF??!11
Python, PHP, Java… => Same problems...
“The IEEE Standard for Floating-Point Arithmetic (IEEE 754-1985)
set the standard for floating-point computation for 23 years. It became the most widely-used standard for floating-point computation, and is followed by many CPU and FPU implementations. Its binary floating-point formats and arithmetic are preserved in the new IEEE 754-2008 standard which replaced it.”
![Page 9: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/9.jpg)
Single float
0.125=0×200×2−1
0×2−21×2−3
0.12510=0.0012
0.0012=1.02×10−11
![Page 10: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/10.jpg)
Single float
M=1.000exp2=−11Mantissa
Sign: 0 for “+”. 1 for “-”. 0 in our case
Exponent bias: + 127 (01111111) – half of a byte.01111100 in our case
Mantissa (fraction): integer part always 1, 23 bits of fraction00000000000000000000000 (23 zeros) in our case
![Page 11: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/11.jpg)
Single float
What about other numbers?
0.125 = 0 01111100 00000000000000000000000
Sign Exponent Mantissa
![Page 12: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/12.jpg)
Single float
0.7 = 0 01111110 01100110011001100110011
0.1 = 0 01111000 10011001100110011001100
0.125 = 0 01111100 00000000000000000000000
![Page 13: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/13.jpg)
Be careful when:
1. You compare results from different sources;2. You do output floats;3. You convert float to another type;4. You use cycles or other ways to accumulate error;
![Page 14: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/14.jpg)
How do people live with this?
1. Don't use float numbers :-)
2. Use 'near' instead of 'equals'
3.Don't trust computers.
![Page 15: Floating point](https://reader036.vdocument.in/reader036/viewer/2022080213/559e10931a28abad158b45e6/html5/thumbnails/15.jpg)
More here:
http://en.wikipedia.org/wiki/IEEE_754-2008
http://php.net/manual/en/language.types.float.php
http://docs.python.org/tutorial/floatingpoint.html
http://en.wikipedia.org/wiki/Single_precision_floating-point_format
http://www.lahey.com/float.htm