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!
don't really think its a bug, think the problem is then you do something like:
if myVala > myValb
it will work whit myVala and myValb as if it is int (think that is a 32 bit number?)
think that i will need to get my code to work is in best case make a function that compares 2 strings, and tells me what string are largest, or if they are equel.
but other people here has to be much better then me have used some other languages, but i has never been any good in any of them.
You have to write some routine to threat long numbers like strings and perform operations on strings.
Here you'll find an example with Rebol, no problem for any number as long as you like: http://rebol2.blogspot.it/2011/12/big-numbers.html
You can convert it to livecode.
Functions created are:
big-add
big-sub
big-multiply
big-divide
Rebo lenguage is simple, either it's the livecode if .
The response there from team member Dr. Peter Brett may be useful for other readers here:
LiveCode uses 64-bit IEEE 754 floating-point representation for numbers. This provides around 16 digits of decimal precision, which should be enough for most users' financial calculations.
You are cubing a 10-digit number, which produces a 30-digit number. This is more than can be represented by a 64-bit floating point number.
The precision of the calculation in this example is exactly as defined by the LiveCode language specification and the IEEE 754 standard. I am therefore closing this report as "not a bug".
Richard Gaskin LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
For financial calculations, 16 digits is fine. That is about the number of pennies in the earth's GDP. For small nations like the United States, which accounts for less than a quarter of that value, well, we have not used half-cents in centuries.
But there are larger issues. For example, such precision would only give the diameter of the universe to a few million miles. Unacceptable when planning vacations to black holes.
--Requires the "addZero" and "longAdd" functions below
function longMult arg1,arg2
put the number of chars of arg1 into index1
put the number of chars of arg2 into index2
if index1 > 8 then
repeat with y = index1 down to 1 step -8
put space after char y of arg1
if the length of word 1 of arg1 <= 8 then exit repeat
end repeat
end if
if index2 > 8 then
repeat with y = index2 down to 1 step -8
put space after char y of arg2
if the length of word 1 of arg2 <= 8 then exit repeat
end repeat
end if
put the number of words of arg1 into arg1WordCount
put the number of words of arg2 into arg2WordCount
repeat with y = 1 to arg1WordCount
repeat with u = 1 to arg2WordCount
get ((arg1WordCount - y) * 8) + ((arg2WordCount - u) * 8)
put word y of arg1 * word u of arg2 & addZero((it)) & return after temp
end repeat
end repeat
repeat with y = 1 to the number of lines of temp - 1
put longAdd(line y of temp,line y + 1 of temp) into line y + 1 of temp
end repeat
return the last line of temp
end longMult
function addZero var
repeat var
put 0 after zeros
end repeat
return zeros
end addZero
function longAdd arg1,arg2
put 0 into carry
put abs(the length of arg1-the length of arg2) into numzeros
repeat numzeros
put "0" after leadingZeros
end repeat
if the length of arg1 >= the length of arg2 then
put the length of arg1 into index
put leadingZeros before arg2
else
put the length of arg2 into index
put leadingZeros before arg1
end if
repeat with y = index down to 1
put char y of arg1 + char y of arg2 + carry into temp
if char y of arg1 + char y of arg2 + carry > 9 then put 1 into carry else put 0 into carry
put last char of temp before accum
end repeat
if carry = 1 then put "1" before accum
return accum
end longAdd
This
Last edited by dunbarx on Tue Sep 01, 2015 1:49 pm, edited 1 time in total.
In a new thread ("longAdd and longMult") in the "Talking LiveCode" section, I posted two functions that will add or multiply two integers of any length.
dunbarx wrote:In a new thread ("longAdd and longMult") in the "Talking LiveCode" section, I posted two functions that will add or multiply two integers of any length.
Now the task of subtraction and division. Ugh.
Craig Newman
Hi there,
I have started a subtraction routine based on your add. But not sure I have taken the right route. As I have dealt with the carrys after the main loop. I will post the script when I think it's good enough.
Again thanks for your time everyone. Would be nice to have all the precision functions for all to use.
(@MaxV Rebol looks good. Just have not got the time to learn and convert the functions for LC)
You can try this functions, substract and divide will follow shortly: ########CODE####### function bigAdd a1, a2 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 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 #####END OF CODE#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Ok, this contains all functions (bigAdd, bigSub, bigMultiply, BigDivide, bigGreater):
########CODE####### function bigAdd a1, a2 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 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 & " , " & a1) end bigDivide #####END OF CODE#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
MaxV has decided to save me some time and stress and has converted some Rebol functions. Special thanks for the time and effort to help me and the LC community to all involved.
Last edited by istech on Tue Sep 01, 2015 4:48 pm, edited 1 time in total.