read the transcript!
how floats are printed
computers lie when they print out floats
0.12, it’s actually (roughly):
is my computer LYING to me??? about NUMBERS?
the string -> float translation
If your program says:
x = 0.12
your interpreter / compiler needs to translate “
0.12” into the float
0.119999999999999995559. Most languages will use the
strtod (“string to double”) function from libc to do that translation.
the float -> string translation
This is where the rounding comes in. Computers round to make the numbers shorter and easier to read.
float -> string translation is actually super complicated
Every floating point number needs a unique string representation.
There are a bunch of academic papers about how to do this well, search “Printing floating point numbers accurately” to read more about it.
some examples of printing floats
you can also print floats in base 16 or base 2
For example, 0.1 as a 32-bit float is:
p-4 is the base 16. version of
The base 2/base 16 representations are not rounded, but they’re rarely used.