Decimal floating point number to binary: Difference between revisions

J - initial draft
m (→‎{{header|REXX}}: recoded a subroutine to be in-line. -- ~~~~)
(J - initial draft)
Line 3:
23.34375 => 10111.01011<br />
1011.11101 => 11.90625
 
=={{header|J}}==
 
In this draft, the task does not give any guidelines for handling precision. So we will use 99 places after the decimal point and trim any trailing zeros (and the decimal point, for integer case).
 
Also, since J does not have a "Decimal floating point number" data type, we will use a list of characters to represent a decimal or binary number (this corresponds roughly with the relevant feature set of REXX which seems to have had a strong influence on this draft of this task), and use an internal (mantissa,exponent) during the conversion.
 
Implementation:
 
<lang J>b2b=:2 :0
NB. string to rational number
exp=. (1x+y i.'.')-#y
mant=. n#.0"."0 y-.'.'
number=. mant*n^exp*'.' e. y
NB. rational number to string
exp=. _99
mant=. <.1r2+number*m^x:-exp
s=. exp&(}.,'.',{.) (":m#.inv mant)-.' '
((exp-1)>.-+/*/\|.s e.'.0') }. s
)</lang>
 
Example use:
 
<lang J> 2 b2b 10 '23.34375'
10111.01011
10 b2b 2 '1011.11101'
11.90625</lang>
 
=={{header|REXX}}==
6,962

edits