Why are integers exact and floating numbers sometimes not? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why are integers exact and floating numbers sometimes not?

You read allways that the accuracy of floating numbers gets worse the more digits it has after the point (10^-x) but you read never something like that for integers. Why?

7th Jun 2017, 11:13 PM
cocoacheescakemuffin
cocoacheescakemuffin - avatar
2 Answers
+ 2
Here is an article that explains this: http://floating-point-gui.de/errors/rounding/
7th Jun 2017, 11:46 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
For people it's difficult to understand machines because they work on different bases. While people work nowadays with the base 10 (decimal numbers) machines work with the base 2(binary numbers). INTEGERS To understand how a machine sees integers is not difficult. Example: 123 01111011 Decimal system: 10^0= 1 × 3 10^1= 10 × 2 10^2=100 × 1 Binary system: 2^0= 1 × 1 2^1= 2 × 1 2^2= 4 × 0 2^3= 8 × 1 2^4= 16 × 1 2^5= 32 × 1 2^6= 64 × 1 2^7=-128 × 0 This would be a Byte number where 1Byte (8 Bit) is reserved for the number with the 8th bit as a negative to be able to represent numbers from -128 to 127. FLOATING NUMBERS: If we understood that it's not so difficult to see how floats work. Example: 2.625 10101 Decimal 10^-2=0.001 × 5 10^-1= 0.01 × 2 10^0 = 0.1 × 6 10^1 = 1 × 2 binary 2^-3=0.125 × 1 2^-2= 0.25 × 0 2^-1= 0.5 × 1 2^0= 1 × 0 2^1= 2 × 1 This number is easily represented in both system. But now try to represent 2.630 with this 5 bits and you will see it's not possible. The system will make it to 2.625 or 2.750. Normally floating numbers have much more bits so you see accuracy problems for many digits after the point only.
8th Jun 2017, 12:01 AM
cocoacheescakemuffin
cocoacheescakemuffin - avatar