small math question int+int = error

Blitz3D Forums/Blitz3D Beginners Area/small math question int+int = error

GC-Martijn(Posted 2004) [#1]
H!
I'm testing a math program for very big numbers.
2147483647 is the largest positive Integer Value in BlitzBasic [4bits]

But I want it bigger :)
I want to handle for example the code below:
Print 2147483645645647+2147483644645345

;or bigger


it must be possible. for example the windows calculator tool has a much bigger range.

Thanks for helping me
:)


dynaman(Posted 2004) [#2]
Sorry, but it's not possible without using something other then ints. Maybe floating point in blitz could handle it. The reason the calculator program can handle this is that it is using longs (or something bigger) instead of ints.


GC-Martijn(Posted 2004) [#3]
I only want:
Print 2147483645645647+2147483644645345
;or bigger


or like you say dynaman , use longs.
but blitz don't support them :(
what now ?

I can't make a .dll or something.


Shagwana(Posted 2004) [#4]
Hows about splitting the hundreds, thousands, millions (and so on) into different varibles - then you could have as many digits as you like.


TomToad(Posted 2004) [#5]
If you're not doing any complex math, Shagwana might have the right idea.




GC-Martijn(Posted 2004) [#6]
that's a cool idea :)

Its helping me a lot with {add}

And how does this trick work with * and / then I have everything i need.


Diablo(Posted 2004) [#7]
Sorry, but it's not possible without using something other then ints. Maybe floating point in blitz could handle it. The reason the calculator program can handle this is that it is using longs (or something bigger) instead of ints.
the no .sig .sig


i would gather that they use doubles and not longs. more storage


TomToad(Posted 2004) [#8]
GC-Martijn: Not going to bother figuring this out at the moment due to time, but I'll give you a clue as to how you might do it. Just think of how you'd do it with pencil and paper. When you multiply 2 numbers (we'll call them num1 and num2) you'd take the 1's column of num 2 and multiply it by the 1', 10's, 100's, etc... of num1. Then you'd do the same with the 10's column of num2, etc... Then when you were done with that, you'd add the results together, offsetting the nmumbers.

123
X456
-----
738
615
492
------
56088

You'd do the same thing with the program. Multiply the hundreds with hundreds, thousands,millions, etc... Then you do that with the thousands, etc... then add all the results together. If you were to use an array instead of a seperate variable for each one, You could create a system that allows an unlimited large int. Only thing stopping it would be time and memory.


Ross C(Posted 2004) [#9]
Hey man, whipped up a function for this. It's not as efficient as it could be, but, it works, that's whats important. Your only limit on digits, is the length of a string, which is...er....very long? :o)

; Add large number example
; By Ross C

Graphics 800,600


Global first_number$
Global second_number$

first_number = Input(" Enter the first number: ")
second_number = Input(" Enter the second number: ")

DebugLog(" first string = "+first_number)

Print " Equals:" + AddLarge$(first_number,second_number)


WaitKey()
End


Function AddLarge$(n1$,n2$)

	Local answer$ = ""
	Local temp_string$ = ""
	Local holder$ = ""
	Local temp_total% = 0
	Local a1%
	Local a2%
	Local carry% = 0
	Local templarge$
	Local tempsmall$

	If Len(n1) => Len(n2) Then ; get which number has the largest length
		templarge = n1 ; then set it to "templarge" for the longest string and "tempsmall" for the smallest one
		tempsmall = n2
	Else
		templarge = n2
		tempsmall = n1
	End If
	
	For loop = 0 To Len(templarge)-1; set up a loop which will loop through all the characters of the longest string "templarge"
		
		a1 = Mid$( templarge,Len(templarge)-loop,1) ; set a INT "a1" to the digit of the longest loop
		If Len(tempsmall) > loop Then ; if the smallest string is long enough, then set INT "a2" to the "loop" digit from the right
			a2 = Mid$( tempsmall,Len(tempsmall)-loop,1)
		Else
			a2 = 0 ; else just make it zero to fit the calculation
		End If
		
		temp_total = a1 + a2 + carry ; perform the calculation, taking into account the carry from a prevoius loops calculation
		If a1 + a2 > 9 Then
			carry = 1 ; set the carry to 1 if the calculation for the two digits is greater than 9 (the highest single digit)
		Else
			carry = 0 ; else set it to zero
		End If
		
		If loop > 0 Then ; make sure the loop is greater than 0 before putting the contents of the temp_string into the holder string
			holder = temp_string ; used to hold the contents of the temp_string. Basically so you can insert a digit at the beginning
								 ; of the temp_string variable. Copy to holder, set temp_string to the digit, then add the holder string back on :o)
		End If

		temp_string = Str(Right$(temp_total, 1)) ; set the temp_string to the rightmost digit in temp_total. This eliminates adidng calculation that went over 9
		temp_string = temp_string+holder ; add the holder string back on, to make up the answer so far
		answer = temp_string ; make the answer string equal temp string so far. Could get away with just using temp_string, but i didn't think of that :o)
		
	Next

	Return answer$ ; return the answer back across
	
End Function



GC-Martijn(Posted 2004) [#10]
That's a second cool function :)

Maybe i'm checking if there is a trick for pow and sqrt.
But there are a lot of numbers behind the , .

i'm not the best in Math :S

Thanks Ross C