When does the computer know when the decimal starts?
Posted by Separate-Judgment949@reddit | learnprogramming | View on Reddit | 9 comments
Hello, I recently learned about types in the C language like float and double. But I am wondering when and how does the computer know when the decimal starts for those types. Like if we have a number say 250.64 we know the decimal starts between the digit 0 and 6 but how does the computer know?
Ormek_II@reddit
You can also have fixed digital and Store them in an integer. You can store cents instead of dollars.
iOSCaleb@reddit
It depends on what the designer or programmer decided should be done, but the usual way to store 250.64 would be as a floating point number. Most languages and most computers have built-in support for the IEEE floating point standard, so that is be far the easiest way to go. Floating point numbers work a bit like scientific notation. You can look up the details, but imagine your example being stored as “25064e-2”, meaning 25064 * 10^-2 , except in binary and usually with 2 as the base instead of 10.
peterlinddk@reddit
Every floating point number is stored as if there is only a single digit before the decimals, and then a number specifying how many times it should be multiplied by 10. So 250.64 would be stored as 2.5064 x 10\^2.
That makes it a lot easier to handle many different sizes of numbers.
When you write 250.64 in your programming language, the compiler reads it and converts it into a binary representation following that pattern, and then that is used directly by the CPU.
The CPU has different operations for working with integers and floating point numbers - in the olden days (the 1990s and earlier) the CPU couldn't do floating point, but you either had to have a second math co-processor installed, or some software that ran all the time and converted back and forth between the two.
Ormek_II@reddit
I doubt that it should be multiplied by ten. Rather by 2 no? Its binary after all.
aanzeijar@reddit
The computer stores floats in exponential form. 250.64 in our decimal exponential form would be 2.5064 * 10^2. So you could store it by leaving out the control characters and just write [26064;2], with the decimal point in the mantissa being assumed at the first spot.
Computers do the same with binary representation. 250.64 happens to be 1.100001101111010101000111 * 2^111. IEEE floats now store:
Put it all together, and the 32 bit value is: 01000011011110101010001111010111
Also note: C doesn't do much with this. It's all encoded in the hardware of your chip. C just tells the chip "add this float to this float" or "check whether this float is lower than this float" and the chip knows how to work on floats. It's only when you try to display then that you have to touch on the internal representation.
0jdd1@reddit
The term “floating point” indicates there is additional data held with the number—called the “exponent”—to indicate where the point goes. Arithmetic operations use the exponents to align the “mantissas”, and they generate results in the same mantissa& exponent format.
nightonfir3@reddit
Tldr. one int for the value and another for how many binary digits to move the decimal over. A lot like scientific notation.
ScholarNo5983@reddit
Do a google search for: IEEE floating point standards history
These ongoing IEEE standards defined how computer can approximate floating point numbers.
szank@reddit
https://pcv.oss-cn-shanghai.aliyuncs.com/wp-content/upload/2022/12/ieee-std-754-2019.pdf