Computers calculate numbers in 0s and 1s. A bit can be either but not in between. So if you enter 3/2 into a calculator, it should return either 1 or 2, right? Wrong! It gives you 1.5, the correct answer. Even on problems with more complexity, the calculator answers with the right number. So my question is, how does all this work? If a computer can only use 1s and 0s, how is it able to interpret a number in between 1 and 0 correctly, and is there a way to build a schematic for a machine that understands decimal?
-
1\$\begingroup\$ Things to look up: fixed point, floating point. \$\endgroup\$– user110971Commented Jul 13, 2020 at 2:04
-
1\$\begingroup\$ A motivating question: If we can only use 0,1,2,3,4,5,6,7,8, and 9, then how do we interpret the notion of "one thousand"? Or "two thirds"? \$\endgroup\$– nanofaradCommented Jul 13, 2020 at 2:06
-
\$\begingroup\$ Ok, so I've done some research and it looks like floating-point numbers have 3 parts: a sign, an exponent and a fraction. I know what the sign does, but not the exponent or fraction. \$\endgroup\$– Nip DipCommented Jul 13, 2020 at 2:09
-
\$\begingroup\$ expanding on what @nanofarad said ... decimal comes from a world where people have 10 fingers ... binary comes from a world where people have 10 fingers (binary 10) \$\endgroup\$– jsotolaCommented Jul 13, 2020 at 12:26
-
\$\begingroup\$ @NipDip To briefly (and handwavily) fill in that gap on floating-point: Floating-point is just like scientific notation. Consider the (human) scientific notation of -2.56*10^2. The sign is negative, the exponent is +2, and the fraction is 2.56. Floating-point numbers use a similar technique, just with binary numbers (e.g. 1.01101001 * 2^-3 is equal to 0.00101101001 (binary)), which is approximately equal to 0.176 (decimal) \$\endgroup\$– nanofaradCommented Jul 13, 2020 at 14:07
1 Answer
Calculators generally work in BCD, whereas in programming languages usually (non-integer) numbers are represented in binary floating point format such as IEEE 754.
In the case of binary floating point, there is a number in 2's complement normalized so the most-significant bit is '1' (and since we know it's one, we can avoid storing it and just assume it is there). The exponent is usually a biased binary number that is always positive.
Doing division in BCD is not all that hard, you can do it with a 4-bit arithmetic logic unit (ALU) and a typical long division algorithm (which involves a number of subtracts until the result turns negative, and then one addition), then shift and repeat.
As far as the decimal or binary point, you can handle that separately as a kind of exponent.
Instead of 3/2, think of 30000000/20000000 = 15000000, then you figure out where to place the decimal point.
To add or subtract you have to right-shift the smaller number to make the exponents the same first. So 3 + 0.01 from 30000000 + 100000000 -> 30000000 + 01000000 = 30100000 and the decimal place is set to get 3.0100000
You could hard-wire logic to do this, but it would involve quite a few MSI level ICs for the registers, the ALU and the control logic, usually we'd want to use a microcontroller, an ASIC (as in a calculator) or an FPGA.