Generalised floating point addition: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(52 intermediate revisions by 14 users not shown)
Line 1:
{{Template:Draft task}}
It is possible to implement floating point arithmetic in many different ways; though the standard IEEE arithmetic implementation on modern hardware usually uses a fixed-width binary implementation (using a total of 32 bits for single-precision floats — <code>float</code> in a number of languages — and 64 bits for double-precision floats — <code>double</code>) many other schemes can be used. For example, the base need not be binary (alternatives include decimal, [[wp:Binary-coded decimal|binary-coded decimal]], and even [[balanced ternary]]) and there is no requirement that a fixed number of digits be used.
If possible, define addition for floating point numbers where the digits are stored in an arbitrary base. eg. the digits can be stored as binary, decimal, [[wp:Binary-coded decimal|Binary-coded decimal]], or even [[wp:Balanced ternary|Balanced ternary]].
 
Define addition for floating point numbers where the digits are stored in an arbitrary base; e.g. the digits can be stored as binary, decimal, [[wp:Binary-coded decimal|binary-coded decimal]], or even [[balanced ternary]].
Implement the code in a generalised form (such as a [[wp:Template (programming)|Template]], [[wp:Modular programming|Module]] or [[wp:Mixin|Mixin]] etc) that permits reusing of the code for different [[wp:Base_(exponentiation)#In_numeral_systems|Bases]].
You should implement the code in a generalised form (such as a [[wp:Template (programming)|Template]], [[wp:Modular programming|Module]] or [[wp:Mixin|Mixin]] etc) that permits reusing of the code for different [[wp:Base_(exponentiation)#In_numeral_systems|Bases]].
 
If it is not possible to implement code inby providing an implementation that uses the ''syntax'' of the specific language then:
* noteNote the reason why this is.
* performDemonstrate that the language still supports the semantics of generalised floating point by implementing the ''test case'' using a built-in code or externala library.
 
'''Test case:'''
 
Use the Template to defineDefine [[wp:Arbitrary-precision arithmetic|Arbitraryarbitrary precision addition]] on floating point numbers stored(e.g., inencoding those numbers using Binary Coded Decimal, or using an arbitrary precision integer arithmetic library). Calculate the terms for -7 to 21 in this sequence of calculations:
{|class="wikitable" style="text-align: center; margin: 1em auto 1em auto;"
|+ Calculate the terms for -7 to 21 in this sequence of calculations
Line 26 ⟶ 27:
|etc.|| The final calculation will be over 256 digits wide || 1e72
|}
PerformYou will either need to implement multiplication, or perform the multiplication ofby 81 by using repeated additions. The results will always be 1e72.
 
;Bonus
Make the template (module, class, mixing, etc.) able to successfully handle using other bases to perform the above test case. Demonstrate using [[balanced ternary]].
 
Ideally the template should be able to successfully handle other bases - such as [[wp:Balanced ternary|Balanced ternary]] - to perform the above test case.
=={{header|ALGOL 68}}==
{{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].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
''Note:'' This code stores the digits as array of digits with the "most significant digit" on the "left" as per normal "human" form. The net effect is that whole numbers (such as 100) are stored in the negative array positions, eg -2, -1 & 0, or [-2:0], And the fractional part of the floating point numbers are stored from index 1, eg. 1, 2, 3 etc. or [1:].
 
'''See also:''' [[Generalised floating point multiplication#ALGOL 68|Generalised floating point multiplication]]
'''File: Mixin_Big_float_base.a68'''
<lang algol68>################################################
# Define the basic operators and routines for #
# manipulating DIGITS in a generalised base #
################################################
MODE STRUCTDIGIT = STRUCT(DIGIT digit);
MODE DIGITS = STRUCT(FLEX[0]STRUCTDIGIT digits);
BOOL balanced arithmetic = FALSE;
INT digit order = -1;
 
'''File: Template.Big_float.Addition.a68''' - task code<syntaxhighlight lang="algol68">########################################
OP ZERO = (STRUCTDIGIT skip)STRUCTDIGIT: INITSTRUCTDIGIT 0;
OP ONE = (STRUCTDIGIT skip)STRUCTDIGIT: INITSTRUCTDIGIT 1;
 
# STRUCTDIGIT OPerators #
OP INITSTRUCTDIGIT = (#LONG# INT i)STRUCTDIGIT: (STRUCTDIGIT out; digit OF out := #SHORTEN# i; out);
#OP INITSTRUCTDIGIT = (INT i)STRUCTDIGIT: INITSTRUCTDIGIT LENG i;#
 
OP /= = (STRUCTDIGIT a,b)BOOL: digit OF a /= digit OF b;
 
# Define additive and multiplicative identities #
OP ZERO = (DIGITS skip)DIGITS: INITDIGITS []DIGIT(0),
ONE = (DIGITS skip)DIGITS: INITDIGITS []DIGIT(1);
 
# Define OPerators for Least and Most Significant DIGIT #
OP MSD = (DIGITS t)INT: LWB digits OF t,
LSD = (DIGITS t)INT: UPB digits OF t;
 
# Define the requitred coercion/casting operators #
OP INITDIGITS = ([]DIGIT in)DIGITS:
(STRUCT(FLEX[LWB in:UPB in]STRUCTDIGIT digits)out; digit OF digits OF out := in; out);
 
OP INITDIGITS = (DIGIT in)DIGITS: INITDIGITS []DIGIT(in);
 
OP INITDIGITS = ([]STRUCTDIGIT in)DIGITS:
(DIGITS out; digits OF out := in; out);
 
# A routine for removing leadng and trailing zeros #
PROC digits normalise = ([]DIGIT in)[]DIGIT: (
DIGIT zero = 0 # ZERO LOC DIGIT#;
INT highest := LWB in, lowest := UPB in;
FOR place FROM highest BY -digit order TO lowest DO
IF in[place] /= zero THEN
highest := place;
done highest
FI
OD;
highest := lowest+digit order;
done highest:
FOR place FROM lowest BY digit order TO highest DO
IF in[place] /= zero THEN
lowest := place;
done lowest
FI
OD;
lowest := highest+digit order;
done lowest:
IF highest=lowest+digit order THEN # normalise zero's exponent #
in[highest:lowest][@0]
ELSE
in[highest:lowest][@highest]
FI
);</lang>'''File: Mixin_Big_float_addition.a68'''
<lang algol68>########################################
# Define the basic addition operators #
# for the generalised base #
########################################
 
# derived DIGIT operators #
# Note: +:= returns carry #
OP +:= = (REF STRUCTDIGIT lhs, STRUCTDIGITDIGIT arg)STRUCTDIGITDIGIT: (arg;
OP + = (DIGIT a,b)DIGIT: (DIGIT out := a; MOID(out +:= b); out);
STRUCTDIGIT carry;
digit OF carry := digit OF lhs +:= digit OF arg;
carry
);
 
# derived hybrid of DIGIT & DIGITS operators #
OP +:= = (REF DIGITS lhs, DIGITS arg)DIGITS: lhs := lhs + arg;
OP + = (DIGITS a, DIGIT b)DIGITS: a + INITDIGITS b;
OP + = (DIGIT a, DIGITS b)DIGITS: INITDIGITS a + a;
OP +:= = (REF DIGITS lhs, DIGIT arg)DIGITS: lhs := lhs + arg;
 
# derived DIGITS operators #
OP + = (DIGITS arg)DIGITS: arg;
OP +:= = (REF DIGITS lhs, DIGITS arg)DIGITS: lhs := lhs + arg;
 
####################################
OP + = (DIGITS in a, in b)DIGITS: (
# TASK CODE #
# Note: []STRUCTDIGIT(~) cast removes FLEX #
# Actual generic addition operator #
[]DIGIT a = digit OF []STRUCTDIGIT(digits OF in a);
####################################
[]DIGIT b = digit OF []STRUCTDIGIT(digits OF in b);
OP + = (DIGITS a, b)DIGITS: (
INT extreme highest = MSD in a MIN MSD in b,
IF SIGN a = overlap0 highestTHEN =b MSDELIF inSIGN ab MAX= MSD0 inTHEN b,a
ELSE
overlap lowest = LSD in a MIN LSD in b,
MODE SIGNED = DIGIT;
extreme lowest = LSD in a MAX LSD in b;
 
INT extreme highest = MSD a MIN MSD b,
DIGIT zero = 0 # ZERO LOC DIGIT#; # "a" can be zero length #
overlap highest = MSD a MAX MSD b,
# DIGITS out; #
overlap lowest = LSD a MIN LSD b,
extreme lowest = LSD a MAX LSD b;
 
SIGNED zero = ZERO LOC SIGNED;
IF overlap highest > overlap lowest THEN # Either: NO overlapping digits #
INT order = digit order OF arithmetic;
DIGITS out;
 
IF MSDoverlap in ahighest > LSDoverlap in alowest THEN # aEither: NO =overlapping 0digits #
# out := # in b
ELIF MSD in b > LSD in b THEN # b = 0 #
# out := # in a
ELSE
[extreme highest:extreme lowest]DIGIT a plus b;
 
[extreme highest:extreme lowest]SIGNED a plus b;
# First: simply insert the known digits #
 
a plus b[MSD in a:LSD in a] := a[:];
# First: simply insert the known digits with their correct sign #
a plus b[MSD in b:LSD in b] := b[:];
a plus b[MSD a:LSD a] := a[@1];
a plus b[MSD b:LSD b] := b[@1];
 
# Next: Zero any totally non overlapping digit #
FOR place FROM overlap highest+digit order BY digit order TO overlap lowest-digit order
DO a plus b[place] := zero OD;
 
# Finally: normalise by removing leading & trailing "zero" digit #
# out := # INITDIGITS digits normalise(a plus b)
FI
 
ELSE # Or: Add ALL overlapping digits #
 
[extreme highest+(digit carry|digit OF arithmetic|order|0):extreme lowest]DIGITSIGNED a plus b;
 
# First: Deal with the non overlapping Least Significant Digits #
a plus b[overlap lowest-digit order:] := (LSD in a > LSD in b|a|b) [overlap lowest-digit order:];
 
# Or: Add any overlapping digits #
DIGIT SIGNED carry := zero;
FOR place FROM overlap lowest BY digit order TO overlap highest DO
DIGIT SIGNED digit a = a[place], digit b = b[place];
REF SIGNED result = digita b =plus b[place];
IF carry OF arithmetic THEN # used in big float #
REF DIGIT result = a plus b[place];
IF digit carry THEN #result used:= in big float #carry;
carry := ( result +:= carrydigit a );
MOID( carry +:= ( result +:= (digit ab) ) );
ELSE
MOID( carry +:= ( result +:= (digit b) ) )
ELSE result := digit a;
result MOID( result +:= digit a;b )
MOID( result +:= digit b )FI
FIOD;
OD;
 
# Next: Deal with the non overlapping Most Significant digits #
FOR place FROM overlap highest+digit order BY digit order TO extreme highest DO
REF []DIGIT resultetc = (MSD a plus< MSD b[place]|a|b);
REF SIGNED result = a plus b[place];
IF digit carry THEN
result :=IF carry; OF arithmetic THEN
carry := ( result +:= (MSD in a < MSD in b|a|b)[place] )carry;
carry := ( result +:= etc[place] )
ELSE
ELSE
result := (MSD in a < MSD in b|a|b)[place]
result := etc[place]
FI
OD; FI
OD;
 
# Next: Deal with the carry #
IF digit IF carry OF arithmetic THEN
a plus b[extreme highest+digit order] := carry
FI;
 
# Finally: normalise by removing leading & trailing "zero" digits #
# out := # INITDIGITS digits normalise(a plus b)
 
FI;
out # EXIT #
FI
);</syntaxhighlight>'''File: Template.Big_float.Base.a68''' - task utility code<syntaxhighlight lang="algol68"># -*- coding: utf-8 -*- #
# out EXIT #
################################################
);</lang>'''File: Mixin_Big_float_subtraction.a68'''
# Define the basic operators and routines for #
<lang algol68>OP - = (DIGITS arg)DIGITS: (
# manipulating DIGITS outin a generalised base := arg;#
################################################
FOR digit FROM LSD arg TO MSD arg DO digit OF (digits OF out)[digit]:=-digit OF (digits OF arg)[digit] OD;
 
out
STRUCT (
BOOL balanced,
carry, # aka "carry" between digits #
INT base,
digit width,
digit places,
digit order,
USTRING repr
) arithmetic := (
FALSE, TRUE,
10, 1, 81, -1, # Default is BCD/Hex #
USTRING( # Note that the "circled" digits are negative - used in balance arithmetic #
"ⓩ","ⓨ","ⓧ","ⓦ","ⓥ","ⓤ","ⓣ","ⓢ","ⓡ","ⓠ","ⓟ","ⓞ","ⓝ","ⓜ","ⓛ","ⓚ","ⓙ","ⓘ","ⓗ","ⓖ","ⓕ","ⓔ","ⓓ","ⓒ","ⓑ","ⓐ",
"Ⓩ","Ⓨ","Ⓧ","Ⓦ","Ⓥ","Ⓤ","Ⓣ","Ⓢ","Ⓡ","Ⓠ","Ⓟ","Ⓞ","Ⓝ","Ⓜ","Ⓛ","Ⓚ","Ⓙ","Ⓘ","Ⓗ","Ⓖ","Ⓕ","Ⓔ","Ⓓ","Ⓒ","Ⓑ","Ⓐ",
"⑨","⑧","⑦","⑥","⑤","④","③","②","①", "0", "1","2","3","4","5","6","7","8","9",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"
)[@-(26*2+9)] # can print up to base 61, or balanced base 123 #
);
 
MODE DIGITS = FLEX[0]DIGIT;
OP SIGN = (DIGITS arg)INT:
IF LSD arg - MSD arg = digit order THEN 0 ELSE SIGN digit OF (digits OF arg)[MSD arg] FI;
 
# DIGIT OPerators #
OP ABS = (DIGITS arg)DIGITS:
OP INITDIGIT = (#LONG# INT i)DIGIT: (DIGIT out; digit OF out := #SHORTEN# i; out);
IF SIGN arg < 0 THEN -arg ELSE arg FI;
#OP INITDIGIT = (INT i)DIGIT: INITDIGIT LENG i;#
 
OP -/= = (DIGITSDIGIT a, b)DIGITSBOOL: digit OF a +/= digit OF -b;
OP -:= = (REF DIGITS a, DIGITS b)DIGITS: a := a + -b;</lang>
'''Start of test case:'''
 
# Define additive and multiplicative identities #
'''File: Big_float_BCD_base.a68'''
OP ZERO = (DIGITS skip)DIGITS: INITDIGITS []DIGIT(ZERO LOC DIGIT),
<lang algol68>################################################
IDENTITY = (DIGITS skip)DIGITS: INITDIGITS []DIGIT(IDENTITY LOC DIGIT);
 
OP SIGN = (DIGIT digit)INT: SIGN digit OF digit;
 
# Define OPerators for Least and Most Significant DIGIT #
OP MSD = (DIGITS t)INT: LWB t,
EXP = (DIGITS t)INT: digit order OF arithmetic * LWB t, # exponent #
LSD = (DIGITS t)INT: UPB t;
 
OP INITDIGITS = (DIGIT in)DIGITS: INITDIGITS []DIGIT(in)[@0];
 
OP INITDIGITS = ([]DIGIT digits)DIGITS: (
### normalise digits:
A) removed leading & trailing zeros
B) IF not balanced arithmetic
THEN make all digits positive and set sign bit FI
###
MODE SIGNED = DIGIT;
DIGIT zero = ZERO LOC DIGIT;
DIGIT one = IDENTITY LOC DIGIT;
DIGIT base digit = INITDIGIT base OF arithmetic;
INT base = base OF arithmetic;
INT half base = base % 2;
INT order = digit order OF arithmetic;
 
INT msd := LWB digits + int width*order, lsd := UPB digits; # XXX #
 
# create an array with some "extra" significant digits incase there is a "big" input value #
[msd:lsd]SIGNED signed; signed[LWB digits:UPB digits] := digits[@1];
FOR place FROM LWB signed TO LWB digits+order DO
signed[place] := zero
OD;
IF msd + order /= lsd THEN
 
# Trim leading zeros #
FOR place FROM msd BY -order TO lsd DO
IF SIGN signed[place] /= 0 THEN msd := place; done msd FI
OD;
msd := lsd-order;
done msd:
# Trim trailing zeros #
FOR place FROM lsd BY order TO msd DO
IF SIGN signed[place] /= 0 THEN lsd := place; done lsd FI
OD;
lsd := msd+order;
done lsd:
IF msd + order /= lsd THEN # not zero #
IF carry OF arithmetic THEN # Normalise to the "base OF arithmetic": #
INT sign msd := SIGN digits[msd]; # first non zero digit #
INT lwb digit := (balanced OF arithmetic|-half base|:sign msd < 0 |1-base|0);
INT upb digit := (balanced OF arithmetic| half base|:sign msd > 0 |base-1|0);
SIGNED carry := zero;
FOR place FROM lsd BY order WHILE SIGN carry /=0 OR place >= LWB digits DO
SIGNED digit := signed[place];
carry := digit +:= carry;
WHILE digit OF digit < lwb digit DO
MOID(digit +:= base digit);
MOID(carry -:= one)
OD;
WHILE digit OF digit > upb digit DO
MOID(digit -:= base digit);
MOID(carry +:= one)
OD;
signed[place] := digit; # normalised #
IF SIGN digit /= 0 THEN msd := place FI
OD
FI
FI;
signed[msd:lsd][@msd]
FI
);
 
# re anchor the array with a shift #
OP SHL = (DIGITS in, INT shl)DIGITS: in[@MSD in-shl];
OP SHR = (DIGITS in, INT shr)DIGITS: in[@MSD in+shr];</syntaxhighlight>'''File: Template.Big_float_BCD.Base.a68 - test case code'''<syntaxhighlight lang="algol68">################################################
# Define the basic operators and routines for #
# manipulating DIGITS specific to a BCD base #
################################################
 
####################################################
# BCD noramlly means Binary Coded Decimal, but... #
# this code handles "Balanced Coded Data", meaning #
# Data can be in any numerical bases, and the data #
# can optionally be stored as Balanced about zero #
####################################################
 
# define the basic axioms of the number system you are extending #
MODE DIGIT = STRUCT(#LONG# INT digit);
# Note: If the +:= and *:= operators for INT are being "overloaded",
DIGIT digit base = #LONG# 10 ** digit width;
then it is sometimes necessary to wrap an INT in a STRUCT to
protect the builtin definitions of the INT OPerators
#
 
# mixin the Big_float base definitions #
PR READ "Mixin_Big_float_baseTemplate.Big_float.Base.a68" PR
 
MODE BIGREAL = DIGITS;
BOOL digit carry = TRUE;
# Most Significant Digit of the left = -1 #
 
# the Yoneda ambiguity forces the peculiar coercion n the body of OP #
OP ZERO = (DIGIT skip)DIGIT: 0;
OP ONEZERO = (DIGIT skip)DIGIT: 1(DIGIT out; digit OF out := 0; out);
OP IDENTITY = (DIGIT skip)DIGIT: (DIGIT out; digit OF out := 1; out);
 
# define the basis operators #
OP ABS = (DIGIT a)INT: ABS digit OF a;
OP - = (DIGIT a)DIGIT: INITDIGIT -digit OF a;
 
####################################################################
# Important: Operator +:= is required by Template_Big_float_addition. #
####################################################################
# Note: +:= returns carry DIGIT #
OP +:= = (REF DIGIT lhs, DIGIT arg)DIGIT: (
# Todo: Implement balanced arithmetic #
INT sum = digit OF lhs + digit OF arg; # arg may be -ve #
INT carry := sum % base OF arithmetic;
INT digit := sum - carry * base OF arithmetic;
IF balanced OF arithmetic THEN
INT half base = base OF arithmetic OVER 2;
IF digit > half base THEN
digit -:= base OF arithmetic;
carry +:= 1
ELIF digit < -half base THEN
digit +:= base OF arithmetic;
carry -:= 1
FI
FI;
lhs := INITDIGIT digit; INITDIGIT carry
);
 
INT half = base OF arithmetic OVER 2;
# ASSERT NOT balanced OF arithmetic OR ODD base OF arithmetic #
##########################################################################
# Important: Operator *:= is required by Template_Big_float_multiplication. #
##########################################################################
# Note: *:= returns carry DIGIT #
 
OP *:= = (REF DIGIT lhs, DIGIT arg)DIGIT: (
# Todo: Implement balanced arithmetic #
INT product = digit OF lhs * digit OF arg; # arg may be -ve #
INT carry = product % base OF arithmetic;
lhs := INITDIGIT(product - carry * base OF arithmetic);
INITDIGIT carry
);
 
##########################################################
Line 231 ⟶ 319:
##########################################################
OP INITLONGREAL = (BIGREAL a)LONG REAL:
IF ABS(MSDSIGN a - LSD a) <= 0 THEN 0
ELSE
INT lsd a = LSD a; # Todo: Optimise/reduce to match "long real width" #
LONG REAL out := digit OF (digits OF a)[MSD a];
FORLONG placeREAL FROMout MSD a -:= digit orderOF BY -digit order TO LSDa[MSD a DO];
FOR place outFROM :=MSD outa *- digit baseorder +OF arithmetic BY -digit order OF (digitsarithmetic OFTO lsd a)[place] DO
out := out * base OF arithmetic + digit OF a[place]
OD;
out * LONG REAL(digit base OF arithmetic) ** -LSD a
FI;
 
OP INITREAL = (BIGREAL r)REAL:
CO
SHORTEN INITLONGREAL r;
OP INITBIGREAL = (INT in int)BIGREAL:
INITBIGREAL #LENG# in int;
END CO
 
OP INITBIGREALMSD = (#LONG# INT in inti)BIGREALINT: (
LONG INT remainder := i; INT count := 0;
WHILE remainder /= 0 DO
remainder %:= base OF arithmetic;
MOID(count +:= 1)
OD;
count
);
 
OP PROC num digitsINITBIGREAL = (#LONG# INT iin int)INTBIGREAL: (
INT remainder := i, count := 0;
WHILE remainder /= 0 DO
remainder %:= digit base;
MOID(count +:= 1)
OD;
count
);
 
DIGIT zero = 0;
 
INT max = numMSD digits(in int);
[1-max:0]STRUCTDIGITDIGIT out;
BIGREAL bigreal;
 
#LONG# INT int := ABS in int;
INT sign = SIGN in int;
 
FOR iplace FROM UPB out BY digit order OF arithmetic TO LWB out WHILE int /= 0 DO
#LONG# INT digit := SHORTEN (int MOD digit base OF arithmetic);
int := (int-digit) OVER digit base OF arithmetic;
(digit OF out)[iplace] := sign * digit;
IF int = 0 THEN done FI
OD;
done:
INITDIGITS out # normalise #
digits OF bigreal := out;
bigreal
);
 
OP INITBIGREAL = (REALINT in realint)BIGREAL:
INITBIGREAL LENG in realint;
 
OP INITBIGREAL = (LONG REAL in real)BIGREAL: (
SKIP; # TODO #
 
INT sign = SIGN in real;
OP INITBIGREAL = ([]DIGIT digits)BIGREAL: (
LONG REAL real := ABS in real;
# Note: assumes digits have been normalised ! #
LONG REAL frac := real - ENTIER real;
STRUCT(FLEX[LWB digits:UPB digits]STRUCTDIGIT digits)out;
BIGREAL whole = INITBIGREAL ENTIER real; # normalised #
digit OF digits OF out := digits;
out
);
 
INT base = base OF arithmetic,
####################################################################
order = digit order OF arithmetic,
# Important: Operator +:= is required by Mixin_Big_float_addition. #
lsd = digit places OF arithmetic; # Todo: can be optimised/reduced #
####################################################################
# Note: +:= returns carry #
[MSD whole:lsd]DIGIT out; out[MSD whole:LSD whole] := whole[@1];
OP +:= = (REF DIGIT lhs, DIGIT arg)DIGIT: (
FOR place FROM LSD whole - order TO 0 DO out[place] := INITDIGIT 0 OD; # pad #
DIGIT out := lhs + arg;
lhs := ABS out %* digit base * SIGN out;
(out-lhs) % digit base * SIGN out
);
 
FOR place FROM -order BY -order TO lsd DO
##########################################################################
frac *:= base;
# Important: Operator *:= is required by Mixin_Big_float_multiplication. #
#LONG# INT digit := SHORTEN ENTIER frac;
##########################################################################
frac -:= digit;
# Note: *:= returns carry #
(digit OF out)[place] := digit;
OP *:= = (REF DIGIT lhs, DIGIT arg)DIGIT: (
DIGIT out IF frac = lhs0 THEN *done arg;FI
OD;
lhs := ABS out %* digit base * SIGN out;
done:
(out-lhs) % digit base * SIGN out
IF sign > 1 THEN INITDIGITS out ELSE - INITDIGITS out FI
);
 
OP INITBIGREAL = (REAL in real)BIGREAL:
FORMAT big digit fmt = $n(digit width)(d)$;
INITBIGREAL LENG in real;
FORMAT big real fmt = $g(-digit width)","$;
 
#FORMAT digit fmt = $n(digit width OF arithmetic+ABS balanced OF arithmetic)(d)$;#
OP REPR = (STRUCTDIGIT digit)STRING:
FORMAT big real fmt = $g((digit width OF arithmetic+ABS balanced OF arithmetic))","$;
whole(ABS digit OF digit,-digit width);
 
OP REPR = (BIGREALDIGIT big realdigit)STRING:(
IF LWB repr OF arithmetic <= digit OF digit AND digit OF digit <= UPB repr OF arithmetic THEN
(repr OF arithmetic)[digit OF digit]
ELIF balanced OF arithmetic THEN
whole(digit OF digit,digit width OF arithmetic+1)
ELSE
whole(digit OF digit,-digit width OF arithmetic)
FI;
 
OP REPR = (BIGREAL real)STRING:(
CHAR repr 0 = "0";
STRING out;
FOR place FROM MSD big real BY -digit order OF arithmetic TO LSD big real DO
outIF place +:= REPR1 (digitsAND OFplace big= MSD real)[place] THEN out +:= "." FI;
IF placeout +:= 0 AND REPR(real[place /= LSD big real OR]);
IF place = 10 AND place /= MSD bigLSD real THEN out +:= "." FI
OD;
IF out = "" THEN out := "repr 0" FI;
IF SIGN big real < 0 AND NOT balanced OF arithmetic THEN "-" +=: out FI;
IF MSD big real > 1 AND LSD big real > 1 OR
MSD big real < 0 AND LSD big real < 0 THEN
# No decimal point yet, so maybe we need to add an exponent #
out+"e"+whole(IF digit order OF arithmetic*LSD big real*digit width,= 1 THEN repr 0)
ELSE "e"+REPR INITBIGREAL(digit order OF arithmetic*LSD real) FI
# ELSE "E"+whole(digit order OF arithmetic*LSD real,0) FI #
ELSE
out
FI
);</syntaxhighlight>'''File: Template.Big_float.Subtraction.a68''' - bonus subtraction definitions<syntaxhighlight lang="algol68">OP - = (DIGITS arg)DIGITS: (
);</lang>'''File: test_Big_float_BCD_addition.a68'''
DIGITS out := arg;
<lang algol68>#!/usr/local/bin/a68g --script #
FOR digit FROM LSD arg BY digit order OF arithmetic TO MSD arg DO
out[digit]:= -out[digit]
OD;
out
);
 
OP SIGN = (DIGITS arg)INT:
IF LSD arg - MSD arg = digit order OF arithmetic THEN 0 # empty array #
ELSE # balanced artihmetic # SIGN arg[MSD arg] FI;
 
OP ABS = (DIGITS arg)DIGITS:
IF SIGN arg < 0 THEN -arg ELSE arg FI;
 
# derived DIGIT operators #
OP - = (DIGIT a, b)DIGIT: a + -b;
OP -:= = (REF DIGIT a, DIGIT b)DIGIT: a := a + -b;
 
# derived DIGITS operators #
OP - = (DIGITS a, b)DIGITS: a + -b;
OP -:= = (REF DIGITS a, DIGITS b)DIGITS: a := a + -b;
 
# derived hybrid DIGIT and DIGITS operators #
OP - = (DIGITS a, DIGIT b)DIGITS: a - INITDIGITS b;
OP - = (DIGIT a, DIGITS b)DIGITS: INITDIGITS a - a;
OP -:= = (REF DIGITS lhs, DIGIT arg)DIGITS: lhs := lhs - arg;</syntaxhighlight>'''File: test.Big_float_BCD.Addition.a68''' - test case code main program<syntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script #
 
##################################################################
# TEST CASE #
# A program to test abritary length BCD floating point addition. #
##################################################################
 
PR READ "prelude/general.a68" PR # [[rc:Template:ALGOL 68/prelude]] #
 
##################################################################
INT digit width := 1; # define how decimal digits each "big" DIGIT is #
# READ Template for doing the actual arbitary precsion addition. #
##################################################################
PR READ "Template.Big_float.Addition.a68" PR
 
# include the basic axioms of the digits being used #
PR READ "Big_float_BCD_baseTemplate.Big_float_BCD.Base.a68" PR
PR READ "Mixin_Big_float_additionTemplate.Big_float.Subtraction.a68" PR
PR READ "Mixin_Big_float_subtraction.a68" PR # need SIGN #
 
test: (
Line 354 ⟶ 471:
sum := INITBIGREAL 0,
shifted pattern := pattern,
shifted tiny := INITBIGREAL 1; # typically 0.000.....00001 etc #
 
FOR term FROM -8 TO 20 DO
 
# First make shifted pattern smaller by shifting right by the pattern width #
digits OF shifted pattern := (digits OF shifted pattern)[@term*pattern width+2];
digits OF shifted tiny := (digits OF shifted tiny)[@(term+1)*pattern width];
 
MOID(sum +:= shifted pattern);
 
Line 372 ⟶ 489:
BIGREAL total = prod + shifted tiny;
 
IF term < -4 THEN
print(( REPR sum," x 81 gives: ", REPR prod, ", Plus ",REPR shifted tiny," gives: "))
ELSE
print((LSD prod - MSD prod + 1," digit test result: "))
Line 379 ⟶ 496:
printf(($g$, REPR total, $" => "b("Passed","Failed")"!"$, LSD total = MSD total, $l$))
OD
)</syntaxhighlight>'''Output:'''
)</lang>
'''Output:'''
<pre style="height:15ex;overflow:scroll">
12345679e63 x 81 gives: 999999999e63, Plus 1e63 gives: 1e72 => Passed!
Line 413 ⟶ 529:
</pre>
[[Category:Arbitrary precision]]
 
=={{header|Go}}==
Although the big.Float type already has a 'Mul' method, we re-implement it by repeated application of the 'Add' method.
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"math/big"
)
 
func repeatedAdd(bf *big.Float, times int) *big.Float {
if times < 2 {
return bf
}
var sum big.Float
for i := 0; i < times; i++ {
sum.Add(&sum, bf)
}
return &sum
}
 
func main() {
s := "12345679"
t := "123456790"
e := 63
var bf, extra big.Float
for n := -7; n <= 21; n++ {
bf.SetString(fmt.Sprintf("%se%d", s, e))
extra.SetString(fmt.Sprintf("1e%d", e))
bf = *repeatedAdd(&bf, 81)
bf.Add(&bf, &extra)
fmt.Printf("%2d : %s\n", n, bf.String())
s = t + s
e -= 9
}
}</syntaxhighlight>
 
{{out}}
<pre>
-7 : 1e+72
-6 : 1e+72
-5 : 1e+72
-4 : 1e+72
-3 : 1e+72
-2 : 1e+72
-1 : 1e+72
0 : 1e+72
1 : 1e+72
2 : 1e+72
3 : 1e+72
4 : 1e+72
5 : 1e+72
6 : 1e+72
7 : 1e+72
8 : 1e+72
9 : 1e+72
10 : 1e+72
11 : 1e+72
12 : 1e+72
13 : 1e+72
14 : 1e+72
15 : 1e+72
16 : 1e+72
17 : 1e+72
18 : 1e+72
19 : 1e+72
20 : 1e+72
21 : 1e+72
</pre>
 
=={{header|J}}==
Line 422 ⟶ 607:
Given
 
<langsyntaxhighlight lang="j">e=: 2 : 0
u * 10x ^ v
)</langsyntaxhighlight>
 
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:
 
<syntaxhighlight lang="text"> 1 e 63 + 12345679 e 63 * 81
1000000000000000000000000000000000000000000000000000000000000000000000000
1 e 54 + 12345679012345679 e 54 * 81
Line 435 ⟶ 620:
1000000000000000000000000000000000000000000000000000000000000000000000000
1 e 36 + 12345679012345679012345679012345679x e 36 * 81
1000000000000000000000000000000000000000000000000000000000000000000000000</langsyntaxhighlight>
 
As usual, if the results seem mysterious, it can be helpful to examine intermediate results. For example:
 
<syntaxhighlight lang="text"> 1 e 36 ,: 12345679012345679012345679012345679x e 36 * 81
1000000000000000000000000000000000000
999999999999999999999999999999999999000000000000000000000000000000000000</syntaxhighlight>
 
So, ok, let's turn this into a sequence:
 
<langsyntaxhighlight lang="j">factor=: [: +/ [: 12345679 e ] _9 * 1 + i.&.(+&8)
adjust=: 1 e (_9&*)</langsyntaxhighlight>
 
Here we show some examples of what these words mean:
 
<langsyntaxhighlight lang="j"> factor _4 NB. this is the number we multiply by 81
12345679012345679012345679012345679000000000000000000000000000000000000
factor _3
Line 452 ⟶ 643:
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 thatthe result of multiplying our factor by 81
1000000000000000000000000000000000000
adjust _3
1000000000000000000000000000</langsyntaxhighlight>
 
Given these words:
 
<syntaxhighlight lang="j">
<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
Line 465 ⟶ 656:
29
~.(adju + 81 * factor)&> _7+i.29 NB. here we see that they are all the same number
1000000000000000000000000000000000000000000000000000000000000000000000000</langsyntaxhighlight>
 
Note that <code>~. list</code> returns the unique elements from that list.
 
=={{header|Java}}==
 
Java provides a <code>BigDecimal</code> class that supports robust arbitrary precision calculations. That class is demonstrated using the computations required in this task.
 
<syntaxhighlight lang="java">
import java.math.BigDecimal;
 
public class GeneralisedFloatingPointAddition {
 
public static void main(String[] args) {
BigDecimal oneExp72 = new BigDecimal("1E72");
for ( int i = 0 ; i <= 21+7 ; i++ ) {
String s = "12345679";
for ( int j = 0 ; j < i ; j++ ) {
s += "012345679";
}
int exp = 63 - 9*i;
s += "E" + exp;
BigDecimal num = new BigDecimal(s).multiply(BigDecimal.valueOf(81)).add(new BigDecimal("1E" + exp));
System.out.printf("Test value (%s) equals computed value: %b. Computed = %s%n", oneExp72, num.compareTo(oneExp72) == 0 , num);
}
}
 
}
</syntaxhighlight>
 
{{out}}
<pre>
Test value (1E+72) equals computed value: true. Computed = 1.000000000E+72
Test value (1E+72) equals computed value: true. Computed = 1.000000000000000000E+72
Test value (1E+72) equals computed value: true. Computed = 1.000000000000000000000000000E+72
Test value (1E+72) equals computed value: true. Computed = 1.000000000000000000000000000000000000E+72
Test value (1E+72) equals computed value: true. Computed = 1.000000000000000000000000000000000000000000000E+72
Test value (1E+72) equals computed value: true. Computed = 1.000000000000000000000000000000000000000000000000000000E+72
Test value (1E+72) equals computed value: true. Computed = 1.000000000000000000000000000000000000000000000000000000000000000E+72
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Test value (1E+72) equals computed value: true. Computed = 1000000000000000000000000000000000000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
</pre>
 
=={{header|Julia}}==
Julia implements arbitary precision intergers and floating point numbers in its base function.
<syntaxhighlight lang="julia">
@assert(big"12345679e63" * BigFloat(81) + big"1e63" == big"1.0e+72")
@assert(big"12345679012345679e54" * BigFloat(81) + big"1e54" == big"1.0e+72")
@assert(big"12345679012345679012345679e45" * BigFloat(81) + big"1e45" == big"1.0e+72")
@assert(big"12345679012345679012345679012345679e36" * BigFloat(81) + big"1e36" == big"1.0e+72")
</syntaxhighlight>
All assertions pass.
 
=={{header|Kotlin}}==
The JDK has an arbitrary precision BigDecimal class which uses a scaled decimal representation internally. This enables decimal numbers of any size to be represented exactly.
 
Although the BigDecimal type supports multiplication, I've used repeated addition here to be more in tune with the spirit of the task.
<syntaxhighlight lang="scala">// version 1.2.10
 
import java.math.BigDecimal
 
fun BigDecimal.repeatedAdd(times: Int): BigDecimal {
var sum = BigDecimal.ZERO
for (i in 0 until times) sum += this
return sum
}
 
fun main(args: Array<String>) {
var s = "12345679"
val t = "123456790"
var e = 63
for (n in -7..21) {
val bd = BigDecimal("${s}e$e")
val oneE = BigDecimal("1e$e")
val temp = bd.repeatedAdd(9)
val result = temp.repeatedAdd(9) + oneE
println("%2d : %e".format(n, result))
s = t + s
e -= 9
}
}</syntaxhighlight>
 
{{out}}
<pre>
-7 : 1.000000e+72
-6 : 1.000000e+72
-5 : 1.000000e+72
-4 : 1.000000e+72
-3 : 1.000000e+72
-2 : 1.000000e+72
-1 : 1.000000e+72
0 : 1.000000e+72
1 : 1.000000e+72
2 : 1.000000e+72
3 : 1.000000e+72
4 : 1.000000e+72
5 : 1.000000e+72
6 : 1.000000e+72
7 : 1.000000e+72
8 : 1.000000e+72
9 : 1.000000e+72
10 : 1.000000e+72
11 : 1.000000e+72
12 : 1.000000e+72
13 : 1.000000e+72
14 : 1.000000e+72
15 : 1.000000e+72
16 : 1.000000e+72
17 : 1.000000e+72
18 : 1.000000e+72
19 : 1.000000e+72
20 : 1.000000e+72
21 : 1.000000e+72
</pre>
 
=={{header|Perl}}==
Calculations done in decimal, prints 'Y' for each successful test.
<syntaxhighlight lang="perl">use strict;
use warnings;
use Math::Decimal qw(dec_add dec_mul_pow10);
 
my $e = 63;
for my $n (-7..21) {
my $num = '12345679' . '012345679' x ($n+7);
my $sum = dec_mul_pow10(1, $e);
$sum = dec_add($sum, dec_mul_pow10($num,$e)) for 1..81;
printf "$n:%s ", 10**72 == $sum ? 'Y' : 'N';
$e -= 9;
}</syntaxhighlight>
{{out}}
<pre>-7:Y -6:Y -5:Y -4:Y -3:Y -2:Y -1:Y 0:Y 1:Y 2:Y 3:Y 4:Y 5:Y 6:Y 7:Y 8:Y 9:Y 10:Y 11:Y 12:Y 13:Y 14:Y 15:Y 16:Y 17:Y 18:Y 19:Y 20:Y 21:Y
</pre>
 
=={{header|Phix}}==
===bigatom===
Note this is decimal-only, and that mpfr.e does not really cope because it cannot hold decimal fractions exactly (though it could probably be fudged with a smidgen of rounding)
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">include</span> <span style="color: #000000;">bigatom</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_scale</span><span style="color: #0000FF;">(</span><span style="color: #000000;">200</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"12345679"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"123456790"</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">63</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">7</span> <span style="color: #008080;">to</span> <span style="color: #000000;">21</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">bigatom</span> <span style="color: #000000;">bd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_new</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%se%d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">})),</span>
<span style="color: #000000;">e1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_new</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1e%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ba_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bd</span><span style="color: #0000FF;">,</span><span style="color: #000000;">81</span><span style="color: #0000FF;">),</span><span style="color: #000000;">e1</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- res = ba_mul(bd,81)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ba_sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%.eB"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)})</span>
<span style="color: #000080;font-style:italic;">-- printf(1,"%2d : %s\n",{n,ba_sprintf("%B",res)})</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">s</span>
<span style="color: #000000;">e</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">9</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out|Output (trimmed)}}
(Use the %B format to better see the difference between adding and not adding e1.)
<pre>
-7 : 1e72
...
21 : 1e72
</pre>
 
=== any base ===
Uses b_add() and b_mul() from [[Generalised_floating_point_multiplication#Phix]]
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Generic_addition.exw
-- =================================
--
-- https://rosettacode.org/wiki/Generalised_floating_point_addition#Phix
--
-- Uses b_add() and b_mul() from Generic_multiplication</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">trace</span>
<span style="color: #000080;font-style:italic;">--&lt;next 210 lines copied from Generic_multiplication.exw&gt;</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">decimal</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0123456789"</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">negate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- negate b (can be balanced or unbalanced)
--</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">alphabet</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- traditional: add/remove a leading '-'
-- eg "-123" &lt;==&gt; "123"</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">"0"</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"-"</span><span style="color: #0000FF;">&</span><span style="color: #000000;">b</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">else</span>
<span style="color: #000080;font-style:italic;">-- balanced: mirror [non-0] digits
-- eg "-0+" (ie -8) &lt;==&gt; "+0-" (ie +8)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">'.'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">alphabet</span><span style="color: #0000FF;">[-</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">b</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">b_trim</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- (common code)
-- trim trailing ".000"</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim_tail</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">),</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000080;font-style:italic;">-- trim leading zeroes, but not "0.nnn" -&gt; ".nnn"
-- [hence we cannot use the standard trim_head()]</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)></span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">'.'</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"0"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">b</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">b_sub</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">b_add</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">base</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">zdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">carry</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">da</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">db</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">digit</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">zdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- (let me know if you can fix this for me!)
-- if a[1]='-' or b[1]='-' then ?9/0 end if -- +ve only</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (-a)+b == b-a</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">b_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">negate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- a+(-b) == a-b</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">b_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">negate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">adt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">bdt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">adt</span> <span style="color: #008080;">or</span> <span style="color: #000000;">bdt</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- remove the '.'s and zero-pad the shorter as needed
-- (thereafter treat them as two whole integers)
-- eg "1.23"+"4.5" -&gt; "123"+"450" (leaving adt==2)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">adt</span> <span style="color: #008080;">then</span> <span style="color: #000000;">adt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bdt</span> <span style="color: #008080;">then</span> <span style="color: #000000;">bdt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">bdt</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">bdt</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">bdt</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bdt</span><span style="color: #0000FF;">></span><span style="color: #000000;">adt</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bdt</span><span style="color: #0000FF;">-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">adt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">bdt</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">adt</span><span style="color: #0000FF;">></span><span style="color: #000000;">bdt</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">-</span><span style="color: #000000;">bdt</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)<</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- ensure b is the shorter</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">da</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;"><-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)?</span><span style="color: #000000;">0</span><span style="color: #0000FF;">:</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">zdx</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">db</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;"><-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)?</span><span style="color: #000000;">0</span><span style="color: #0000FF;">:</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">zdx</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">digit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">da</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">db</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">carry</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">zdx</span>
<span style="color: #000000;">carry</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">></span><span style="color: #000000;">base</span><span style="color: #0000FF;">?+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digit</span><span style="color: #0000FF;"><</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">0</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">alphabet</span><span style="color: #0000FF;">[</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">-</span><span style="color: #000000;">carry</span><span style="color: #0000FF;">*</span><span style="color: #000000;">base</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">carry</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">carry</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">alphabet</span><span style="color: #0000FF;">[</span><span style="color: #000000;">carry</span><span style="color: #0000FF;">+</span><span style="color: #000000;">zdx</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">a</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">adt</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">a</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"."</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b_trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">a_smaller</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- return true if a is smaller than b
-- if not balanced then both are +ve</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)!=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- sanity check</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">da</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">db</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">c</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">false</span> <span style="color: #000080;font-style:italic;">-- (=, which is not &lt;)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">b_sub</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">base</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">zdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">carry</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">da</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">db</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">digit</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">zdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (-a)-b == -(a+b)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">negate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">negate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- a-(-b) == a+b</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">b_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">negate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">adt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">bdt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">adt</span> <span style="color: #008080;">or</span> <span style="color: #000000;">bdt</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- remove the '.'s and zero-pad the shorter as needed
-- (thereafter treat them as two whole integers)
-- eg "1.23"+"4.5" -&gt; "123"+"450" (leaving adt==2)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">adt</span> <span style="color: #008080;">then</span> <span style="color: #000000;">adt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bdt</span> <span style="color: #008080;">then</span> <span style="color: #000000;">bdt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">bdt</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">bdt</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">bdt</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bdt</span><span style="color: #0000FF;">></span><span style="color: #000000;">adt</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bdt</span><span style="color: #0000FF;">-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">adt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">bdt</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">adt</span><span style="color: #0000FF;">></span><span style="color: #000000;">bdt</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">-</span><span style="color: #000000;">bdt</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">bNegate</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)<</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">a_smaller</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">bNegate</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- ensure b is the shorter/smaller</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">da</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;"><-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)?</span><span style="color: #000000;">0</span><span style="color: #0000FF;">:</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">zdx</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">db</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;"><-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)?</span><span style="color: #000000;">0</span><span style="color: #0000FF;">:</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">zdx</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">digit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">da</span> <span style="color: #0000FF;">-</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">db</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">carry</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">zdx</span>
<span style="color: #000000;">carry</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">0</span>
<span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">alphabet</span><span style="color: #0000FF;">[</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">+</span><span style="color: #000000;">carry</span><span style="color: #0000FF;">*</span><span style="color: #000000;">base</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">carry</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">carry</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- should have set bNegate above...</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">adt</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">a</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">adt</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"."</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b_trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bNegate</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">negate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">b_mul</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">base</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">zdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">dpa</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">dpb</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">ndp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">dpa</span> <span style="color: #008080;">then</span> <span style="color: #000000;">ndp</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">dpa</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dpa</span><span style="color: #0000FF;">..</span><span style="color: #000000;">dpa</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">dpb</span> <span style="color: #008080;">then</span> <span style="color: #000000;">ndp</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">dpb</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dpb</span><span style="color: #0000FF;">..</span><span style="color: #000000;">dpb</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">pos</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0"</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">zdx</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- balanced number systems</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">neg</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">negate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pos</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">zdx</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">m</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #000000;">neg</span><span style="color: #0000FF;">:</span><span style="color: #000000;">pos</span><span style="color: #0000FF;">),</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">m</span> <span style="color: #0000FF;">+=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">pos</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'0'</span>
<span style="color: #000000;">neg</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'0'</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">else</span>
<span style="color: #000080;font-style:italic;">-- non-balanced (normal) number systems</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">negative</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">then</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$];</span> <span style="color: #000000;">negative</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">then</span> <span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$];</span> <span style="color: #000000;">negative</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">not</span> <span style="color: #000000;">negative</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">zdx</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pos</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">m</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">pos</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'0'</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">negative</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">negate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">alphabet</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ndp</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">ndp</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">ndp</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"."</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b_trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #000080;font-style:italic;">--&lt;/copied from Generic_multiplication.exw&gt;</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">normalise_decimal</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- eg "12.34e-3" ==&gt; "0.01234"
-- and "1e2" ==&gt; "100"
--</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'e'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">e</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">z</span><span style="color: #0000FF;">}}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">e</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$],</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">e</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">d</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- eg 12.34e-3 == 1234e-5</span>
<span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">..</span><span style="color: #000000;">d</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #000000;">z</span> <span style="color: #0000FF;">-=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- eg 1e2 ==&gt; 100</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">z</span><span style="color: #0000FF;"><-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- eg 1234e-5 ==&gt; 0.01234</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0."</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">z</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">))&</span><span style="color: #000000;">a</span>
<span style="color: #008080;">else</span>
<span style="color: #000080;font-style:italic;">-- eg 1234e-3 ==&gt; 1.234</span>
<span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">z</span><span style="color: #0000FF;">..</span><span style="color: #000000;">z</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"."</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">e_notation</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- eg "1000000" ==&gt; "1e6"</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (to do)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #008000;">".0"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">-=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">&=</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"e%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"12345679"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"123456790"</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">63</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">7</span> <span style="color: #008080;">to</span> <span style="color: #000000;">21</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">bd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">normalise_decimal</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%se%d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">})),</span>
<span style="color: #000000;">e1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">normalise_decimal</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1e%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bd</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"81"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">decimal</span><span style="color: #0000FF;">),</span><span style="color: #000000;">e1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">decimal</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d res : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e_notation</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)})</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">s</span>
<span style="color: #000000;">e</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">9</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
same ouput
 
=={{header|Racket}}==
 
Racket has native arbitrary precision numbers (if in doubt, prefixed with <code>#e...</code>)
 
The printed result is the exact difference of 1e72 and the result. So those <code>0</code>s you see are exactly nothing.
 
<syntaxhighlight lang="racket">#lang racket
(define (f n (printf printf))
(define exponent (* (- n) 9))
(define mantissa
(for/fold ((m 0))
((i (in-range -7 (add1 n))))
(+ (* m 1000000000) 12345679)))
 
(let ((rv (+ (expt 10 exponent) (* mantissa (expt 10 exponent) 81))))
(printf "~ae~a * 81 + 1e~a - 1e72 = ~a~%"
(~.a mantissa
#:max-width 20
#:limit-marker (format "..[~a].." (add1 (order-of-magnitude mantissa))))
exponent
exponent
(- #e1e72 rv))
rv))
 
(module+ test
(require rackunit)
(check-equal? (f -7 void) (expt 10 72))
(check-equal? (f -6 void) (expt 10 72)))
 
(module+ main
(displayln "number in brackets is TOTAL number of digits")
(for ((i (in-range -7 (add1 21)))) (f i)))</syntaxhighlight>
 
{{out}}
 
<pre>number in brackets is TOTAL number of digits
12345679e63 * 81 + 1e63 - 1e72 = 0
12345679012345679e54 * 81 + 1e54 - 1e72 = 0
123456790123..[26]..e45 * 81 + 1e45 - 1e72 = 0
123456790123..[35]..e36 * 81 + 1e36 - 1e72 = 0
123456790123..[44]..e27 * 81 + 1e27 - 1e72 = 0
123456790123..[53]..e18 * 81 + 1e18 - 1e72 = 0
123456790123..[62]..e9 * 81 + 1e9 - 1e72 = 0
123456790123..[71]..e0 * 81 + 1e0 - 1e72 = 0
123456790123..[80]..e-9 * 81 + 1e-9 - 1e72 = 0
123456790123..[89]..e-18 * 81 + 1e-18 - 1e72 = 0
123456790123..[98]..e-27 * 81 + 1e-27 - 1e72 = 0
12345679012..[107]..e-36 * 81 + 1e-36 - 1e72 = 0
12345679012..[116]..e-45 * 81 + 1e-45 - 1e72 = 0
12345679012..[125]..e-54 * 81 + 1e-54 - 1e72 = 0
12345679012..[134]..e-63 * 81 + 1e-63 - 1e72 = 0
12345679012..[143]..e-72 * 81 + 1e-72 - 1e72 = 0
12345679012..[152]..e-81 * 81 + 1e-81 - 1e72 = 0
12345679012..[161]..e-90 * 81 + 1e-90 - 1e72 = 0
12345679012..[170]..e-99 * 81 + 1e-99 - 1e72 = 0
12345679012..[179]..e-108 * 81 + 1e-108 - 1e72 = 0
12345679012..[188]..e-117 * 81 + 1e-117 - 1e72 = 0
12345679012..[197]..e-126 * 81 + 1e-126 - 1e72 = 0
12345679012..[206]..e-135 * 81 + 1e-135 - 1e72 = 0
12345679012..[215]..e-144 * 81 + 1e-144 - 1e72 = 0
12345679012..[224]..e-153 * 81 + 1e-153 - 1e72 = 0
12345679012..[233]..e-162 * 81 + 1e-162 - 1e72 = 0
12345679012..[242]..e-171 * 81 + 1e-171 - 1e72 = 0
12345679012..[251]..e-180 * 81 + 1e-180 - 1e72 = 0
12345679012..[260]..e-189 * 81 + 1e-189 - 1e72 = 0
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
As long as all values are kept as rationals (type <code>Rat</code>) calculations are done full precision.
<syntaxhighlight lang="raku" line>my $e = 63;
for -7..21 -> $n {
my $num = '12345679' ~ '012345679' x ($n+7);
my $sum = $_ + ($num * $_) * 81 given $e > -20 ?? 10**$e !! Rat.new(1,10**abs $e);
printf "$n:%s ", 10**72 == $sum ?? 'Y' !! 'N';
$e -= 9;
}</syntaxhighlight>
{{out}}
<pre>-7:Y -6:Y -5:Y -4:Y -3:Y -2:Y -1:Y 0:Y 1:Y 2:Y 3:Y 4:Y 5:Y 6:Y 7:Y 8:Y 9:Y 10:Y 11:Y 12:Y 13:Y 14:Y 15:Y 16:Y 17:Y 18:Y 19:Y 20:Y 21:Y
</pre>
 
=={{header|REXX}}==
===base ten only===
┌────────────────────────────────────────────────────────────────────┐
┌─┘ This REXX program uses an uncompressed (or zoned) BCD which └─┐
│ consumes one byte for each represented digit. A leading sign (+ or -) │
│ is optional. An exponent is also allowed which is preceded by a ^. │
│ The value of the exponent may have a leading sign (+ or -). │
│ Each numeral (digit) is stored as its own character (glyph), as well │
│ as the signs and exponent indicator. There is essentially no limit on │
│ the number of digits in the mantissa or the exponent, but the value of │
│ the exponent is limited to around 16 million. The mantissa may also │
│ have a decimal point (.). │
│ │
│ Method: a table of twenty-eight BCD numbers is built, and a test case │
│ of adding that BCD number 81 times (essentially multiplying by 81), │
│ and then a number is added to that sum, and the resultant sum should │
│ result in the final sum of 1e72 (for all cases). │
│ │
└─┐ The number of digits for the precision is automatically adjusted. ┌─┘
└────────────────────────────────────────────────────────────────────┘
<syntaxhighlight lang="rexx">/*REXX program performs generalized floating point addition using BCD numbers. */
maxW= linesize() - 1 /*maximum width allowed for displays. */
/*Not all REXXes have the LINESIZE BIF.*/
_123= 012345679; reps= 0; mult= 63 /*vars used to construct test cases. */
say ' # addend uncompressed (zoned) BCD number' /*display the header*/
say left('── ────── ─', maxW, '─') /* " header sep*/
 
do j=-7 to 21 /*traipse through the test cases. */
reps= reps + 1 /*increase number of repetitions. */
BCD.j= strip(copies(_123, reps)'^'mult,'L',0) /*construct a zoned BCD. */
if j//3==0 then BCD.J= '+'BCD.j /*add a leading plus sign every 3rd #. */
parse var BCD.j '^' pow /*get the exponent part of the number. */
addend.j= '1e'pow /*build exponent addend the hard way. */
_=right(j, 2) right(addend.j, 6) /*construct the prefix for a line. */
aLine= _ BCD.j /*construct a line for the output. */
if length(aLine)<maxW then say aLine /*Does it fit on a line? Display it. */
else say _ ' ['length(BCD.j) "digits]" /*otherwise...*/
mult= mult - 9 /*decrease the multiplier's exponent. */
maxDigs= length(BCD.j) + abs(pow) + 5 /*compute the maximum precision needed.*/
if maxDigs>digits() then numeric digits maxDigs /*increase digits if needed.*/
end /*j*/
 
say copies('═', maxW) /*display a fence for separation. */
times= 81 /*the number of times to add it. */
 
do k=-7 to 21 /*traipse through the test cases. */
parse var BCD.k mantissa '^' exponent /*decompose the zoned BCD number. */
x= mantissa'e'exponent /*reconstitute the original number. */
sum= 0 /*prepare for the 81 additions. */
do times
sum= sum + x /*multiplying the hard way, yuppers! */
end
 
sum= (sum + addend.k) / 1 /*one method to elide trailing zeroes. */
_= format(sum, , , , 0) /*force sum ──► exponential format. */
say right(k,3) 'sum=' translate(_, "e", 'E') /*use a lowercase "E" for exponents. */
end /*k*/
/*stick a fork in it, we're all done. */</syntaxhighlight>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
<br>The &nbsp; '''LINESIZE.REX''' &nbsp; REXX program is included here &nbsp; ───► &nbsp; [[LINESIZE.REX]].<br>
 
{{out|output|text=:}}'
<pre>
# addend uncompressed (zoned) BCD number
── ────── ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
-7 1e63 12345679^63
-6 1e54 +12345679012345679^54
-5 1e45 12345679012345679012345679^45
-4 1e36 12345679012345679012345679012345679^36
-3 1e27 +12345679012345679012345679012345679012345679^27
-2 1e18 12345679012345679012345679012345679012345679012345679^18
-1 1e9 12345679012345679012345679012345679012345679012345679012345679^9
0 1e0 +12345679012345679012345679012345679012345679012345679012345679012345679^0
1 1e-9 12345679012345679012345679012345679012345679012345679012345679012345679012345679^-9
2 1e-18 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-18
3 1e-27 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-27
4 1e-36 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-36
5 1e-45 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-45
6 1e-54 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-54
7 1e-63 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-63
8 1e-72 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-72
9 1e-81 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-81
10 1e-90 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-90
11 1e-99 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-99
12 1e-108 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-108
13 1e-117 [193 digits]
14 1e-126 [202 digits]
15 1e-135 [212 digits]
16 1e-144 [220 digits]
17 1e-153 [229 digits]
18 1e-162 [239 digits]
19 1e-171 [247 digits]
20 1e-180 [256 digits]
21 1e-189 [266 digits]
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
-7 sum=1e+72
-6 sum=1e+72
-5 sum=1e+72
-4 sum=1e+72
-3 sum=1e+72
-2 sum=1e+72
-1 sum=1e+72
0 sum=1e+72
1 sum=1e+72
2 sum=1e+72
3 sum=1e+72
4 sum=1e+72
5 sum=1e+72
6 sum=1e+72
7 sum=1e+72
8 sum=1e+72
9 sum=1e+72
10 sum=1e+72
11 sum=1e+72
12 sum=1e+72
13 sum=1e+72
14 sum=1e+72
15 sum=1e+72
16 sum=1e+72
17 sum=1e+72
18 sum=1e+72
19 sum=1e+72
20 sum=1e+72
21 sum=1e+72
</pre>
 
===any base===
<syntaxhighlight lang="rexx">/*REXX program performs generalized floating point addition using BCD numbers. */
parse arg base . /*obtain optional argument from the CL.*/
if base=='' | base=="," then base= 10 /*Not specified? Then use the default.*/
maxW= linesize() - 1 /*maximum width allowed for displays. */
/*Not all REXXes have the LINESIZE BIF.*/
_123= 012345679; reps= 0; mult= 63 /*vars used to construct test cases. */
say ' # addend uncompressed (zoned) BCD number' /*display the header*/
say left('── ────── ─', maxW, '─') /* " header sep*/
 
do j=-7 to 21 /*traipse through the test cases. */
reps= reps + 1 /*increase number of repetitions. */
BCD.j= strip(copies(_123, reps)'^'mult,'L',0) /*construct a zoned BCD. */
if j//3==0 then BCD.J= '+'BCD.j /*add a leading plus sign every 3rd #. */
parse var BCD.j '^' pow /*get the exponent part of the number. */
addend.j= '1e'pow /*build exponent addend the hard way. */
_= right(j, 2) right(addend.j, 6) /*construct the prefix for a line. */
aLine= _ BCD.j /*construct a line for the output. */
if length(aLine)<maxW then say aLine /*Does it fit on a line? Display it. */
else say _ ' ['length(BCD.j) "digits]" /*otherwise...*/
mult= mult - 9 /*decrease the multiplier's exponent. */
maxDigs= length(BCD.j) + abs(pow) + 5 /*compute the maximum precision needed.*/
if maxDigs>digits() then numeric digits maxDigs /*increase digits if needed.*/
end /*j*/
 
say copies('═', maxW) /*display a fence for separation. */
times= 81 /*the number of times to add it. */
 
do k=-7 to 21 /*traipse through the test cases. */
parse var BCD.k mantissa '^' exponent /*decompose the zoned BCD number. */
x= mantissa'e'exponent /*reconstitute the original number. */
sum= 0 /*prepare for the 81 additions. */
do times
sum= sum + x /*multiplying the hard way, yuppers! */
end
 
sum= (sum + addend.k) / 1 /*one method to elide trailing zeroes. */
_= format(sum, , , , 0) /*force sum ──► exponential format. */
_bX= base(_ / 1, base) /*this expresses _ in base BASE. */
say right(k,3) 'sum=' translate(_bX, "e", 'E') /*use a lowercase "E" for exponents. */
end /*k*/ /*output is in base BASEX. */
 
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
base: procedure; parse arg x 1 s 2 1 ox,tt,ii,left_,right_; f= 'BASE'
@#= 0123456789; @abc='abcdefghijklmnopqrstuvwxyz'; @abcu= @abc; upper @abcu
$= $basex() /*character string of maximum base. */
m= length($) - 1 /*M: the maximum base that can be used*/
c= left_\=='' | right_\==''
if tt=='' then tt= 10 /*assume base ten if omitted. */
if ii=='' then ii= 10 /*assume base ten if omitted. */
i= abs(ii)
t= abs(tt)
if t==999 | t=="*" then t= m
if t>m & \c then call er81 t,2 m f'-to'
if i>m then call er81 i,2 m f'-from'
 
if \c then do /*build character string for base ? */
!= substr($, 1 + 10*(tt<0), t) /*the character string for base T. */
if tt<0 then != 0 || ! /*prefix a zero if negative base. */
end
 
if x=='' then if c then return left_ || t || right_
else return left(!, t)
 
@= substr($, 1 + 10*(ii<0), i) /*@: legal characters for base X. */
oS= /*the original sign placeholder. */
if s='-' | s="+" then do /*process the sign (if any). */
x= substr(x,2) /*strip the sign character. */
oS= s /*save the original sign glyph. */
end
 
if (ii>10 & ii<37) | (ii<0 & ii>-27) then upper x /*should we uppercase it ? */
 
/*Base 10? Then it must be a number.*/
 
if pos('-', x)\==0 |, /*too many minus signs ? */
pos('+', x)\==0 |, /*too many plus signs ? */
x==. |, /*is it a single decimal point ? */
x=='' then call er53 ox /* ... or a single + or - sign ? */
 
parse var x w '.' g /*sep whole number from the fraction. */
if pos(., g)\==0 then call er53 ox /*too many decimals points? */
items.1= 0 /*number of whole part "digits". */
items.2= 0 /*number of fractional "digitsd". */
 
if c then do /*any "digit" specifiers ? */
do while w\=='' /*process the "whole" part. */
parse var w w.1 (left_) w.2 (right_) w
do j=1 for 2; if w.j\=='' then call putit w.j,1
end /*j*/
end /*while*/
 
do while g\=='' /*process the fractional part. */
parse var g g.1 (left_) g.2 (right_) g
do j=1 for 2; if g.j\=='' then call putit g.j,2
end /*j*/
end /*while*/
 
_= 0; p= 0 /*convert the whole number part. */
 
do j=items.1 to 1 by -1; _= _ + item.1.j * (i**p)
p= p + 1 /*increase the power of the base. */
end /*j*/
 
w=_; p= 0; _= 0 /*convert the fractional part. */
 
do j=1 to items.2; _=_+item.2.j/i**p
p= p + 1 /*increase power of the base. */
end /*j*/
 
g= strip( strip(_, 'L', 0), , .) /*strip leading decimal point. */
if g=0 then g= /*no signifcant fract. part.*/
end
 
__= w || g /*verify re-composed number.*/
_= verify(__,@'.') /*# have any unusual digits?*/
if _\==0 then call er48,ox,substr(__, _, 1) '[for' f i"]" /*oops─say.*/
 
if i\==10 then do /*convert number base I ──► base 10. */
/*... but only if not base 10. */
_= 0; p= 0 /*convert the whole number part. */
 
do j=length(w) to 1 by -1 while w\==''
_= _ + ((pos( substr(w, j, 1), @) - 1) * i **p)
p= p + 1 /*increase the power of the base. */
end /*j*/
w= _; _= 0; p= 1 /*convert the fractional part. */
 
do j=1 for length(g)
_= _ d + ( (pos( substr(g, j, 1), @) - 1) / i**p)
p= p + 1 /*increase the power of the base. */
end /*j*/
g= _
end
else if g\=='' then g="."g /*reinsert the period if needed. */
 
if t\==10 then do /*convert base10 number to base T. */
if w\=='' then do /*convert the whole number part. */
do j=1; _= t**j; if _>w then leave
end /*J*/
n=
do k=j-1 to 1 by -1; _= t**k; d= w % _
if c then n= n left_ || d || right_
else n= n || substr(!, 1 + d, 1)
w= w // _
end /*k*/
if c then w= n left_ || w || right_
else w= n || substr(!, 1 + w, 1)
end
 
if g\=='' then do; n= /*convert the fractional part. */
do digits()+1; if g==0 then leave
p= g * t; g = p // 1; d= trunc(p)
if c then n= n left_ || d || right_
else n= n || substr(!, d + 1, 1)
end /*digits···*/
if n==0 then n=
if n\=='' then n= '.'n /*is it only a fraction?*/
g= n
end
end
 
return oS || p( strip( space(w), 'L', 0)strip( strip(g, , 0), "T",.) 0)
/*──────────────────────────────────────────────────────────────────────────────────────*/
$basex: return @# || @abcu || @abc || space( translate(,
xrange('1'x, "fe"x), , @#'.+-'@abc || @abcu"0708090a0b0c0d"x), 0)
/*──────────────────────────────────────────────────────────────────────────────────────*/
num: procedure; parse arg x .,f,q; if x=='' then return x
if isnum(x) then return x/1; x= space( translate(x, , ','), 0)
if isnum(x) then return x/1; return numnot()
/*──────────────────────────────────────────────────────────────────────────────────────*/
putit: parse arg px,which; if \isint(px) then px= numx(px)
items.which= items.which + 1; _= items.which; item.which._= px; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
er: say '***error!***'; say; say arg(1); say; exit 13
er48: call er arg(1) 'contains invalid characters:' arg(2)
er53: call er arg(1) 'not numeric'
er81: call er arg(1) 'must be in the range:' arg(2)
isint: return datatype( arg(1), 'W')
isnum: return datatype( arg(1), 'N')
numnot: if q==1 then return x; call er53 x
numx: return num( arg(1), arg(2), 1)
p: return subword( arg(1), 1, max(1, words( arg(1) ) - 1) )</syntaxhighlight>
{{out|output|text=&nbsp; when using the (base) input of: &nbsp; &nbsp; <tt> 62 </tt>}}
<pre>
# addend uncompressed (zoned) BCD number
── ────── ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
-7 1e63 12345679^63
-6 1e54 +12345679012345679^54
-5 1e45 12345679012345679012345679^45
-4 1e36 12345679012345679012345679012345679^36
-3 1e27 +12345679012345679012345679012345679012345679^27
-2 1e18 12345679012345679012345679012345679012345679012345679^18
-1 1e9 12345679012345679012345679012345679012345679012345679012345679^9
0 1e0 +12345679012345679012345679012345679012345679012345679012345679012345679^0
1 1e-9 12345679012345679012345679012345679012345679012345679012345679012345679012345679^-9
2 1e-18 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-18
3 1e-27 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-27
4 1e-36 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-36
5 1e-45 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-45
6 1e-54 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-54
7 1e-63 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-63
8 1e-72 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-72
9 1e-81 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-81
10 1e-90 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-90
11 1e-99 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-99
12 1e-108 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-108
13 1e-117 [193 digits]
14 1e-126 [202 digits]
15 1e-135 [212 digits]
16 1e-144 [220 digits]
17 1e-153 [229 digits]
18 1e-162 [239 digits]
19 1e-171 [247 digits]
20 1e-180 [256 digits]
21 1e-189 [266 digits]
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
-7 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
-6 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
-5 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
-4 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
-3 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
-2 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
-1 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
0 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
1 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
2 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
3 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
4 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
5 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
6 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
7 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
8 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
9 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
10 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
11 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
12 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
13 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
14 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
15 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
16 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
17 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
18 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
19 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
20 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
21 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
</pre>
 
=={{header|Ruby}}==
No code, it's built in (uses '*' for multiplication):
<syntaxhighlight lang="ruby">p 12345679e63 * 81 + 1e63
p 12345679012345679e54 * 81 + 1e54
p 12345679012345679012345679e45 * 81 + 1e45
p 12345679012345679012345679012345679e36 * 81 + 1e36</syntaxhighlight>
All result in 1.0e+72.
 
=={{header|Tcl}}==
Tcl does not allow overriding the default mathematical operators (it does allow you to define your own expression engine — this is even relatively simple to do — but this is out of the scope of this task) but it also allows expressions to be written using a Lisp-like prefix form:
<syntaxhighlight lang="tcl">namespace path ::tcl::mathop
for {set n -7; set e 63} {$n <= 21} {incr n;incr e -9} {
append m 012345679
puts $n:[+ [* [format "%se%s" $m $e] 81] 1e${e}]
}</syntaxhighlight>
The above won't work as intended though; the default implementation of the mathematical operators does not handle arbitrary precision floats (though in fact it will produce the right output with these specific values; this is a feature of the rounding used, and the fact that the exponent remains expressible in an IEEE double-precision float). So here is a version that does everything properly:
<syntaxhighlight lang="tcl">namespace eval longfloat {
proc + {num args} {
set num [impl::Tidy $num]
foreach x $args {
set num [impl::Add $num [impl::Tidy $x]]
}
return [impl::Normalize $num]
}
proc * {num args} {
set num [impl::Tidy $num]
foreach x $args {
set num [impl::Mul $num [impl::Tidy $x]]
}
return [impl::Normalize $num]
}
 
namespace export + *
 
# This namespace contains the implementations of the operations and
# isn't intended to be called directly by the rest of the program.
namespace eval impl {
variable FloatRE \
{(?i)^([-+]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+))(?:e([-+]?[0-9]+))?$}
 
proc Tidy {n} {
variable FloatRE
# Parse the input
if {[llength $n] == 2} {
return $n
} elseif {[llength $n] != 1} {
return -level 2 -code error "non-numeric argument"
} elseif {![regexp $FloatRE $n -> mantissa exponent]} {
return -level 2 -code error "non-numeric argument"
}
 
# Default exponent is zero
if {$exponent eq ""} {
set exponent 0
}
# Eliminate the decimal point
set bits [split $mantissa .]
if {[llength $bits] == 2} {
incr exponent [expr {-[string length [lindex $bits 1]]}]
set mantissa [join $bits ""]
}
# Trim useless leading zeroes
return [list [regsub {^([-+]?)0*([0-9])} $mantissa {\1\2}] $exponent]
}
 
proc Normalize {n} {
lassign $n mantissa exponent
# Trim useless trailing zeroes
while {[regexp {^([-+]?.+?)(0+)$} $mantissa -> head tail]} {
set mantissa $head
incr exponent [string length $tail]
}
# Human-readable form, please
return ${mantissa}e${exponent}
}
 
# Addition and multiplication on pairs of arbitrary-precision floats,
# in decomposed form
proc Add {a b} {
lassign $a am ae
lassign $b bm be
set de [expr {$ae - $be}]
if {$de < 0} {
append bm [string repeat 0 [expr {-$de}]]
} elseif {$de > 0} {
incr ae [expr {-$de}]
append am [string repeat 0 $de]
}
list [expr {$am+$bm}] $ae
}
proc Mul {a b} {
lassign $a am ae
lassign $b bm be
list [expr {$am * $bm}] [expr {$ae + $be}]
}
}
}</syntaxhighlight>
Now we can demonstrate (compare with the original code to see how little has changed syntactically):
<syntaxhighlight lang="tcl">namespace path longfloat
for {set n -7; set e 63} {$n <= 21} {incr n;incr e -9} {
append m 012345679
puts $n:[+ [* [format "%se%s" $m $e] 81] 1e${e}]
}</syntaxhighlight>
{{out|Output (trimmed)}}
<pre>
-7:1e72
-6:1e72
-5:1e72
-4:1e72
………
18:1e72
19:1e72
20:1e72
21:1e72
</pre>
If we extend the series to 40<!-- not shown here; very boring output! -->, we still get correct computations, and that's something which is not computable with normal IEEE arithmetic (due to problems with limits on the size of exponent<!-- happens at entry 29 -->) even at reduced precision.
 
Obviously, changing the code inside the <code>impl</code> namespace would alter how the calculation was carried out.
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|wren-fmt}}
{{libheader|wren-big}}
The BigRat class in the above module allows not only rational numbers but decimal numbers of any size to be represented exactly. However, no bases other than 10 are supported.
 
Although the BigRat type supports multiplication, repeated addition has been used as in the Kotlin example.
<syntaxhighlight lang="wren">import "./big" for BigRat
import "./fmt" for Fmt
 
var repeatedAdd = Fn.new { |br, times|
var sum = BigRat.zero
for (i in 0...times) sum = sum + br
return sum
}
 
var s = "12345679"
var t = "123456790"
var e = 63
var ans = BigRat.fromDecimal("1e72")
for (n in -7..21) {
var br = BigRat.fromDecimal("%(s)e%(e)")
var oneE = BigRat.fromDecimal("1e%(e)")
var temp = repeatedAdd.call(br, 9)
var res = repeatedAdd.call(temp, 9) + oneE
Fmt.print("$2d : 1e72? $s", n, res == ans)
s = t + s
e = e - 9
}</syntaxhighlight>
 
{{out}}
<pre>
-7 : 1e72? true
-6 : 1e72? true
-5 : 1e72? true
-4 : 1e72? true
-3 : 1e72? true
-2 : 1e72? true
-1 : 1e72? true
0 : 1e72? true
1 : 1e72? true
2 : 1e72? true
3 : 1e72? true
4 : 1e72? true
5 : 1e72? true
6 : 1e72? true
7 : 1e72? true
8 : 1e72? true
9 : 1e72? true
10 : 1e72? true
11 : 1e72? true
12 : 1e72? true
13 : 1e72? true
14 : 1e72? true
15 : 1e72? true
16 : 1e72? true
17 : 1e72? true
18 : 1e72? true
19 : 1e72? true
20 : 1e72? true
21 : 1e72? true
</pre>
9,485

edits