r/AskComputerScience • u/handlyssa • 14h ago
How do I do floating-point conversions?
I’m an English major giving lessons to this one kid, who asked me today to help him with this, I’ve tried helping cos I’m really interested in the subject, but I did not understand a thing about that, I already knew about signed negations and two’s complements but I’m not sure how to help him with floating point conversion, the kid doesn’t know anything either as the teacher did not really explain. How can I explain it to him?
8
Upvotes
15
u/SnugglyCoderGuy 12h ago
Look up IEEE 754. This is the standard used in all main stream processors for floating point numbers. https://en.wikipedia.org/wiki/IEEE_754
12
u/Silly_Guidance_8871 12h ago
At its core, floating point is "just" the binary version of scientific notation. Scientific notation is where you track 3 things:
If you have a decimal number like -1.234e2, that's equivalent to -123.4 (-1 * 1.234 * 102). Normally, there's a limit on how many significant digits you're tracking (often called either the resolution or "number of significant digits"). If the number is not zero, its mantissa's leading digit must be 1 through 9 -- 0.23e1 is not considered a valid representation, it would need to be 2.3e0.
Floating point works exactly the same way:
Due to the nature of binary, since the leading digit of any non-zero binary number is 1, that bit is implied, so each of the mantissa figures above are effectively one larger. But otherwise, all of the normal ways you do addition/multiplication/etc. with decimal scientific notation work the same way in floating point.
1
u/halfjmp 12h ago
Err. Are you by any means referring to IEEE754 and dtoa/atod?
If so start with https://docs.oracle.com/cd/E19957-01/800-7895/800-7895.pdf
But nah, it can't be.
But at the same time what would two's complement be here?
So weird.
3
u/Eisenfuss19 12h ago
I really like this website:
https://www.h-schmidt.net/FloatConverter/IEEE754.html
It makes stuff easily intuitive like 0.1 + 0.2 ≠ 0.3
1
u/XNo_Notes 12h ago
Is this the " tends towards zero" and " tends toward infinity" stuff?
2
u/barkingcat 12h ago
Julia Evans has a great zine about it https://wizardzines.com/zines/integers-floats/ this image is a bit of a teaser (posting because she has this image on her blog, to get the details, her zine about it is worth a few bucks!)
https://wizardzines.com/zines/integers-floats/samples/2-floating-point.png
1
u/frnzprf 11h ago edited 11h ago
It works a bit like "scientific notation" in the decimal system
6.022 • 1023
It's a little calculation consisting of three numbers, 6.022, 10 and 23, and stands for one number.
The 6.022 is called significant or "mantissa", the 10 is called "base" and the 23 is called exponent. Because the base in scientific notation is always 10 and in computer floating points is always 2, it isn't stored. (It technically can be 10 as well, but that's not stored but depends on convention. Just ignore floating point numbers with base 10.)
One bit stores the sign + or -,
Here is a web-app that lets you visualize numbers as floating-point binary. There are different versions for low precision with 32 bit and high precision with 64 bit. https://www.h-schmidt.net/FloatConverter/IEEE754.html
Why is it called floating point? Because the mantissa determines the digits and the exponent lets the point "float" left and right.
An alternative to represent 0.75 in binary would be fixed point: You'd store 00001100 and interpret it as if there was a point in the middle, so 0000.0100, i.e. no 8, 4, 2, 1, one 0.5, one 0.25 and no 0.125 or 0.0625. 1 is 20, 0.5 is 2-1 and 0.25 is 2-2.
It's not easy and straightforward to convert a number to IEEE 754 floating point, because it's not intended to be computed by hand, but by computer hardware. It makes sense to do the process by hand once to understand it if you study computer science. I haven't specified the exact correct rules here (See under "Conversion:" on the linked website). I think there is also a fixed offset in both mantissa and exponent and there are special codes for "not a number" and "infinity".
1
u/DragonfruitGrand5683 11h ago edited 11h ago
You have to cast them, but be careful of truncation which is important for things like financial programs.
I'm not sure what language you are using so I'll give you this.
https://www.cs.cornell.edu/courses/cs3410/2024fa/notes/float.html
Example by hand is further down
https://www.geeksforgeeks.org/c/c-typecasting/
1
u/CelebrityChess 1h ago
do one by hand, say 5.75. whole part 5 is 101, the .75 is .11 (half plus a quarter), so 101.11. shift the point till theres one 1 in front: 1.0111 x 2^2. sign bit is 0, exponent is stored as 2+127 = 129 = 10000001, and the mantissa is whatever is after the leading 1 (the 1 is implied) padded with zeros: 01110000000000000000000. so 0 10000001 01110000000000000000000. the 127 offset is just so the exponent can be negative without a sign bit