Decimal floating point number to binary: Difference between revisions

m
→‎{{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)
printf(1,"%.8gatom g => 0to_dec(%d):%s\n", {f, base, s})
fprintf(1,"%.8g => to_dec0(%d):%s => %.8g\n", {f,base,s,g})
printf(1,"0(%d):%s => %.8g\n", {base, s, f})
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
-2311.3437590625 => 0(2):-101111011.0101111101 => 11.90625
-11.90625 => 0(2):-101111011.0101111101 => -2311.3437590625
11.9062513 => 0(2):1011.111011101 => 13
0.1 => 0(2):0.0001100110011001100110011001100110011001100110011001101 => 0.1
0(2):1011.11101 => 11.90625
-11.906255 => 0(2):-1011.11101101 => -5
-0.25 => 0(2):-10110.1110101 => -110.9062525
130 => 0(2):11010 => 0
65535 => 0(216):1101FFFF => 1365535
0.1 => 0(2):0.0001100110011001100110011001100110011001100110011001101
0(2):0.0001100110011001100110011001100110011001100110011001101 => 0.1
-5 => 0(2):-101
0(2):-101 => -5
-0.25 => 0(2):-0.01
0(2):-0.01 => -0.25
0 => 0(2):0
0(2):0 => 0
65535 => 0(16):FFFF
0(16):FFFF => 65535
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.
 
7,805

edits