in reply to Converting decimals to binary
There is something call fixed point numbers (as opposed to floating point), and it sounds like that's what you need. In this case, I fixed the point (B-scaling) to 0 (unsigned) integer bits (allowing numbers from 0 to +1, not including +1).
foreach ( 0.750 0.500, 0.400, 0.250, 0.125, ) { my $num = $_; my $num_B0 = $num * 4294967296; # 0..+1 not incl +1. #my $num_B1 = $num * 2147483648; # 0 to just over +1. printf("%f = %s B0$/", $num, unpack('B32', pack('N', $num_B0)), ); } __END__ output ====== 0.750000 = 11000000000000000000000000000000 B0 0.500000 = 10000000000000000000000000000000 B0 0.400000 = 01100110011001100110011001100110 B0 0.250000 = 01000000000000000000000000000000 B0 0.125000 = 00100000000000000000000000000000 B0
.5, .25 and .125 are round numbers cause they're 2^(-1), 2^(-2) and 2^(-3) respectively.
The machine for which I write software doesn't support floating point numbers, so we use fixed point arithmetic (which any integer arithmetic machine can do). It can be a pain at times, since we deal with an extreme number of decimal numbers like valve positions, temperature measurements, etc. (It's the control system of a nuclear power plant.)
Let me know if you want to know how to do math with fixed point numbers.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Converting decimals to binary
by ketema (Scribe) on Nov 11, 2004 at 16:37 UTC | |
by ikegami (Patriarch) on Nov 11, 2004 at 18:12 UTC | |
by Roger (Parson) on Nov 12, 2004 at 15:22 UTC | |
by ikegami (Patriarch) on Nov 12, 2004 at 16:07 UTC | |
by Anonymous Monk on Nov 12, 2004 at 23:34 UTC |