Decimal floating point number to binary: Difference between revisions
Content added Content deleted
Line 592: | Line 592: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<lang M2000 Interpreter> |
<lang M2000 Interpreter> |
||
Set slow |
|||
Module Checkit { |
Module Checkit { |
||
Conv2dec=lambda (n$, frombase=10, dp$=".") -> { |
Conv2dec=lambda (n$, frombase=10, dp$=".") -> { |
||
Line 620: | Line 621: | ||
=n0 |
=n0 |
||
} |
} |
||
Conv2Any$=Lambda$ (dec, tobase=10, dp$=".") -> { |
Conv2Any$=Lambda$ (dec, tobase=10, dp$=".", prec=16) -> { |
||
a$="" |
a$="" |
||
neg=false |
neg=false |
||
Line 626: | Line 627: | ||
dec=abs(dec) |
dec=abs(dec) |
||
n2=frac(dec) |
n2=frac(dec) |
||
if dec=0 then { |
|||
a$="0" |
|||
} else { |
|||
do { |
|||
n=dec mod tobase |
|||
if n>=10 then n+=7 |
|||
a$=chr$(n+48)+a$ |
|||
dec=dec div tobase |
|||
} until dec==0 |
|||
} |
|||
if n2<>0 then { |
if n2<>0 then { |
||
a$+=dp$ |
a$+=dp$ |
||
prec-- |
|||
do { |
do { |
||
prec-- |
|||
dec=n2*tobase |
dec=n2*tobase |
||
n2=frac(dec) |
n2=frac(dec) |
||
dec-=n2 |
dec-=n2 |
||
n2=round(n2) |
|||
if dec>=10 then dec+=7 |
if dec>=10 then dec+=7 |
||
a$+=chr$(dec+48) |
a$+=chr$(dec+48) |
||
} until n2 |
} until n2=0 or prec<0 |
||
} |
} |
||
if neg then {="-"+a$} else =a$ |
if neg then {="-"+a$} else =a$ |
||
} |
} |
||
Rem : Locale 1033 ' use . for all print out for decimal point |
|||
Print Conv2dec("10111.01011",2); " => ";Conv2Any$(23.34375,2) |
Print Conv2dec("10111.01011",2); " => ";Conv2Any$(23.34375,2) |
||
Print Conv2Any$(11.90625, 2); " => "; Conv2dec("1011.11101",2) |
Print Conv2Any$(11.90625, 2); " => "; Conv2dec("1011.11101",2) |
||
\\ using , for decimal point |
\\ using , for decimal point |
||
Print Conv2Any$(Conv2dec("1011,11101",2, ","), 10, ",") |
Print Conv2Any$(Conv2dec("1011,11101",2, ","), 10, ",") |
||
Print 12312321.1212 |
|||
Print Conv2Any$(12312321.1212, 2) |
|||
\\ using . for 1033 locale |
|||
Print Str$(Conv2Dec(Conv2Any$(12312321.1212, 2), 2), 1033)="12312321.1211853" |
|||
} |
} |
||
Checkit |
Checkit |
||
Line 656: | Line 669: | ||
1011.11101 => 11.90625 |
1011.11101 => 11.90625 |
||
11,90625 |
11,90625 |
||
12312321.1212 |
|||
101110111101111100000001.0001111100000110 |
|||
True |
|||
</pre> |
</pre> |
||