Every number will be save in computer by binary value such as 0, 1. In Single-precision numbers occupy 32 bits.
The floating point number can be presented by: 1 bit for sign, 8 bit for exponent and 23 bit called mantissa (fraction).
Look the example below:
0.15625 = 0.00101 = 1.01*2^(-3)
sign: 0 mean positive number, 1 mean negative number, in this case it is 0.
exponent: 01111100 = 127 - 3 = 124.
Note: the bias = 127 so biased exponent = −3 + the "bias". In single precision, the bias is ,127, so in this example the biased exponent is 124;
At fraction part, we have: 1.01 mean: 0*2^-1 + 1*2^-2
Number 1 (first position of 1.01) do not need to save because when present the floating number in this way the first number always be 1.
For example convert: 0.11 => 1.1*2^(-1), 0.01 => 1*2^(-2).
Another example show always remove the first zero: 0.1 will be presented 1*2^(-1). So the first alwasy be 1.
The present number of 1*2^(-1) will be:
- 0: positive number
- 127-1 = 126 = 01111110
- fraction: 00000000000000000000000 (23 number)
Finally: The raw binary is:
0 01111110 00000000000000000000000
Check it here: http://www.binaryconvert.com/result_float.html?decimal=048046053
Now if you already understand how a floating point number are saved. What happen if the number cannot save in 32 bit (simple precision).
For example: in decimal. 1/3 = 0.3333333333333333333333 and because it is infinite I suppose we have 5 bit to save data. Repeat again this is not real. just suppose. So the data saved in computer will be:
0.33333.
Now when the number loaded the computer calculate again:
0.33333 = 3*10^-1 + 3*10^-2 + 3*10^-3 + 3*10^-4 + 3*10^-5.
About this:
$a = '35';
$b = '-34.99';
echo ($a + $b);
The result is 0.01 ( decimal). Now let show this number in binary.
0.01 (decimal) = 0 10001111 01011100001010001111 (01011100001010001111)*(binary)
Check here: http://www.binaryconvert.com/result_double.html?decimal=048046048049
Because (01011100001010001111) is repeat just like 1/3. So computer cannot save this number in their memory. It must sacrifice. This lead not accuracy in computer.
Advanced
( You must have knowledge about mathematics )
So why we can easily show 0.01 in decimal but not in binary.
Suppose the fraction in binary of 0.01 (decimal) is finite.
So 0.01 = 2^x + 2^y... 2^-z
0.01 * (2^(x+y+...z)) = (2^x + 2^y... 2^z)*(2^(x+y+...z)). This expression is true when (2^(x+y+...z)) = 100*x1. There are not integer n = x+y+...+z exists.
=> So 0.01 (decimal) must be infine in binary.