Author Topic: Problem storing decimal numbers longer then 9 digits  (Read 7864 times)

Agradon

  • Guest
Problem storing decimal numbers longer then 9 digits
« on: July 27, 2004, 07:06:50 AM »
Hello,

I am still working on the project that requires me to store and manipulate large amounts of data. I have recently come across a very odd problem where integer variables (A,B,C...) will not store numbers that have more then 9 digits in them properly. As an example, if I read in the string "9062500000" into variable A$, and then do: B = VAL(A$), the number that winds up in variable B turns out to be 472565408. What causes this error and is there any way to avoid it? Thank you very much.

-Gene

Agradon

  • Guest
Re:Problem storing decimal numbers longer then 9 digits
« Reply #1 on: July 27, 2004, 07:12:12 AM »
Just realized that the variables are 32-bit and can only store numbers that are between 2^31 and -2^31. Is there any way to store numbers that are larger then that? i.e. up to 12 digits long?

Joel Moore

  • Full Member
  • Posts: 128
    • View Profile
Re:Problem storing decimal numbers longer then 9 digits
« Reply #2 on: July 27, 2004, 08:01:40 AM »
I think bitshifting is the way (not sure of the specific steps) but do you have to convert them to integers?  Because even if you figure out how to handle such large numbers you're not going to be able to do any math with them (at least not easily).  Why not leave them as strings?

Agradon

  • Guest
Re:Problem storing decimal numbers longer then 9 digits
« Reply #3 on: July 27, 2004, 08:15:21 AM »
I can't leave them as strings precisely because I need to perform arithmetic operations on them. Or can I? Basically I need to be able to add decimal integers that are longer then 9 digits in length, is there a way for me to do that?

-Gene

Joel Moore

  • Full Member
  • Posts: 128
    • View Profile
Re:Problem storing decimal numbers longer then 9 digits
« Reply #4 on: July 27, 2004, 06:05:48 PM »
You can probably do this but it won't be simple (I don't think).  You'll have to work with chunks of the string and loop through it.  Also, this assumes you are only planning on displaying the result since the final answer goes into a string.  Basically you are limited since there are no unsigned or 64-bit variables available on the TriPLC so you have no way of working with this data as a number.

Operand 1 is stored in A$
Operand 2 is stored in B$
Result will be stored in C$

This assumes both operands are the same length.  You'll obviously need a way to deal with operands of different lengths (i.e. "1234567890" + "123")

Code: [Select]

D = 0 ' Set the carry to "0"

FOR I = LEN(A$) TO 0 STEP -1 ' Start from the end of the strings and work backwards
     A = VAL(MID$(A$, I, 1))
     B = VAL(MID$(B$, I, 1))
     C = A + B + D
     IF C > 9 THEN
          D = 1 ' Carry the 1 for the next column (next iteration)
          C = C - 10
     ELSE
          D = 0 ' Clear the carry so it won't get added in the next iteration
     ENDIF
     C$ = STR$(C) + C$ ' Build up the answer string
NEXT

IF D > 0 THEN ' Add the final carry if there is one
     C$ = STR$(D) + C$
ENDIF


Something along those lines.  I haven't tested that but I think it should get you pointed in the right direction.  You can probably do other operations ( subtraction, multiplication, division) with similar loops.