 Numbers bigger than 1,000,000,000July 5 2008 at 8:44 AM  Bikram (Login Bikramthegreat) 
 Hi,
I need to make a program which can handle variable larger than 1,000,000,000 for addition. The problem with integers is that I start getting 1.0E09 which I don't want. I tried to make them string but then I can't do simple addition with numbers like v=v+10 it would say "type mismatch". Any suggestions. Thanks in advance. 
 Author  Reply  Pete (Login TheUniverse) Admin  You can stretch it some by using #  July 5 2008, 9:42 AM 
CLS
INPUT x#
PRINT x# + x#
END

Also check it with numbers like:
x# = 100000000002345#
or
x# = VAL("100000000002345")
Integers are limited to 32768 to 32767 but double precision numbers go much further. You can also use DIM x as DOUBLE at the top of your program as in:
DIM x as DOUBLE
INPUT x
PRINT x + x
END
Double precision will give you up to 15digits to use for the sum of your numbers. Beyond that, you would need to convert with strings and a function to add together. I know you stated integers, which means you could use long (&) but double allows for larger integers than long does, even though it 'doubles' for decimal numbers.
Personally, I always like to convert to strings for most applications to avoid other problems but if this gets you by for what you need, great. I would like to know what type of project you are working on and if you need some help in the future, just post the code you have so far and ask for help where you are stuck?
Pete
This message has been edited by TheUniverse on Jul 5, 2008 9:55 AM 

 qbguy (no login)  QB64 allows 64bit integers  July 5 2008, 10:54 AM 
64bit integer is
9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 (signed)
or
0 to 18,446,744,073,709,551,615 (unsigned).
If you need to calculate exact numbers higher than that then you need arbitrary precision arithmetic like in Scheme or Java.

 qbguy (no login)  Oops, I forgot  July 5 2008, 11:01 AM 
GENERIC GALLEON COMPILER IS NOT READY YET AND I'M NOT SUPPOSED TO MENTION IT IN THIS FORUM AND EVEN THOUGH IT USUALLY GETS BETTER WITH EACH DEMO WHICH IS USUALLY RELEASED EVERY MONTH (A MONTH ON THE THIRD PLANET FROM THE SUN ("EARTH") IS ONE TWELFTHPART OF THE TIME IT TAKES FOR THE PLANET TO REVOLVE AROUND THE SUN, WHICH IS USUALLY THIRTY TIMES THE TIME IT TAKES FOR THE PLANET TO ROTATE ON ITS AXIS), YOU MIGHT TRY IT AND GIVE UP ON IT AND NEVER USE IT AGAIN. DO NOT EAT QB64. 
 Bikram (Login Bikramthegreat)  No approximation  July 5 2008, 11:23 AM 
Well just need it not approximate. Like instead of giving me 102154548343 it gives 1.021E07. I just dont want the times 10 approximated value. 
 The PhyloGenesis (Login PhyloGenesis) R  You can't do that  July 6 2008, 1:00 PM 
You cannot "convert" it back if it gives you this. You must increase the precision you are using to get the full number.
DIM a AS SINGLE
a = 102154548343#
PRINT a
Outputs:
1.021546E+11
Whereas
DIM a AS DOUBLE
a = 102154548343#
PRINT a
Outputs:
102154548343
PhyloGenesis

 Clippy (Login burger2227) R  To view the number try PRINT USING  July 5 2008, 11:49 AM 
The E number may be ugly, but it is an actual number. Do your math and then try printing it. PRINT USING to slightly less than the full number places should eliminate the D or E.
Actually if you INPUT a number E is also considered a number and will not create a Redo from start error either.
I left you a responce at QBStation also!
Ted 
 Bikram (Login Bikramthegreat)  Important number at the end.  July 5 2008, 11:56 AM 
Thanks for the response on both website. I actually need the last digits so I can't have E at all. Even if there was a way to print as a number (not with E) after all the additions that would have worked too.
Thanks again 
 Michael Calkins (Login MCalkins) R  adddecstr  April 20 2011, 11:18 PM 
adddecstr adds two nonnegative integers, in the form of strings. The result is stored in the first string. The strings should contain only ASCII decimal numerals in bigendian order. It should work with input strings up to 32766 bytes long, but I haven't tested it with strings that big.
Regards,
Michael
'public domain, april 2011, michael calkins
DEFINT AZ
SUB adddecstr (a AS STRING, b AS STRING)
DIM i AS INTEGER
DIM c AS INTEGER
DIM n AS INTEGER
n = LEN(b)  LEN(a)
IF n < 0 THEN n = 0
a = STRING$(n + 1, &H30) + a
c = 0
FOR i = 0 TO LEN(a)  1
IF i >= LEN(b) THEN
n = &H30
ELSE
n = ASC(MID$(b, LEN(b)  i, 1))
END IF
n = c + ASC(MID$(a, LEN(a)  i, 1)) + n + &HFFD0
IF n > &H39 THEN
c = 1
n = n  &HA
ELSE
c = 0
END IF
MID$(a, LEN(a)  i, 1) = CHR$(n)
NEXT
FOR i = 1 TO LEN(a)
IF ASC(MID$(a, i, 1)) > &H30 THEN a = MID$(a, i): EXIT FOR
IF i = LEN(a) THEN a = CHR$(&H30)
NEXT i
END SUB 
 Michael Calkins (Login MCalkins) R  addbinstr  April 26 2011, 10:10 PM 
This function is similar to the other one in a few ways, but also different in a few ways.
Like the other one, it accepts two input strings of basically any lengths. Like the other one, it stores the result in the first string.
In this case, the string is not a sequence of decimal numerals in bigendian order, but is a littleendian unsigned integer of arbitrary size.
Note that in this function, as well as in the last, the two input strings can be different lengths. The first one, since it will store the result, is expanded to the size of the larger string, plus 1, to accommodate a possible carry. Afterward, leading zeros are removed. In the case of the other function, 0 becomes "0". In the case of this function, 0 becomes "", an empty string.
Regards,
Michael
'public domain, april 2011, michael calkins
DEFINT AZ
SUB addbinstr (a AS STRING, b AS STRING)
DIM i AS INTEGER
DIM c AS INTEGER
DIM n AS INTEGER
n = LEN(b)  LEN(a)
IF n < 0 THEN n = 0
a = a + STRING$(n + 1, &H0)
c = 0
FOR i = 1 TO LEN(a)
IF i > LEN(b) THEN
n = &H0
ELSE
n = ASC(MID$(b, i, 1))
END IF
n = c + ASC(MID$(a, i, 1)) + n
IF n AND &H100 THEN
c = 1
n = n AND &HFF
ELSE
c = 0
END IF
MID$(a, i, 1) = CHR$(n)
NEXT
FOR i = LEN(a) TO 1 STEP 1
IF ASC(MID$(a, i, 1)) > &H0 THEN a = LEFT$(a, i): EXIT FOR
IF i = 1 THEN a = ""
NEXT i
END SUB
This message has been edited by MCalkins on Apr 26, 2011 10:17 PM 

 David (no login)  Split the number  April 22 2011, 11:25 AM 
Say 1st 8 digits, 2nd 8 digits etc. then do the adds with carries.
That's how the processor does it. Remember qbasic is only 16 bit. So for long integers when the 16 bit overflows it starts again and the carry bit is set to one. 
 Clippy (Login burger2227) R  * Qbasic numbers can't use commas...  April 23 2011, 12:54 PM 
 lawgin (no login)  If only adding positive integers...  April 23 2011, 12:41 PM 
this will add integers of any length, treating them as strings.
INPUT "2 integers to add ", a$, b$
DO UNTIL a$ = "" AND b$ = ""
c = VAL(RIGHT$(a$, 1)) + VAL(RIGHT$(b$, 1)) + cry
c$ = c$ + LTRIM$(STR$(c MOD 10))
cry = c \ 10
IF a$ <> "" THEN a$ = LEFT$(a$, LEN(a$)  1)
IF b$ <> "" THEN b$ = LEFT$(b$, LEN(b$)  1)
LOOP
FOR d = LEN(c$) TO 1 STEP 1
d$ = d$ + MID$(c$, d, 1)
NEXT
PRINT "sum = "; d$

 lawgin (no login)  oops, forgot the last carry  April 23 2011, 12:51 PM 
INPUT "2 integers to add ", a$, b$
DO UNTIL a$ = "" AND b$ = ""
c = VAL(RIGHT$(a$, 1)) + VAL(RIGHT$(b$, 1)) + cry
c$ = c$ + LTRIM$(STR$(c MOD 10))
cry = c \ 10
IF a$ <> "" THEN a$ = LEFT$(a$, LEN(a$)  1)
IF b$ <> "" THEN b$ = LEFT$(b$, LEN(b$)  1)
LOOP
IF cry THEN c$ = c$ + "1"
FOR d = LEN(c$) TO 1 STEP 1
d$ = d$ + MID$(c$, d, 1)
NEXT
PRINT "sum = "; d$
 
   

