Routines for very large numbers.
Posted: Mon Aug 24, 2015 9:31 am
Hi all,
I have a test project that needs to calculate very large numbers (30-50 digit plus). I have been running into problems with the accuracy of LC manipulating these numbers as they do not come out correctly
calculated.
After much research I found out from a few posts LC's built in function does not work correctly over 15 digits and would need to use custom functions to get the accuracy I need. Below is one of the functions I have
found. I need to now create the divide-subtract and add routines. Can any one give me some pointers or pseudocode to play with many thanks.
function bigTimesN7 X,Y
if char 1 of X is "-" then
put "-" into leadChar
delete char 1 of X
end if
if char 1 of Y is "-" then
if leadChar is "-" then
put empty into leadChar else put "-" into leadChar
delete char 1 of Y
end if
put (6 + length(X)) div 7 * 7 into XL
put char 1 to XL - length(X) of "000000" before X
put (6 + length(Y)) div 7 * 7 into YL
put char 1 to YL - length(y) of "000000" before y
repeat with N = XL + YL down to 15 step -7
repeat with M = max(7,N - YL) to min(XL,N - 7) step 7
add (char M - 6 to M of X) * (char N - M - 6 to N - M of Y) to S
if S > 900000019999997 then
add char -20 to -8 of S to Scarry
delete char -20 to -8 of S
end if
end repeat
put char -7 to -1 of ("0000000" & S) before R
add char -20 to -8 of S to Scarry
put char -7 to -1 of Scarry into S
delete char -7 to -1 of Scarry
end repeat
put Scarry & S before R
repeat with i = 1 to 15
if char i of R is not "0" then exit repeat
end repeat
if i > 1 then delete char 1 to i - 1 of R
return leadChar & R
end bigTimesN7
I have a test project that needs to calculate very large numbers (30-50 digit plus). I have been running into problems with the accuracy of LC manipulating these numbers as they do not come out correctly
calculated.
After much research I found out from a few posts LC's built in function does not work correctly over 15 digits and would need to use custom functions to get the accuracy I need. Below is one of the functions I have
found. I need to now create the divide-subtract and add routines. Can any one give me some pointers or pseudocode to play with many thanks.
function bigTimesN7 X,Y
if char 1 of X is "-" then
put "-" into leadChar
delete char 1 of X
end if
if char 1 of Y is "-" then
if leadChar is "-" then
put empty into leadChar else put "-" into leadChar
delete char 1 of Y
end if
put (6 + length(X)) div 7 * 7 into XL
put char 1 to XL - length(X) of "000000" before X
put (6 + length(Y)) div 7 * 7 into YL
put char 1 to YL - length(y) of "000000" before y
repeat with N = XL + YL down to 15 step -7
repeat with M = max(7,N - YL) to min(XL,N - 7) step 7
add (char M - 6 to M of X) * (char N - M - 6 to N - M of Y) to S
if S > 900000019999997 then
add char -20 to -8 of S to Scarry
delete char -20 to -8 of S
end if
end repeat
put char -7 to -1 of ("0000000" & S) before R
add char -20 to -8 of S to Scarry
put char -7 to -1 of Scarry into S
delete char -7 to -1 of Scarry
end repeat
put Scarry & S before R
repeat with i = 1 to 15
if char i of R is not "0" then exit repeat
end repeat
if i > 1 then delete char 1 to i - 1 of R
return leadChar & R
end bigTimesN7