Decimal floating point number to binary: Difference between revisions
Content added Content deleted
(→{{header|dc}}: bc) |
m (→{{header|Phix}}: bsae 2 -> 2/4/8/16/32) |
||
Line 1,093: | Line 1,093: | ||
if d>0 then |
if d>0 then |
||
res &= '.' |
res &= '.' |
||
-- while d>0 do -- (see update below) |
|||
while d>0 |
while d>0 |
||
and (base |
and (find(base,{2,4,8,16,32}) or length(res)<15) do |
||
d *= base |
d *= base |
||
integer digit = floor(d) |
integer digit = floor(d) |
||
Line 1,166: | Line 1,165: | ||
The truth of the matter is simply that you ''can'' extract a float to a binary text representation exactly, |
The truth of the matter is simply that you ''can'' extract a float to a binary text representation exactly, |
||
in a way that you just cannot do for most other (ie non-power-2) bases.<br> |
in a way that you just cannot do for most other (ie non-power-2) bases.<br> |
||
Update: Added a limiter for non-base-2 fractions, as per 1/3 -> 0.333 forever in decimal. |
Update: Added a limiter for non-base-2 fractions, as per 1/3 -> 0.333 forever in decimal. |
||
terminate anyway, but for other bases we need some limit |
Base 2/4/8/16/32 are guaranteed to terminate anyway, but for other bases we need some limit |
||
- the 15 that I opted for is completely arbitrary. |
|||
=={{header|Python}}== |
=={{header|Python}}== |