Generalised floating point multiplication: Difference between revisions
Content added Content deleted
m (→{{header|ALGOL 68}}: {{incorrect|ALGOL 68|The result a*(b-c) should be about -262510.90267998143}}) |
m (→{{header|ALGOL 68}}: remove broken optimsation) |
||
Line 55: | Line 55: | ||
--> |
--> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{incorrect|ALGOL 68|The result a*(b-c) should be about -262510.90267998143}} |
|||
{{works with|ALGOL 68|Revision 1 - one minor extension to language used - PRAGMA READ, similar to C's #include directive.}} |
{{works with|ALGOL 68|Revision 1 - one minor extension to language used - PRAGMA READ, similar to C's #include directive.}} |
||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.3.3 algol68g-2.3.3].}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.3.3 algol68g-2.3.3].}} |
||
Line 80: | Line 79: | ||
DIGIT zero = ZERO LOC DIGIT; |
DIGIT zero = ZERO LOC DIGIT; |
||
DIGIT one = IDENTITY LOC DIGIT; |
DIGIT one = IDENTITY LOC DIGIT; |
||
[order + MSD a+MSD b: LSD a+LSD b]DIGIT a |
[order + MSD a+MSD b: LSD a+LSD b]DIGIT a x b; |
||
FOR place FROM LSD a+LSD b BY order TO LSD a+MSD b DO |
FOR place FROM LSD a+LSD b BY order TO LSD a+MSD b DO |
||
a |
a x b[place] := zero # pad the MSDs of the result with Zero # |
||
OD; |
OD; |
||
FOR place a FROM LSD a BY order TO MSD a DO |
FOR place a FROM LSD a BY order TO MSD a DO |
||
Line 90: | Line 89: | ||
FOR place b FROM LSD b BY order TO MSD b DO |
FOR place b FROM LSD b BY order TO MSD b DO |
||
DIGIT digit b = b[place b]; |
DIGIT digit b = b[place b]; |
||
REF DIGIT digit ab = a |
REF DIGIT digit ab = a x b[place a + place b]; |
||
IF |
IF carry OF arithmetic THEN # used for big number arithmetic # |
||
MOID(carry := ( digit ab +:= carry )); |
|||
DIGIT prod := digit a; |
|||
MOID(carry +:= ( prod *:= digit b )); |
|||
MOID(carry +:= ( digit ab +:= prod )) |
|||
ELSE # carry = 0 so we can just ignore the carry # |
|||
DIGIT prod := digit a; |
|||
MOID(prod *:= digit b); |
|||
MOID(digit ab +:= prod) |
|||
MOID(digit ab +:= prod) |
|||
FI |
|||
FI |
FI |
||
OD; |
OD; |
||
a |
a x b[place a + MSD b + order] := carry |
||
OD; |
OD; |
||
INITDIGITS a |
INITDIGITS a x b # normalise # |
||
FI |
FI |
||
); |
); |
||
Line 124: | Line 121: | ||
# First: define the attributes of the arithmetic we are using. # |
# First: define the attributes of the arithmetic we are using. # |
||
################################################################ |
################################################################ |
||
⚫ | |||
CO STRUCT ( |
|||
BOOL balanced, |
|||
carry, # aka "carry" between digits # |
|||
INT base, |
|||
digit width, |
|||
digit places, |
|||
digit order, |
|||
USTRING repr |
|||
⚫ | |||
# balanced = # TRUE, |
# balanced = # TRUE, |
||
# carry = # TRUE, |
# carry = # TRUE, |
||
Line 153: | Line 142: | ||
BIGREAL out; |
BIGREAL out; |
||
BIGREAL base of arithmetic = INITBIGREAL base OF arithmetic; # Todo: Opt # |
BIGREAL base of arithmetic = INITBIGREAL base OF arithmetic; # Todo: Opt # |
||
INT point := UPB s |
INT point := UPB s; # put the point on the extreme right # |
||
FOR |
FOR place FROM LWB s TO UPB s DO |
||
IF s[ |
IF s[place]="." THEN |
||
point := |
point := place |
||
ELSE |
ELSE |
||
out := out SHR digit order OF arithmetic + INITDIGIT s[ |
out := out SHR digit order OF arithmetic + INITDIGIT s[place] |
||
FI |
FI |
||
OD; |
OD; |
||
out SHR (UPB s-point) |
out SHR (UPB s-point) |
||
);</lang>'''File: test.Balanced_ternary_float.Multiplication.a68'''<lang algol68>#!/usr/local/bin/a68g --script # |
);</lang>'''File: test.Balanced_ternary_float.Multiplication.a68'''<lang algol68>#!/usr/local/bin/a68g --script # |
||
#################################################################### |
#################################################################### |
||
# A program to test arbitrary length floating point multiplication # |
# A program to test arbitrary length floating point multiplication # |
||
Line 215: | Line 203: | ||
<pre> |
<pre> |
||
a = +523.23914037494284407864655 +-0++0+.+-0++0+ |
a = +523.23914037494284407864655 +-0++0+.+-0++0+ |
||
b = -436.43600000000000000000000 |
b = -436.43600000000000000000000 -++-0--.--0+-00+++-0-+---0-+0++++0--0000+00-+-+--+0-0-00--++0-+00---+0+-+++0+-0----0++ |
||
c = +65.26748971193415637860082 +-++-.+-++- |
c = +65.26748971193415637860082 +-++-.+-++- |
||
a*(b-c) - |
a*(b-c) -262510.90267998140903693919 ----000-0+0+.0+0-0-00---00--0-0+--+--00-0++-000++0-000-+0+-----+++-+-0+-+0+0++0+0-++-++0+---00++++ |
||
# | * |+ #1 |+- #2 |+0 #3 |++ #4 |+-- #5 |+-0 #6 |+-+ #7 |+0- #8 |+e+- #9|+0+ #10|++- #11|++0 #12| |
# | * |+ #1 |+- #2 |+0 #3 |++ #4 |+-- #5 |+-0 #6 |+-+ #7 |+0- #8 |+e+- #9|+0+ #10|++- #11|++0 #12| |
||
Line 227: | Line 215: | ||
6 |+-0 |+-0 |++0 |+-e+- |+0-0 |+0+0 |++e+- | | | | | | | |
6 |+-0 |+-0 |++0 |+-e+- |+0-0 |+0+0 |++e+- | | | | | | | |
||
7 |+-+ |+-+ |+--- |+-+0 |+00+ |++0- |+---0 |+--++ | | | | | | |
7 |+-+ |+-+ |+--- |+-+0 |+00+ |++0- |+---0 |+--++ | | | | | | |
||
8 |+0- |+0- |+--+ |+0-0 |++-- |++++ |+--+0 |+-0+- |+0+ |
8 |+0- |+0- |+--+ |+0-0 |++-- |++++ |+--+0 |+-0+- |+-+0+ | | | | | |
||
9 |+e+- |+e+- |+-e+- |+e+0 |++e+- |+--e+- |+-e+0 |+-+e+- |+0-e+- |+e++ | | | | |
9 |+e+- |+e+- |+-e+- |+e+0 |++e+- |+--e+- |+-e+0 |+-+e+- |+0-e+- |+e++ | | | | |
||
10|+0+ |+0+ |+-+- |+0+0 |++++ |+-0-- |+-+-0 |+0--+ |+000- |+0+e+- |+ |
10|+0+ |+0+ |+-+- |+0+0 |++++ |+-0-- |+-+-0 |+0--+ |+000- |+0+e+- |++-0+ | | | |
||
11|++- |++- |+-++ |++-0 |+--0- |+-00+ |+-++0 |+00-- |++-+ |
11|++- |++- |+-++ |++-0 |+--0- |+-00+ |+-++0 |+00-- |+0+-+ |++-e+- |++0+- |+++++ | | |
||
12|++0 |++0 |+0-0 |++e+- |+--+0 |+-+-0 |+0-e+- |+00+0 |++--0 |++e+0 |++++0 |+--0-0 |+--+e+-| |
12|++0 |++0 |+0-0 |++e+- |+--+0 |+-+-0 |+0-e+- |+00+0 |++--0 |++e+0 |++++0 |+--0-0 |+--+e+-| |
||
13|+++ |+++ |+00- |+++0 |+-0-+ |+-++- |+00-0 |+0+0+ |++0-- |+++e+- |+- |
13|+++ |+++ |+00- |+++0 |+-0-+ |+-++- |+00-0 |+0+0+ |++0-- |+++e+- |+---++ |+--+0- |+-0-+0 | |
||
14|+--- |+--- |+00+ |+---0 |+-0+- |+0--+ |+00+0 |++-0- | |
14|+--- |+--- |+00+ |+---0 |+-0+- |+0--+ |+00+0 |++-0- |++0++ |+---e+-|+--+-- |+-0-0+ |+-0+-0 | |
||
15|+--0 |+--0 |+0+0 |+--e+- |+-+-0 |+0-+0 |+0+e+- |++0-0 | |
15|+--0 |+--0 |+0+0 |+--e+- |+-+-0 |+0-+0 |+0+e+- |++0-0 |++++0 |+--e+0 |+-0--0 |+-00+0 |+-+-e+-| |
||
16|+--+ |+--+ |++-- |+--+0 |+-+0+ |+000- |++--0 |++0++ |+-+- |
16|+--+ |+--+ |++-- |+--+0 |+-+0+ |+000- |++--0 |++0++ |+---+- |+--+e+-|+-00-+ |+-+--- |+-+0+0 | |
||
17|+-0- |+-0- |++-+ |+-0-0 |+0--- |+00++ |++-+0 |++++- |+--00+ |+-0-e+-|+ |
17|+-0- |+-0- |++-+ |+-0-0 |+0--- |+00++ |++-+0 |++++- |+--00+ |+-0-e+-|+-0+0- |+-+0-+ |+0---0 | |
||
18|+-e+-|+-e+- |++e+- |+-e+0 |+0-e+- |+0+e+- |++e+0 |+---e+-|+--+e+-|+-e++ |+-+-e+-|+-++e+-|+0-e+0 | |
18|+-e+-|+-e+- |++e+- |+-e+0 |+0-e+- |+0+e+- |++e+0 |+---e+-|+--+e+-|+-e++ |+-+-e+-|+-++e+-|+0-e+0 | |
||
19|+-0+ |+-0+ |+++- |+-0+0 |+0-++ |++--- |+++-0 |+--0-+ |+ |
19|+-0+ |+-0+ |+++- |+-0+0 |+0-++ |++--- |+++-0 |+--0-+ |+-0-0- |+-0+e+-|+-+00+ |+0--+- |+0-++0 | |
||
20|+-+- |+-+- |++++ |+-+-0 |+000- |++-0+ |++++0 |+--+-- |+-00-+ |+-+-e+-| |
20|+-+- |+-+- |++++ |+-+-0 |+000- |++-0+ |++++0 |+--+-- |+-00-+ |+-+-e+-|+-+++- |+0-0++ |+000-0 | |
||
21|+-+0 |+-+0 |+---0 |+-+e+- |+00+0 |++0-0 |+---e+-|+--++0 |+-0+-0 |+-+e+0 |+ |
21|+-+0 |+-+0 |+---0 |+-+e+- |+00+0 |++0-0 |+---e+-|+--++0 |+-0+-0 |+-+e+0 |+0--+0 |+00--0 |+00+e+-| |
||
22|+-++ |+-++ |+--0- |+-++0 |+0+-+ |++0+- |+--0-0 |+-0-0+ |+ |
22|+-++ |+-++ |+--0- |+-++0 |+0+-+ |++0+- |+--0-0 |+-0-0+ |+-+--- |+-++e+-|+0-0++ |+0000- |+0+-+0 | |
||
23|+0-- |+0-- |+--0+ |+0--0 |+0++- |+++-+ |+--0+0 |+-000- |+-++ |
23|+0-- |+0-- |+--0+ |+0--0 |+0++- |+++-+ |+--0+0 |+-000- |+-+-++ |+0--e+-|+00--- |+00+0+ |+0++-0 | |
||
24|+0-0 |+0-0 |+--+0 |+0-e+- |++--0 |++++0 |+--+e+-|+-0+-0 |+0+0 |
24|+0-0 |+0-0 |+--+0 |+0-e+- |++--0 |++++0 |+--+e+-|+-0+-0 |+-+0+0 |+0-e+0 |+000-0 |+0+-+0 |++--e+-| |
||
25|+0-+ |+0-+ |+-0-- |+0-+0 |++-0+ |+---0- |+-0--0 |+-0+++ |+++- |
25|+0-+ |+0-+ |+-0-- |+0-+0 |++-0+ |+---0- |+-0--0 |+-0+++ |+-+++- |+0-+e+-|+00+-+ |+0++-- |++-0+0 | |
||
26|+00- |+00- |+-0-+ |+00-0 |++0-- |+---++ |+-0-+0 |+-+-+- |+0--0+ |+00-e+-|+0+-0- |++---+ |++0--0 | |
26|+00- |+00- |+-0-+ |+00-0 |++0-- |+---++ |+-0-+0 |+-+-+- |+0--0+ |+00-e+-|+0+-0- |++---+ |++0--0 | |
||
27|+e+0 |+e+0 |+-e+0 |+e++ |++e+0 |+--e+0 |+-e++ |+-+e+0 |+0-e+0 |+e+-- |+0+e+0 |++-e+0 |++e++ | |
27|+e+0 |+e+0 |+-e+0 |+e++ |++e+0 |+--e+0 |+-e++ |+-+e+0 |+0-e+0 |+e+-- |+0+e+0 |++-e+0 |++e++ | |
||
</pre> |
</pre> |
||
[[Category:Arbitrary precision]] |
[[Category:Arbitrary precision]] |