OIC. Thanks, I didn't think of thatdunbarx wrote:GoLive.
The function "removeZeros" is part of MaxV's suite of handlers. It is not a native function. Look back at his post where he lists all his functions.
Craig

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
OIC. Thanks, I didn't think of thatdunbarx wrote:GoLive.
The function "removeZeros" is part of MaxV's suite of handlers. It is not a native function. Look back at his post where he lists all his functions.
Craig
Ah yes, you are right. But one will normally need the result as a decimal to display an answer or to do further calculations.MaxV wrote:
For example:
1 / 3 = 0.3 is not correct, it's an approximation 0.3=3/10
1 / 3 = 0.33 is not correct, it's an approximation 0.33=33/100
1 / 3 = 0.333 is not correct, it's an approximation 0.333=333/1000
1 / 3 = 0.333333333 is not correct, it's an approximation
1 / 3 = 0.3333333333333333333333333333 is not correct, it's an approximation
1 / 3 = 0.3333333333333333333333333333333333 is not correct, it's an approximation
1 / 3 = 0.3333333333333333333333333333333333333333333333333333333333 is not correct, it's an approximation
1 / 3 = 0 with remainder of 1. This is the only correct solution contemplating all digits
Code: Select all
put bigDivide2(123,111) into tA
Put sqrt(tA) into tB
Code: Select all
function bAdd x,y
if char 1 of x is "-" then
delete char 1 of x
if char 1 of y is "-" then
delete char 1 of y
put "-" into theSign
else
return bSubtract(y,x)
end if
else
if char 1 of y is "-" then
delete char 1 of y
return bSubtract(x,y)
else
put empty into theSign
end if
end if
put length(x) into lenX
put length(y) into lenY
if lenX > lenY then
put x into r
put lenX into lenR
else
put y into r
put lenY into lenR
put x into y
put lenX into lenY
end if
put 0 into c
repeat with i = 14 to lenR step 14
add char -i to 13 - i of r + char -i to 13 - i of y to c
put char -14 to -1 of ("00000000000000" & c) into char -i to 13 - i of r
delete char -14 to -1 of c
if i > lenY and c is empty then exit repeat
end repeat
put c before r
put 0 + char 1 to 14 of r into char 1 to 14 of r
return theSign & r
end bAdd
function bSubtract x,y
if char 1 of x is "-" then
if char 1 of y is "-" then
delete char 1 of x
delete char 1 of y
put "-" into theSign
else
return bAdd(x,"-" & y)
end if
else
if char 1 of y is "-" then
return bAdd(x,char 2 to -1 of y)
else
put empty into theSign
end if
end if
put length(x) into lenX
put length(y) into lenY
if lenX > lenY then
put x into r
put lenX into lenR
else if lenX < lenY then
if theSign is "-" then put empty into theSign else put "-" into theSign
put y into r
put lenY into lenR
put x into y
put lenX into lenY
else
get bCompare(x,y)
if it is "greater" then
put x into r
put lenX into lenR
else if it is "less" then
if theSign is "-" then put empty into theSign else put "-" into theSign
put y into r
put lenY into lenR
put x into y
put lenX into lenY
else
return 0
end if
end if
put 0 into c
repeat with i = 14 to lenR step 14
put char -i to 13 - i of r into s
add char -i to 13 - i of y to c
if s >= c then
put s - c into s
put 0 into c
else
put ("1" & s) - c into s
put 1 into c
end if
put char -14 to -1 of ("00000000000000" & s) into char -i to 13 - i of r
if i > lenY and c = 0 then exit repeat
end repeat
put 0 + char 1 to 15 of r into char 1 to 15 of r
return theSign & r
end bSubtract
function bCompare x,y -- only works on unsigned values
put length(x) into lenX
put length(y) into lenY
if lenX > lenY then return "greater"
if lenX < lenY then return "less"
repeat with i = 1 to lenX step 14
put char i to i + 13 of x into x1
put char i to i + 13 of y into y1
if x1 > y1 then return "greater"
if x1 < y1 then return "less"
end repeat
return "equal"
end bCompare
Code: Select all
function bTimes X,Y
if X = 0 or Y = 0 then return 0
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 X & Y into R
put "0000000000000000" into char 1 to 10 of R
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
put length(R) - 6 into rStart
repeat with N = XL + YL down to 15 step -7
put min(XL,N - 7) into finalM
put 0 into C
repeat with startM = max(7,N - YL) to finalM - 7 step 630
repeat with M = startM to min(startM + 623,finalM) step 7
add (char M - 6 to M of X) * (char N - M - 6 to N - M of Y) to S
end repeat
add char 1 to -8 of S to C
delete char 1 to -8 of S
end repeat
put char -7 to -1 of ("000000" & S) into char rStart to rStart + 6 of R
subtract 7 from rStart
put C into S
end repeat
if S > 0 then put S into char max(1,rStart) to rStart + 6 of R
put 0 into zR
repeat until zR > 0
put 0 + char 1 to 15 of R into zR
put zR into char 1 to 15 of R
end repeat
return leadChar & R
end bTimes
Code: Select all
if "12345678901234561234" = "12345678901234561235" then
answer "same number" with "okay" --will always be true
end if
Code: Select all
answer "12345678901234561234" = "12345678901234561235" --last digit different
Thanks for the great post. Going to give this a try and compare the performance. However using long numbers would still be a problem unless I use python? or a custom LC function to check. See below:[-hh] wrote:Just to complete this interesting discussion with another option.
You could use python's bignum package via shell (or more advanced: use sockets).
Here is a stack that show hows to do this, use example 1 for start.
What one should know: In Python is "power" not "^" but "**", for example 2^5 is in python 2**5.
Python helper (= Raspi stacks collection #22) http://forums.livecode.com/viewtopic.ph ... 55#p101655
Code: Select all
put "12345678912345678919" into t
switch
case t = "12345678912345678912"
answer "same number - 1" with "okay" --still true
break
case t = "12345678912345678913"
answer "same number - 2" with "okay" --still true
break
end switch