Decimal floating point number to binary: Difference between revisions
Decimal floating point number to binary (view source)
Revision as of 08:13, 2 May 2024
, 18 days agoadded RPL
(added RPL) |
|||
(6 intermediate revisions by 5 users not shown) | |||
Line 9:
1011.11101 => 11.90625
<br><br>
=={{header|11l}}==
{{trans|Kotlin}}
<syntaxhighlight lang="11l">F decToBin(d)
V whole = Int(floor(d))
V binary = bin(whole)‘.’
V dd = d - whole
L dd > 0.0
V r = dd * 2.0
I r >= 1.0
binary ‘’= ‘1’
dd = r - 1
E
binary ‘’= ‘0’
dd = r
R binary
F binToDec(s)
V num = Int(s.replace(‘.’, ‘’), radix' 2)
V den = Int(‘1’s.split(‘.’)[1].replace(‘1’, ‘0’), radix' 2)
R Float(num) / den
V d = 23.34375
print(d"\t => "decToBin(d))
V s = ‘1011.11101’
print(s"\t => "binToDec(s))</syntaxhighlight>
{{out}}
<pre>
23.34375 => 10111.01011
1011.11101 => 11.90625
</pre>
=={{header|Ada}}==
<syntaxhighlight lang="ada">
-- Decimal floating point number to binary (and vice versa)
-- J. Carter 2023 Apr
-- We'll presume the input is a string containing the image of the number in the appropriate base, and the output is the
-- image in the other base; using the language's conversion of numeric literals seems like cheating
-- Uses the PragmAda Reusable Components (https://github.com/jrcarter/PragmARC)
with Ada.Strings.Fixed;
with Ada.Text_IO;
with PragmARC.Unbounded_Numbers.Rationals;
procedure Dec_Bin_FP is
use PragmARC.Unbounded_Numbers.Rationals; -- Avoid losing any precision in the inputs
function To_Binary (Decimal : in String) return String is
(Image (Value (Decimal), Base => 2) );
function To_Decimal (Binary : in String) return String is
(Image (Value ("2#" & Binary & '#') ) );
Decimal : constant String := "23.34375";
Binary : constant String := "1011.11101";
Pi : constant String := "3.14159265358979323846264338327950288419716939937511";
begin -- Dec_Bin_FP
Ada.Text_IO.Put_Line (Item => Decimal & ' ' & To_Binary (Decimal) );
Ada.Text_IO.Put_Line (Item => Binary & ' ' & To_Decimal (Binary) );
Ada.Text_IO.Put_Line (Item => Pi & ' ' & To_Binary (Pi) );
end Dec_Bin_FP;
</syntaxhighlight>
{{out}}
<pre>
23.34375 10111.01011
1011.11101 11.90625
3.14159265358979323846264338327950288419716939937511 11.0010010000111111011010101000100010000101101000110000100011010011000100110001100110001010001011100000001101110000011100110100010010100100000010010011100000100010001010110010111101110111001101010111010110100001001100011101010110011100010011100000101110011010001111000010101010101101011011001000010100111011100010111000010011101110010100111100011001101011101101111111010101100100010110111101011100101101100101000100100011001011011000011001000011110100001101001011101001101101110101010011000110010100100000010010000111111110011000000000010101110110001110001110010110111000101110001010110101110111011001010111010111101111011110000101011000110010101011101010110110100011110010110101111111011111010110111101100111101111011011001101011010110011010110000010011100110000001011100111000110011001111000001000001101110100000010000110110000000110001001010111011110011000110010100010010100101000000011101001111010101011001000101010001111101001110110010101101110110110000000000011110111110011110101011010000101001010
</pre>
=={{header|bc}}==
<syntaxhighlight lang="bc">
obase = 2
scale=100
Line 19 ⟶ 91:
</syntaxhighlight>
=={{header|D}}==
{{trans|Python}}
<
immutable string[string] hex2bin, bin2hex;
Line 83 ⟶ 155:
y2.bin2dec.writeln;
writefln("%.6f", "1011.11101p+0".bin2dec);
}</
{{out}}
<pre>1.011101011p+100
Line 94 ⟶ 166:
{{works with|dc|1.3.95 (GNU bc 1.06.95)}}
Interactively:
<
2o
23.34375 p
Line 105 ⟶ 177:
11.90625
q
$</
Directly on the command line:
<
10111.01011000000000000
$ dc -e '2i 1011.11101 p'
Line 116 ⟶ 188:
$ echo '2i 1011.11101 p' | dc
11.90625
$</
From the manpage: "To enter a negative number, begin the number with '_'. '-' cannot be used for this, as it is a binary operator for subtraction instead."
<
-10111.01011000000000000
$ dc -e '2i _1011.11101 p'
-11.90625
$</
=={{header|Delphi}}==
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program FloatToBinTest;
Line 214 ⟶ 286:
Readln;
end.
</syntaxhighlight>
{{out}}
<pre>
Line 222 ⟶ 294:
=={{header|Elixir}}==
<
def dec2bin(dec, precision\\16) do
[int, df] = case String.trim(dec) |> String.split(".") do
Line 274 ⟶ 346:
dec2 = RC.bin2dec(bin)
:io.format "~10s => ~18s =>~12s~n", [dec, bin, dec2]
end)</
{{out}}
Line 289 ⟶ 361:
=={{header|Factor}}==
<
: bin>dec ( x -- y )
Line 295 ⟶ 367:
23.34375 dup >bin
1011.11101 dup bin>dec [ [I ${} => ${}I] nl ] 2bi@</
{{out}}
<pre>
Line 309 ⟶ 381:
An alternative method is to present the text to the I/O system as with <code>READ (ACARD,*) X</code>, except that there is no facility for specifying any base other than ten. In a more general situation the text would first have to be scanned to span the number part, thus incurring double handling. The codes for hexadecimal, octal and binary formats do ''not'' read or write numbers in those bases, they show the bit pattern of the numerical storage format instead, and for floating-point numbers this is very different. Thus, Pi comes out as 100000000001001001000011111101101010100010001000010110100011000 in B64 format, not 11·0010010000111111011010101... Note the omitted high-order bit in the normalised binary floating-point format - a further complication.
The source is F77 style, except for the MODULE usage simply for some slight convenience in sharing DIGIT and not having to re-declare the type of EATNUM. <
CHARACTER*36 DIGIT !A set of acceptable digit characters.
PARAMETER (DIGIT = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") !Not only including hexadecimal.
Line 470 ⟶ 542:
END DO !On to the next test.
END DO !And another base.
END !Enough of that. </
Rather than mess about with invocations, the test interprets the texts firstly as base ten sequences, then base two. It makes no complaint over encountering the likes of "666" when commanded to absorb according to base two. The placewise notation is straightforward: 666 = 6x2<sup>2</sup> + 6x2<sup>1</sup> + 6x2<sup>0</sup>
Line 498 ⟶ 570:
=={{header|FreeBASIC}}==
<
' Expresses (or rounds) the fractional part to the same number of places in binary as the decimal to be converted.
Line 543 ⟶ 615:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 557 ⟶ 629:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 597 ⟶ 669:
s := "1011.11101"
fmt.Printf("%s\t => %v\n", s, binToDec(s))
}</
{{out}}
Line 607 ⟶ 679:
=={{header|Haskell}}==
float to binary part only:
<
import Numeric (floatToDigits, showIntAtBase)
Line 615 ⟶ 687:
main :: IO ()
main = putStrLn $ dec2bin 23.34375</
{{out}}
<pre>
Line 629 ⟶ 701:
Implementation:
<
NB. string to rational number
exp=. (1x+y i.'.')-#y
Line 639 ⟶ 711:
s=. exp&(}.,'.',{.) (":m#.inv mant)-.' '
((exp-1)>.-+/*/\|.s e.'.0') }. s
)</
Example use:
<
10111.01011
10 b2b 2 '1011.11101'
11.90625</
=={{header|Java}}==
Line 652 ⟶ 724:
Precision is not discussed. Most decimal fractional values are not representable in binary. For example, .1 in decimal is a repeating decimal in binary, with a value of .0[0011]... in binary. As a result, a precision of 50 digits is the default. However, this is a helper method and the actual implementation uses the specified precision.
<syntaxhighlight lang="java">
import java.math.BigDecimal;
import java.math.MathContext;
Line 737 ⟶ 809:
}
</syntaxhighlight>
{{out}}
Line 750 ⟶ 822:
=={{header|Julia}}==
<
function dec2bin(x::String)
bx = parse(BigFloat, x)
Line 798 ⟶ 870:
testdata = ["23.34375", "11.90625", "-23.34375", "-11.90625"]
testconversions(testdata)
</
<pre>
String (base 10) Base 2 Base 10
Line 808 ⟶ 880:
=={{header|Kotlin}}==
<
fun decToBin(d: Double): String {
Line 839 ⟶ 911:
val s = "1011.11101"
println("$s\t => ${binToDec(s)}")
}</
{{out}}
Line 846 ⟶ 918:
1011.11101 => 11.90625
</pre>
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
# Decimal floating point number to binary
# # Variables:
#
# # Functions:
#
# # Function _fpdec2bin(n.d) - return binary of floating point decimal n.d
#
function _fpdec2bin {
typeset _fp ; _fp=$1
typeset _base ; integer _base=2
typeset _n _q _r _whole _fract ; integer _n _q _r
typeset _p _d ; float _p _d=.0
typeset -a _arr _barr
_n=${_fp%\.*}
[[ ${_fp} == +(\d)\.*(\d) ]] && _d=${_fp#*${_n}}
(( _q = _n / _base ))
(( _r = _n % _base ))
_arr+=( ${_r} )
until (( _q == 0 )); do
_n=${_q}
(( _q = _n / _base ))
(( _r = _n % _base ))
_arr+=( ${_r} )
done
_revarr _arr _barr
_whole=${_barr[@]} ; _whole=${_whole// /}
(( _p = _d * _base ))
unset _q ; _q=${_p%\.*}
_fract="${_q}" ; float _q
(( _d = _p - _q ))
until (( ${_d} == 0.0 )); do
(( _p = _d * _base ))
unset _q ; _q=${_p%\.*}
_fract+=${_q} ; float _q
(( _d = _p - _q ))
done
echo "${_whole}.${_fract}"
}
# # Function _fpbin2dec(b) - return floating point decimal for binary b
#
function _fpbin2dec {
typeset _b ; _b=$1
[[ ${_b} != *(0|1)*(\.)*(0|1) ]] && return 1
typeset _m ; _m=${_b%\.*} # Before the .
typeset _d ; _d=${_b#*\.} # After the .
typeset _i _p _base ; typeset -si _i _p=0 _base=2
typeset _msum ; typeset -si _msum=0
typeset _psum ; float _psum=0
for ((_i=${#_m}-1; _i>=0; _i--)); do
(( ${_m:_i:1} )) && (( _msum+=(_base**_p) ))
(( _p++ ))
done
_p=1
for ((_i=0; _i<${#_d}; _i++)); do
(( ${_d:_i:1} )) && (( _psum+=(1.0 / (_base**_p)) ))
(( _p++ ))
done
echo "${_msum}.${_psum#*0\.}"
}
# # Function _revarr(arr, barr) - reverse arr into barr
#
function _revarr {
typeset _arr ; nameref _arr="$1"
typeset _barr ; nameref _barr="$2"
typeset _i ; integer _i
for ((_i=${#_arr[*]}-1; _i>=0; _i--)); do
_barr+=( ${_arr[_i]} )
done
}
######
# main #
######
print "Floating point decimal to Binary conversion:"
print "23.34375 => $(_fpdec2bin 23.34375)"
print "11.90625 => $(_fpdec2bin 11.90625)"
print "\nFloating point binary to decimal conversion:"
print "10111.01011 => $(_fpbin2dec 10111.01011)"
print "1011.11101 => $(_fpbin2dec 1011.11101)"
</syntaxhighlight>
{{out}}<pre>
Floating point decimal to Binary conversion:
23.34375 => 10111.01011
11.90625 => 1011.11101
Floating point binary to decimal conversion:
10111.01011 => 23.34375
1011.11101 => 11.90625</pre>
=={{header|LiveCode}}==
LiveCode's baseConvert only works on integers.
Only the first part of this task is complete.
<
put 15 into limit
put 0 into i
Line 867 ⟶ 1,047:
put float2bin(23.34375) // 10111.01011
put float2bin(11.90625) //1011.11101</
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module Checkit {
Conv2dec=lambda (n$, frombase=10, dp$=".") -> {
Line 942 ⟶ 1,122:
}
Checkit
</syntaxhighlight>
{{out}}
<pre>
Line 955 ⟶ 1,135:
=={{header|Maple}}==
<syntaxhighlight lang="maple">
convert(23.34375,binary,decimal);
convert(1011.11101,decimal,binary);
</syntaxhighlight>
Output:
<pre>
Line 968 ⟶ 1,148:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{digits, pos} = (RealDigits[ToExpression@x, 2] /. {a_, b_} :> {ToString /@ a, b});
StringJoin @@
Line 974 ⟶ 1,154:
bin2dec[x_] := FromDigits[RealDigits@ToExpression@x, 2] // N
Print[NumberForm[#, {9, 5}], " => ", dec2bin@#] &@23.34375;
Print[NumberForm[#, {9, 5}], " => ", NumberForm[bin2dec@#, {9, 5}]] &@1011.11101;</
{{out}}
<pre>23.34375 => 10111.01011
Line 981 ⟶ 1,161:
=={{header|Nim}}==
{{trans|Kotlin}}
<
func decToBin(f: float): string =
Line 1,006 ⟶ 1,186:
echo d, " → ", decToBin(d)
let s = "1011.11101"
echo s, " → ", binToDec(s)</
{{out}}
Line 1,019 ⟶ 1,199:
program should perform conversions from any base to any other base.
{{works with|OCaml|4.03+}}
<
#load "str.cma"
(* Using the interpteter or the compiler:
Line 1,262 ⟶ 1,442:
and _ = List.iter (fun tpl -> print_endline (testit tpl)) values
in ()
</syntaxhighlight>
{{out}}
23.343750 => 10111.01011; expected 10111.01011 [PASS]
Line 1,290 ⟶ 1,470:
=={{header|Perl}}==
<
use warnings;
use feature 'say';
Line 1,309 ⟶ 1,489:
say dec2bin(23.34375);
say bin2dec('1011.11101');</
{{out}}
<pre>10111.01011
Line 1,316 ⟶ 1,496:
=={{header|Phix}}==
Handles bases 2..36. Does not handle any form of scientific notation.
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">dec_to</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- convert d to a string in the specified base
Line 1,386 ⟶ 1,566:
<span style="color: #0000FF;">?</span><span style="color: #000000;">to_dec</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"23.7"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">dec_to</span><span style="color: #0000FF;">(</span><span style="color: #000000;">23.7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 1,413 ⟶ 1,593:
===shorter===
Inspired by Kotlin/Go/Wren (no attempt to handle signs, nor will it handle no-dot properly)
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">dec2bin</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">whole</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trunc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
Line 1,443 ⟶ 1,623:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%.5f => %s => %.5f\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s => %.5f => %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">u</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 1,452 ⟶ 1,632:
=={{header|Python}}==
Python has float.hex() and float.fromhex() that can be used to form our own binary format.
<
bin2hex = dict('{:b} {:x}'.format(x,x).split() for x in range(16))
Line 1,485 ⟶ 1,665:
hx = (('-' if neg else '') + '0x' + hx + bn2[p:p+2]
+ str(int('0b' + bn2[p+2:], 2)))
return float.fromhex(hx)</
{{out}}
Line 1,506 ⟶ 1,686:
=={{header|Racket}}==
The binary to number conversion is easy because it's supported by Racket. We can use <code>string->number</code>, wrap it in a dedicated function or use the read extension.
<
(define (string->number/binary x)
Line 1,516 ⟶ 1,696:
#b0.01
(string->number "0.01" 2)
(newline)</
{{out}}
<pre>2.3125
Line 1,524 ⟶ 1,704:
0.25</pre>
Racket only supports the number to binary conversion for integer numbers, so we multiply the original number by a power of two, to get all the binary digits, and then we manipulate the string to place the point in the correct place.
<
(define decimals-places 10)
(define digits-all (~r (inexact->exact (round (* x (expt 2 decimals-places))))
Line 1,543 ⟶ 1,723:
(number->string/binary 9)
(number->string/binary 0.01)
(newline)</
{{out}}
<pre>"1001.000000101"
Line 1,549 ⟶ 1,729:
"0.000000101"</pre>
Some additional interesting examples
<
(string->number/binary (number->string/binary .1))
(newline)
(number->string/binary (string->number/binary "0.11111111111"))
(string->number/binary "0.11111111111")</
{{out}}
<pre>"10110.001101000"
Line 1,564 ⟶ 1,744:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
given "1011.11101" { say "$_ => ", :2($_).base(10) }</
{{out}}
<pre>23.34375 => 10111.01011
Line 1,607 ⟶ 1,787:
This REXX program can handle any sized number (as per the number of digits/numerals) that can be entered at the
<br>command line.
<
parse arg number toBase inBase digits . /*obtain optional arguments from the CL*/
if toBase=='' | toBase=="," then toBase= 10 /*Not specified? Then use the default.*/
Line 1,677 ⟶ 1,857:
return oS || word( strip( space(w), 'L', 0)strip( strip(g, , 0), "T", .) 0, 1)
/*──────────────────────────────────────────────────────────────────────────────────────*/
err: say; say '***error***: ' arg(1); say; exit 13</
{{out|output|text= when using the input of: <tt> 23.34375 2 </tt>}}
<pre>
Line 1,692 ⟶ 1,872:
===version 2===
<
* 08.02.2014 Walter Pachl
*--------------------------------------------------------------------*/
Line 1,759 ⟶ 1,939:
If res<>soll Then
Say 'soll='soll
Return</
'''Output:'''
<pre>23.34375 -> 10111.01011
Line 1,767 ⟶ 1,947:
=={{header|Ring}}==
<
decimals(5)
Line 1,841 ⟶ 2,021:
see "" + numdec + "." + numdig
</syntaxhighlight>
Output:
<pre>
23.34375 => 10111.01011
1011.11101 => 11.90625
</pre>
=={{header|RPL}}==
{| class="wikitable"
! RPL code
! Comment
|-
|
≪
SIGN LAST ABS
DUP BIN R→B →STR 3 OVER SIZE 1 - SUB
"." ROT FP
'''WHILE''' OVER SIZE 12 ≤ OVER AND '''REPEAT'''
2 * DUP IP →STR ROT SWAP +
SWAP FP
'''END''' DROP + STR→
*
≫ ‘<span style="color:blue">R→FB</span>’ STO
≪
SIGN LAST ABS
DUP FP →STR SIZE 1 - SWAP
10 3 PICK ^ *
"#" SWAP →STR + →STR BIN B→R 2 ROT ^ / *
≫ ‘<span style="color:blue">FB→R</span>’ STO
|
<span style="color:blue">R→FB</span> ''( b10_real → b2_real ) ''
Memorize sign, use absolute value
Convert integer part to a binary string
Prepare stack for fractional part
loop until zero or decimal part size > 12
multiply by 2, take integer part as next decimal digit
keep fractional part
clean stack, add strings and convert back to floating point
restore sign
<span style="color:blue">FB→R</span> ''( b2_real → b10_real )''
Memorize sign, use absolute value
Get n = # of digits after the decimal point
Multiply input number by 10^n
Convert to base 10, divide by 2^n and restore sign
|}
23.34375 <span style="color:blue">R→FB</span>
1011.11101 <span style="color:blue">FB→R</span>
{{out}}
<pre>
2: 10111.01011
1: 11.90625
</pre>
=={{header|Ruby}}==
<
int, df = dec.split(".")
minus = int.delete!("-")
Line 1,885 ⟶ 2,115:
dec2 = bin2dec(bin)
puts "%10s => %12s =>%10s" % [dec, bin, dec2]
end</
{{out}}
Line 1,897 ⟶ 2,127:
=={{header|Scala}}==
===Idiomatic (FP with tailrec)===
<
import scala.annotation.tailrec
Line 1,935 ⟶ 2,165:
println( { def s = "1011.11101"; s"$s\t => ${binToDec(s)}" } )
}
}</
{{Out}}Experience running it in your browser by [https://scastie.scala-lang.org/auzWgFqCRBaYoOaJV92tgw Scastie (remote JVM)].
=={{header|Sidef}}==
<
Num(Num(n, 10).base(2), 10)
}
Line 1,948 ⟶ 2,178:
with("23.34375") { |s| say (" #{s} => ", dec2bin(s)) }
with("1011.11101") { |s| say ( "#{s} => ", bin2dec(s)) }</
{{out}}
<pre>
Line 1,958 ⟶ 2,188:
By far the easiest way to do this is to use Tcl's built-in handling of IEEE arithmetic, converting the IEEE representation into the string representation we want (and ''vice versa'') by simple string manipulations.
{{works with|Tcl|8.6}}
<
proc dec2bin x {
Line 2,015 ⟶ 2,245:
set d [bin2dec $b]
puts "$case => $b => $d"
}</
{{out}}
<pre>
Line 2,035 ⟶ 2,265:
{{trans|Go}}
{{libheader|Wren-fmt}}
<
var decToBin = Fn.new { |d|
Line 2,066 ⟶ 2,296:
Fmt.print("$g\t => $s", f, decToBin.call(f))
var s = "1011.11101"
Fmt.print("$s\t => $g", s, binToDec.call(s))</
{{out}}
Line 2,076 ⟶ 2,306:
=={{header|zkl}}==
Rather limited.
<
bstr=bstr.strip();
m:=bstr[0,1]=="-"; if(m)bstr=bstr[1,*]; m=m and -1 or 1;
a,b:=bstr.split(".").apply(fcn(s){ s and s or 0 }).append(0,0);
(a.toInt(2).toFloat() + b.toInt(2).toFloat()/(2).pow(b.len()))*m
}</
<
println(bstr," --> ",bin2float(bstr).toString(20))
}</
{{out}}
<pre>
Line 2,094 ⟶ 2,324:
1 --> 1
</pre>
<
m,zeros:="","0"*digitsOfPrecision;
if(x<0){ m="-"; x=-x }
Line 2,102 ⟶ 2,332:
if(z:=b.reverse().prefix(zeros)) b=b[0,-z]; // remove trailing zeros
String(m,a.toString(2),".",b);
}</
<
println(x," --> ",s:=float2bin(x)," --> ",bin2float(s).toString(20));
}</
{{out}}
<pre>
|