Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Hey dudes I like computer programming

Name: Anonymous 2022-03-16 11:09

Did you know that 0.1 + 0.2 does NOT equal 0.3? It will blow your mind!

Name: Anonymous 2022-03-16 19:30

Yeah it's because fractionary numbers are represented as floating points in computers, using base 2 so 0.2=1/5 has no exact representation since 5 doesn't divide any power of two, making it a "recurring binary", in this case 0.0011001100110011...
It is easy to see this, since (0.0011)_2 = (3/16)_10 and the next term of the sum, 0.00000011 is the previous shifted 4 binary places right, that is, divided by 2^4=16, we have S = 3/16 + 3/(16^2) + 3/(16^3) + ...
This geometric sum can be calculated using
S = a_0/(1-q) where a_0 is the first term and q is the fixed ratio between consecutive terms, giving:
S = (3/16)/(1-(1/16)) = (3/16)/(15/16) = 3/15 = 1/5 as we expected.
So I guess the floating point system would truncate it at some point, giving something like, 0.0011001100110011 = 11001100110011 * 2^(-16), i.e. 13107*2(-16) or 13107/65536, (0.19999694824 in base 10)

It is similar for 0.1, it is 1/10, ergo 1/5 shifted right one binary place.
So in base 2 it is 0.00011001100110011...
If, as was the example before, the floating point system ttuncates at the 16th binary places, then we get
0.0001100110011001 = 1100110011001 * 2^(-16) = 6553 * 2^(-16) or 6553/65536 (0.09999084472 in base 10)

So, adding these numbers we get (13107+6553)/65536 = 19660/65536 = 0.29998779296

Slightly less than 0.3! Mind=Blown

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List