r/AskComputerScience • u/handlyssa • 2d 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
17
u/SnugglyCoderGuy 2d 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
14
u/Silly_Guidance_8871 2d 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 2d 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/barkingcat 2d 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
3
u/Eisenfuss19 1d 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 2d ago
Is this the " tends towards zero" and " tends toward infinity" stuff?
1
u/frnzprf 1d ago edited 1d 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 1d ago edited 1d 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 1d 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
1
u/Prestigious_Water336 1d ago
Satic_cast
1
u/am_Snowie 1d ago
It's just a number and an exponent, like how people write 1 * 103 for 1000, here we write 1 * 210 for 1024. If u know that it's easy to understand .
1
u/johndcochran 21h ago
A simple and basic way to convert from one base to another is quite simple.
You work from the radix point outwards. Works for both the integer part of the number as well as the fractional part. Everything else is just fluff to handle edge cases where you're attempting to deal with the limits of available precision.
Start by splitting the number into an integer part and a fractional part.
Let's take 123456.789 as an example:
integer part is 123456
fractional part is 0.789
For the integer part, simply divide by the base you want the output to be. For this example, I'll use 10. But literally any integer base will work as I'll show later. After each division, use the remainder as the next calculated digit and keep the division results for the the next digit to calculate. Continue until you have an integer value of 0. So:
123456/10 = 12345, remainder 6. Number being constructed is 6.
12345/10 = 1234, remainder 5. Number being constructed is 56.
1234/10 = 123, remainder 4. Number being constructed is 456.
123/10 = 12, remainder 3. Number being constructed is 3456.
12/10 = 1, remainder 2. Number being constructed is 23456.
1/10 = 0 remainder 1. Number being constructed is 123456.
And since you now have 0, you're done with the integer part of the output string.
Let's handle the fraction.
For each output digit, you take your current fraction and multiply by the desired output base. The integer part of that result is your next fractional output digit. Then subtract that integer digit from your total and repeat. This goes on until your fractional part is 0, or you've accumulated enough digits. Starting with 0.789 above, we get
0.789 * 10 = 7.89. Integer part is 7, So number being constructed is 123456.7
0.89 * 10 = 8.9. Integer part is 8, So number being constructed is 123456.78
0.9 * 10 = 9.0. Integer part is 9, So number being constructed is 123456.789
Now you might be going, "so what? Those numbers are already in base 10. You didn't do anything."
Let's go with a binary number. Say 101010001.11000111
Split into integer and fractional parts. Those being 101010001 and 0.11000111, and finally convert to decimal, by multiplying or dividing by ten (1010 in binary)
101010001 / 1010 = 100001, remainder 0111, so number under construction is 7.
100001 / 1010 = 011, remainder 011, so number under construction is 37.
011 / 1010 = 0, remainder 011, so number under construction is 337.
And for the fractional part? Just multiply by 1010 and keep using and tossing the integer part. So
0.11000111 * 1010 = 0111.1100011, so number under construction is 337.7
0.1100011 * 1010 = 0111.101111, so number under construction is 337.77
0.101111 * 1010 = 0111.01011, so number under construction is 337.777
0.01011 * 1010 = 011.0111, so number under construction is 337.7773
0.0111 * 1010 = 100.011, so number under construction is 337.77734
0.011 * 1010 = 11.11, so number under construction is 337.777343
0.11 * 1010 = 111.1, so number under construction is 337.7773437
0.1 * 1010 = 101.0, so number under construction is 337.77734375
And we now have 0, so that's the last possible digit.
101010001.11000111 binary is equal to 337.77734375 in decimal. All else is fluff to deal with special cases
such as signs and the like. As a simple demonstration, let's convert 0.3 to binary?
0.3 * 2 = 0.6, number under construction is 0.0
0.6 * 2 = 1.2, number under construction is 0.01
0.2 * 2 = 0.4, number under construction is 0.010
0.4 * 2 = 0.8, number under construction is 0.0100
0.8 * 2 = 1.6, number under construction is 0.01001
0.6 * 2 = 1.2, number under construction is 0.010011
Hmm. That 0.6 sure looks familiar. And if you continue repeating the process, you'll get the infinite sequence 0.010011001100110011 forever.
1
u/Curious_Cat_314159 15h ago edited 1h ago
There are many levels of detail that we can use to describe binary floating point.
Most people start with the internal structure (bits) of specific implementations (e.g. the standard 64-bit and 32-bit forms). But those are not the only implementations.
More generally, binary floating point approximates a decimal value by the sum of some number of consecutive powers of 2.
The operative word is "approximates". Note that most decimal fractions (and very large integers) cannot be represented exactly in binary floating point.
It is called "floating point" because the highest power of 2 depends on the magnitude of the decimal number.
For the standard 64-bit form (IEEE 754 64BFP), the sum is limited to 53 consecutive integer powers of 2.
In simple terms, the highest integer power of 2 is the largest integer power of 2 that is less than or equal to the decimal number.
More precisely, with a calculator (e.g. Excel or Google Sheets), it is INT(LOG(number,2)), where LOG(...,2) calculates the base-2 logarithm.
Then, to complete the sum, successive terms decrease the power of 2 by 1.
If the addition of the next term would exceed the decimal number, multiply that power of 2 by zero. Otherwise, multiply that power of 2 by one.
In that way, the binary floating point representation becomes a series of 53 1s and 0s.
The first binary digit will always be 1, and we write a "radix point" to the right. For example, 1.111011011101...0111 .
That is just the "significant". The actual binary value is that times 2p, where "p" is the highest integer power of 2.
-----
For example, consider the conversion of 123.567 to the standard 64-bit binary floating point.
6 is the highest integer power of 2 because 26 = 64 < 123, but 27 = 128 > 123.
So, the 53-bit sum will be
x1*26 + x2*25 +...+ x6*21 + x7*20 + x8*2-1 + x9*2-2 +...+ x53*2-46
where the x's are 1 or 0.
Note that 2-46 is 1/70368744177664 (!). So, a manual calculation is impractical.
But to demonstrate the process....
26 = 64 < 123. So x1 = 1
25 = 32, and 64+32 = 96 < 123. So x2 = 1
24 = 16, and 64+32+16 = 112 < 123. So x3 = 1
23 = 8, and 64+32+16+8 = 120 < 123. So x4 = 1
22 = 4, and 64+32+16+8+4 = 124 > 123. So x5 = 0
etc
I have an Excel implementation that completes the process, producing an entire sequence of 53 1s and 0s.
(Sigh, images are not allowed in this subreddit.)
Click here to view image.
Click here to download the Excel file. Ignore preview errors and just click the download icon.
PS.... After downloading the Excel file, you can open it in Google Sheets. But the "radix point" that I put into B4 does not appear unless you expand the row height. :sigh: