Decimal floating point number to binary: Difference between revisions
Decimal floating point number to binary (view source)
Revision as of 12:10, 7 July 2019
, 4 years ago→{{header|Phix}}
m (→{{header|Phix}}: added limiter) |
|||
Line 1,131:
test(0)
test(65535,16)
test(23.7,35)
?to_dec("23.7",10)
?dec_to(23.7,10)</lang>
Line 1,150 ⟶ 1,151:
</pre>
Aside: I was quite surprised to get 100% accuracy on these tests, but actually it is more of
a lucky coincidence in the way it is written, as the last
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>
Update: Added a limiter for non-base-2 fractions, as per 1/3 -> 0.333 forever in decimal. Base 2 is guaranteed to
terminate anyway, but for other bases we need some limit - the 15 I opted for is completely arbitrary.
|