Decimal floating point number to binary: Difference between revisions
Decimal floating point number to binary (view source)
Revision as of 11:32, 7 July 2019
, 4 years ago→{{header|Phix}}: simplified output
(julia example) |
m (→{{header|Phix}}: simplified output) |
||
Line 1,114:
procedure test(atom f, integer base=2)
string s = dec_to(f,base)
end procedure
test(23.34375)
Line 1,132 ⟶ 1,131:
{{out}}
<pre>
23.34375 => 0(2):10111.01011 => 23.34375
-23.34375 => 0(2):-10111.01011 => -23.34375
-11.90625 => 0(2):-
0.1 => 0(2):0.0001100110011001100110011001100110011001100110011001101 => 0.1▼
-
-0.25 => 0(2):-
65535 => 0(
▲0.1 => 0(2):0.0001100110011001100110011001100110011001100110011001101
23.7
"23.699999999999999289457264239899814128875732421875"
Line 1,157 ⟶ 1,146:
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 test shows. 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.
|