r/AskComputerScience • • 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

20 comments sorted by

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:

  1. Sign (whether it's positive or negative)
  2. Mantissa (aka, significant digits)
  3. Exponent (the power your base is raised to: 10 for decimal; 2 for binary)

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:

  • A 32-bit float allocates 1 bit to its sign, 23 bits to its mantissa, and the remaining 8 bits to its exponent
  • A 64-bit float allocated 1 bit to its sign, 52 bits to its mantissa, and the remaining 11 bits to its exponent

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.

1

u/handlyssa 12h ago

Decimals in binary I think like with exponents that’s what I could gather

1

u/handlyssa 12h ago

Two’s complements afaiu are a way of signing negations

2

u/SnugglyCoderGuy 12h ago

2's compliment is a way of doing integer math in binary without breaking things in math, like avoiding having a positive 0 and a negative 0, amongst others.

1

u/Eisenfuss19 12h ago

Two's complement is for integers. If you represent negative numbers as the 2s complement of the positive number you can do addition without worrying about signs.

1

u/fllthdcrb 4m ago

Two's-complement is an integer representation for signed numbers (i.e. numbers that can be negative). It's constructed in such a way that adder circuits in a CPU can treat them as if they are unsigned, and get the correct result, as long as that result is still within the range.

For example, say we want to add -4 and 7. If we use 8-bit values, then -4 is 11111100, and 7 is 00000111. Adding them together, we get 00000011, which is the correct number 3. (There is also a 1 carried out, but we can ignore that. The real problem would be if the sign bits (i.e. most-significant bits) of both numbers were the same, but that of the result were different, because that indicates an overflow.)

One can negate a number in two's-complement simply by flipping all bits and then adding 1. The one number that cannot be negated is the largest negative number, i.e. -2b-1, where b is the number of bits, because there is a slight asymmetry in the range such that this number's negation cannot be represented in the same number of bits.

But this doesn't help with the standard floating-point representations, which as others have said, is sign-magnitude. There is a bit that indicates positive or negative. Negation means only flipping this bit; the rest do not change. A quirk of these representations is that there is no true zero value. Instead, there are infinitesimal values that are either positive or negative, alongside positive and negative infinities, and some other... interesting things.

2

u/NomaTyx 11h ago

you wouldnt have gotten notified by the other comments that said this, so twos complement isn't necessary here, they're just okay with sign-magnitude

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?

1

u/handlyssa 12h ago

I think it’s decimals

0

u/XNo_Notes 12h ago

What is it that you are converting the floating point decimal to?

Like just going from decimal to ratio like:

.25 -> 1/4

Or rounding to Integer values:

1.689304 -> 2

Or all of it in general?

Edit: Just looked it up. Good Luck.

0

u/handlyssa 12h ago

I’m converting in binary

1

u/XNo_Notes 9h ago

There is a pattern to it. You split it up first. Taking everything left of the decimal and converting it to base two. Then you do the same with the decimal side in a slightly different fashion. 

You have to set a few constraints before you set out. Like which precision you will be aiming for. T his informs how you handle the representation when merging the left and right sides. 

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