Generalised floating point addition: Difference between revisions

moved REXX to its proper place alphabetically. -- ~~~~
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
(moved REXX to its proper place alphabetically. -- ~~~~)
Line 524:
</pre>
[[Category:Arbitrary precision]]
 
=={{header|J}}==
 
I am not currently able to implement the task exactly because I do not quite understand what is being asked for (nor why it would be useful).
 
That said, the task does specify some calculations to be performed.
 
Given
 
<lang j>e=: 2 : 0
u * 10x ^ v
)</lang>
 
In other words, given a parse time word (<code>e</code>) which combines its two arguments as numbers, multiplying the number on its left by the exact exponent of 10 given on the right, I can do:
 
<lang> 1 e 63 + 12345679 e 63 * 81
1000000000000000000000000000000000000000000000000000000000000000000000000
1 e 54 + 12345679012345679 e 54 * 81
1000000000000000000000000000000000000000000000000000000000000000000000000
1 e 45 + 12345679012345679012345679x e 45 * 81
1000000000000000000000000000000000000000000000000000000000000000000000000
1 e 36 + 12345679012345679012345679012345679x e 36 * 81
1000000000000000000000000000000000000000000000000000000000000000000000000</lang>
 
So, ok, let's turn this into a sequence:
 
<lang j>factor=: [: +/ [: 12345679 e ] _9 * 1 + i.&.(+&8)
adjust=: 1 e (_9&*)</lang>
 
Here we show some examples of what these words mean:
 
<lang j> factor _4 NB. this is the number we multiply by 81
12345679012345679012345679012345679000000000000000000000000000000000000
factor _3
12345679012345679012345679012345679012345679000000000000000000000000000
factor 2 NB. here we see that we are using rational numbers
12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679r1000000000000000000
90j18 ": factor 2 NB. formatted as decimal in 90 characters with 18 characters after the decimal point
12345679012345679012345679012345679012345679012345679012345679012345679.012345679012345679
adjust _4 NB. this is the number we add to the result of multiplying our factor by 81
1000000000000000000000000000000000000
adjust _3
1000000000000000000000000000</lang>
 
Given these words:
 
<lang j>
_7+i.29 NB. these are the sequence elements we are going to generate
_7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#(adju + 81 * factor)&> _7+i.29 NB. we generate a sequence of 29 numbers
29
~.(adju + 81 * factor)&> _7+i.29 NB. here we see that they are all the same number
1000000000000000000000000000000000000000000000000000000000000000000000000</lang>
 
Note that <code>~. list</code> returns the unique elements from that list.
 
=={{header|REXX}}==
Line 635 ⟶ 690:
21 sum=1E+72
</pre>
 
=={{header|J}}==
 
I am not currently able to implement the task exactly because I do not quite understand what is being asked for (nor why it would be useful).
 
That said, the task does specify some calculations to be performed.
 
Given
 
<lang j>e=: 2 : 0
u * 10x ^ v
)</lang>
 
In other words, given a parse time word (<code>e</code>) which combines its two arguments as numbers, multiplying the number on its left by the exact exponent of 10 given on the right, I can do:
 
<lang> 1 e 63 + 12345679 e 63 * 81
1000000000000000000000000000000000000000000000000000000000000000000000000
1 e 54 + 12345679012345679 e 54 * 81
1000000000000000000000000000000000000000000000000000000000000000000000000
1 e 45 + 12345679012345679012345679x e 45 * 81
1000000000000000000000000000000000000000000000000000000000000000000000000
1 e 36 + 12345679012345679012345679012345679x e 36 * 81
1000000000000000000000000000000000000000000000000000000000000000000000000</lang>
 
So, ok, let's turn this into a sequence:
 
<lang j>factor=: [: +/ [: 12345679 e ] _9 * 1 + i.&.(+&8)
adjust=: 1 e (_9&*)</lang>
 
Here we show some examples of what these words mean:
 
<lang j> factor _4 NB. this is the number we multiply by 81
12345679012345679012345679012345679000000000000000000000000000000000000
factor _3
12345679012345679012345679012345679012345679000000000000000000000000000
factor 2 NB. here we see that we are using rational numbers
12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679r1000000000000000000
90j18 ": factor 2 NB. formatted as decimal in 90 characters with 18 characters after the decimal point
12345679012345679012345679012345679012345679012345679012345679012345679.012345679012345679
adjust _4 NB. this is the number we add to the result of multiplying our factor by 81
1000000000000000000000000000000000000
adjust _3
1000000000000000000000000000</lang>
 
Given these words:
 
<lang j>
_7+i.29 NB. these are the sequence elements we are going to generate
_7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#(adju + 81 * factor)&> _7+i.29 NB. we generate a sequence of 29 numbers
29
~.(adju + 81 * factor)&> _7+i.29 NB. here we see that they are all the same number
1000000000000000000000000000000000000000000000000000000000000000000000000</lang>
 
Note that <code>~. list</code> returns the unique elements from that list.