ketema has asked for the wisdom of the Perl Monks concerning the following question:
However, lets assume that we decide to only use a max of two bits, the only numbers we can represent are 0,1,2, and 3 (00,01,10,11). SInce our decimal numbers are constrained between 0 and 1, we can simply divide 1 by 4 and approximate with an if block:
so all numbers from 0 to .25 map to 00, from .25 to .5 map to 01, .5 to .75 to 10, and .75 to 1 to 11
1 divided by above number is 2.3283064370807973754314699618685e-10, that is a pretty fine division, however I don't want to write an if statement that long. What is the best way to figure out where a decimal would lie in a given range then map it to the correct binary representation?
2004-11-12 Edited by Arunbear:
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Converting decimals to binary
by ikegami (Patriarch) on Nov 11, 2004 at 15:54 UTC | |
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 | |
Re: Converting decimals to binary
by fglock (Vicar) on Nov 11, 2004 at 15:56 UTC | |
Re: Converting decimals to binary
by JediWizard (Deacon) on Nov 11, 2004 at 15:42 UTC |