Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!
istech wrote:I have been testing the routines over the last couple days and all but one appear to work as expected. The function in question is "BigDivide"
Can someone confirm that if you do.
put bigDivide(99999999,10) into t
answer t
LC stops responding or crashes. This does not happen on every calculation only on some trying to narrow it down.
Yes, you are right.
MaxV's bigDivide hangs my PC as well.
breakpoint
repeat while (bigGreater(a1,a2))
put bigSub(a1,a2) into a1
put bigAdd(1,count) into count
wait 0 milliseconds with messages
end repeat
Thanks MaxV. Addling the with 0 milliseconds stops the GUI freeze up. But the time taken to process the sum is not efficient as MaxV said. Maybe some chunking of the numbers may speed it up?
function bigDivide a1,a2
#output is a block of two numbers "quotient , remainder"
if bigGreater(a2,a1) then
return ("0," & a1)
end if
if a1 = a2 then
return "1,0"
end if
put 0 into count
repeat while (bigGreater(a1,a2))
put bigSub(a1,a2) into a1
put bigAdd(1,count) into count
end repeat
if a1 = a2 then
put bigAdd(count,1) into count
put 0 into a1
end if
return (count & comma & a1)
end bigDivide
function bigDivide2 a1,a2
put length(a1) into ldividendo
put length(a2) into ldivisore
if ldividendo <= ldivisore then
return bigDivide(a1,a2)
end if
put char 1 to ldivisore of a1 into tDiv
put bigDivide(tDiv,a2) into rrr
put item 1 of rrr into quoziente
put item 1 of rrr into resto
put ldivisore + 1 into nn
repeat with i = nn to ldividendo
put char i of a1 after resto
put bigDivide(resto,a2) into temp
put item 1 of temp after quoziente
put item 2 of temp into resto
end repeat
return (quoziente & comma & resto)
end bigDivide2
returns
99999999,9
in less than a second.
Please test bigDivide2 and let me know.
TO DO:
working with negative numbers. At the present the substract function generates only positive numbers (like an abs function). Moreover all other functions work only with positive numbers.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
########CODE####### function bigAdd a1, a2 #let's check if it's negative... #end check put reverse2(a1) into b1 put reverse2(a2) into b2 put0into mem iflength(b1) < length(b2) then put b1 into temp put b2 into b1 put temp into b1 endif repeatwhile b2 isnotempty put (char1of b1) + (char1of b2) + mem into ppp iflength(ppp) = 1then put0into mem put ppp before rrr else put1into mem putchar2of ppp before rrr endif deletechar1of b1 deletechar1of b2 endrepeat repeatwhile (b1 isnotempty) put mem + (char1of b1) into ppp iflength(ppp) = 1then put0into mem put ppp before rrr else put1into mem putchar2of ppp before rrr endif deletechar1of b1 endrepeat if mem = 1then put1before rrr else put b1 before rrr endif return rrr end bigAdd
function reverse2 temp repeatforeachchar tChar in temp put tChar before temp2 endrepeat return temp2 end reverse2
function bigMultiply a1,a2 put reverse2(a1) into a1 put0into temp repeatforeachchar tChar in a1 repeatwith i=1to tChar put bigAdd(temp,a2) into temp endrepeat put0after a2 endrepeat return temp end bigMultiply
function bigGreater a1,a2 #compare two bignumbers: #return true if n1 > n2 #return false if n1 < n2 #return empty if n1 = n2 #let's check is a number is negative if ((char1of a1 = "-") or (char1of a2 = "-")) then if ((char1of a1 = "-") and (char1of a2 isnot"-")) then returnfalse endif if ((char1of a1 isnot"-") and ( char1of a2 = "-" )) then returntrue endif #if we are here, both are negative deletechar1of a1 deletechar1of a2 if a1 = a2 then returnempty else return (not bigGreater(a1,a2)) endif endif iflength(a1) isnotlength(a2) then return ( length(a1) > length(a2) ) #this is evalueted true or false else if a1 = a2 then returnempty else repeatwhile ((char1of a1) = (char1of a2) ) deletechar1of a1 deletechar1of a2 endrepeat return ((char1of a1) > (char1of a2)) #this is evalueted true or false endif endif end bigGreater
function bigSub a1,a2 #substract the smallest big number from the largest one if bigGreater(a2,a1) then put a1 into temp put a2 into a1 put temp into a2 endif put reverse2(a1) into a1 put reverse2(a2) into a2 put0into mem repeatwhile (a2 isnotempty) put (char1of a1) - mem + 10 - (char1of a2) into minus iflength(minus) = 1then put1into mem put minus before rrr else put0into mem putchar2of minus before rrr endif deletechar1of a1 deletechar1of a2 endrepeat repeatwhile (a1 isnotempty) putchar1of a1 + 10 - mem into minus iflength(minus) = 1then put1into mem put minus before rrr else put0into mem putchar2of minus before rrr endif deletechar1of a1 endrepeat #remove inital zeros repeatwhile (char1of rrr is"0") deletechar1of rrr endrepeat return rrr end bigSub
function bigDivide a1,a2 #output is a block of two numbers "quotient , remainder" if bigGreater(a2,a1) then return ("0," & a1) endif if a1 = a2 then return"1,0" endif put0into count repeatwhile (bigGreater(a1,a2)) put bigSub(a1,a2) into a1 put bigAdd(1,count) into count endrepeat if a1 = a2 then put bigAdd(count,1) into count put0into a1 endif return (count & comma & a1) end bigDivide
function bigDivide2 a1,a2 putlength(a1) into ldividendo putlength(a2) into ldivisore if ldividendo <= ldivisore then return bigDivide(a1,a2) endif putchar1to ldivisore of a1 into tDiv put bigDivide(tDiv,a2) into rrr putitem1of rrr into quoziente putitem2of rrr into resto put ldivisore + 1into nn repeatwith i = nn to ldividendo putchar i of a1 after resto put bigDivide(resto,a2) into temp putitem1of temp after quoziente putitem2of temp into resto endrepeat put removezeros(quoziente) into quoziente put removezeros(resto) into resto return (quoziente & comma & resto) end bigDivide2
function removezeros temp repeatwhilechar1of temp is"0" deletechar1of temp endrepeat return temp end removezeros #####END OF CODE#####
Please let me know.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
on mouseUp
put 123 into tNumerator
put 118 into tDenominator
put bigDivide2(tNumerator,tDenominator) into tNum
answer tNum&cr&\
item 1 of tNum &(item 2 of tNum)/tDenominator
end mouseUp
Actually this adds an extra zero ie item 1 of tNum &(item 2 of tNum)/tDenominator begins with "0."
Can you tell me what is the Livecode command to remove the leading zero?
on mouseUp
put 123 into tNumerator
put 118 into tDenominator
put bigDivide2(tNumerator,tDenominator) into tNum
answer tNum&cr&\
item 1 of tNum &(item 2 of tNum)/tDenominator
end mouseUp
Actually this adds an extra zero ie item 1 of tNum &(item 2 of tNum)/tDenominator begins with "0."
Can you tell me what is the Livecode command to remove the leading zero?
The function to remove leading zeros is removezeros(), however I'll avoid try to get decimal numbers that way, it isn't correct.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Also you say "I'll avoid try to get decimal numbers that way, it isn't correct." Why is it not correct?
How would you get the result as a decimal number?
MaxV wrote:
The function to remove leading zeros is removezeros(), however I'll avoid try to get decimal numbers that way, it isn't correct.
Also you say "I'll avoid try to get decimal numbers that way, it isn't correct." Why is it not correct?
How would you get the result as a decimal number?
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
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w