Decimal floating point number to binary: Difference between revisions

added RPL
m (→‎{{header|Wren}}: Minor tidy)
(added RPL)
 
Line 2,026:
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>
 
1,150

edits