Arithmetic operations with large numbers

  • Thread starter Thread starter Sam Fisher
  • Start date Start date
S

Sam Fisher

Hi,
I have a situation where I have to support a large number(not decimals),
something of the order of 20 to 30 digits in the database(sql server 2000)
I am not able to use LONG data type as it exceeds the limit, so I am
thinking of using the string value now. But do not know how to do the
arithmetic operations on this large number, I need the simple operations
like incrementing the number by one etc.

Any help will be greatly appreciated.

Sam
 
They are basically the certified mail article numbers
They are going to be numeric, but they are right now fixed length 20 chars
I can very well store them as String(Varchar) in the application, but I do
not know how to increment these numbers. I think I need help on conversion
of these stings to number with some numeric format and then increment it,
then get the string representation to save it back to the database.
Thanks
 
Sam,
Could you not use something like:

long bigNumber = long.Parse(bigNumberString);
bigNumber.ToString();

or even

ulong bigNumber = ulong.Parse(bigNumberString);
bigNumber.ToString();

Jason Newell, MCAD
Software Engineer
 
They are basically the certified mail article numbers
They are going to be numeric, but they are right now fixed length 20 chars
I can very well store them as String(Varchar) in the application, but I do
not know how to increment these numbers. I think I need help on conversion
of these stings to number with some numeric format and then increment it,
then get the string representation to save it back to the database.
Thanks

According to the documentation the largest possible decimal value with a
scale of 0 (no decimal places is)
+/-79,228,162,514,264,337,593,543,950,335. Is it likely you will ever
exceed this limit?

If you think they will, then you would probably be better off storing
them as a byte array and then writting functions to handle the
mathimatical operations... Of course, it may be easier to find a
pre-existing arbitrary precision integer library.
 
Sam,

I don't think that you have much possibilities.

You can use the classic method for accumulators of course

You can splits them in two string from 10
assuming you add 20 the last value is 9.999.999.999 and the first is
1,000,000,000

convert them to two int64 and than when you add 20 and the the last becomes
9.999.999.999 than add 1 to the first value and set the second to the
difference.

Than it will be
1.000.000.001 and the second 0.000.000.019

Just my thought,

Cor
 
Sam,
| I am not able to use LONG data type as it exceeds the limit,
I would consider doing something similar to what Cor suggests, Define a
LongLong data type (an Int128 if you will) . I would consider making this
data type an immutable Structure that has two Long fields, a low order long
& a hi order long. Possible four Integers...

Then define the "operators" that I needed on it.

I would consider using C# or VB 2005 as defining the low order long as a
unsigned long would be easier...

The "trick" is going to be defining your operators, as you need to go back
to Math 101 & remember how to carry & borrow digits when you add & subtract
the numbers. Hint each of the fields (2 longs or 4 integers) represent a
"digit"...

Doing a quick google search, I found the following that may give you an idea
of how to do this.

http://blogs.msdn.com/ajenner/default.aspx

HINT: You need unchecked math, alternatively you could handle the exception
when the low digits have an overflow exception... Again unsigned for the low
digits would be easier...

Hope this helps
Jay




| Hi,
| I have a situation where I have to support a large number(not decimals),
| something of the order of 20 to 30 digits in the database(sql server 2000)
| I am not able to use LONG data type as it exceeds the limit, so I am
| thinking of using the string value now. But do not know how to do the
| arithmetic operations on this large number, I need the simple operations
| like incrementing the number by one etc.
|
| Any help will be greatly appreciated.
|
| Sam
|
|
|
|
 
Use System.Decimal

From MSDN:

The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative
79,228,162,514,264,337,593,543,950,335.
 
Back
Top