By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,759 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,759 IT Pros & Developers. It's quick & easy.

Is there a way to calculate BIG numbers expressed as strings?

P: n/a
Hello!

I wonder if there's a good way to make a function that calculates 2
big numbers (whole numbers) expressed as strings and return a result
string with the value of a number?

For example:

string MyAdd(string s1, string s2);
s = MyAdd("10", "20"); //=> returns "30"

When the numbers are small, no problem.
However, if the numbers are like
"1234567890123456789012345678901234567890123456789 01234567890", how
can I convert it to number?
The number is longer than the significance digits of double or
decimal.

TIA.
Sam
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Look for arbitrary length arithmetics libraries -- there are many free. Or
you can easily make your own for the basic operators, simulating "pen and
paper" calculations.
Uzytkownik "Sam Kong" <ss*@chol.net> napisal w wiadomosci
news:f0**************************@posting.google.c om...
Hello!

I wonder if there's a good way to make a function that calculates 2
big numbers (whole numbers) expressed as strings and return a result
string with the value of a number?

For example:

string MyAdd(string s1, string s2);
s = MyAdd("10", "20"); //=> returns "30"

When the numbers are small, no problem.
However, if the numbers are like
"1234567890123456789012345678901234567890123456789 01234567890", how
can I convert it to number?
The number is longer than the significance digits of double or
decimal.

TIA.
Sam

Nov 16 '05 #2

P: n/a
Hi,

Not in the framework.

Of course you can always implement a Add routine, if you need other
operations then it's different :)

maybe you can find some third party module that does that.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Sam Kong" <ss*@chol.net> wrote in message
news:f0**************************@posting.google.c om...
Hello!

I wonder if there's a good way to make a function that calculates 2
big numbers (whole numbers) expressed as strings and return a result
string with the value of a number?

For example:

string MyAdd(string s1, string s2);
s = MyAdd("10", "20"); //=> returns "30"

When the numbers are small, no problem.
However, if the numbers are like
"1234567890123456789012345678901234567890123456789 01234567890", how
can I convert it to number?
The number is longer than the significance digits of double or
decimal.

TIA.
Sam

Nov 16 '05 #3

P: n/a
Les
This is very very simple.

Since you are dealing with strings you have a wealth of
conversion function and substring indexing. With a
little imagination and math structuring knowlege you can
easily see that if you are dealing with whole number you
can start from the right side and move to the left adding
2 digits as you go and moving the carry along. ie

"12345678901234567890" + "3333333333333333333"
These numbers would be processed by adding the 2 right
most digits together 0 and 3 yeilding 3. Your result
string is built from the left side then as follows:

myresult := 3;

now the next value: 9 + 3 is 12.... only process 1 digit
(right most) and carry the rest.

mysresult = newDigit + myresult ==> "23"

next comes 3 + 8 and don't forget the carry + 1 goes to
12 so: "223" and a carry...

and so forth. You can easlity generalize this algorithm
to a routine that will do any sized numbers and add
a "find the decimal point" feature for decimals.

hope this helps, if not give an e-mail i'll generalize
the algo for you.

-----Original Message-----
Hello!

I wonder if there's a good way to make a function that calculates 2big numbers (whole numbers) expressed as strings and return a resultstring with the value of a number?

For example:

string MyAdd(string s1, string s2);
s = MyAdd("10", "20"); //=> returns "30"

When the numbers are small, no problem.
However, if the numbers are like
"123456789012345678901234567890123456789012345678 90123456 7890", howcan I convert it to number?
The number is longer than the significance digits of double ordecimal.

TIA.
Sam
.

Nov 16 '05 #4

P: n/a
Thanks for the reply.

Well... addition may be simple.
But what about multiplication?
Could you send me the general algo, please?

su***********@nospam.hotmail.com

Thank you.

Sam

"Les" <vb**********@yahoo.com> wrote in message
news:3e****************************@phx.gbl...
This is very very simple.

Since you are dealing with strings you have a wealth of
conversion function and substring indexing. With a
little imagination and math structuring knowlege you can
easily see that if you are dealing with whole number you
can start from the right side and move to the left adding
2 digits as you go and moving the carry along. ie

"12345678901234567890" + "3333333333333333333"
These numbers would be processed by adding the 2 right
most digits together 0 and 3 yeilding 3. Your result
string is built from the left side then as follows:

myresult := 3;

now the next value: 9 + 3 is 12.... only process 1 digit
(right most) and carry the rest.

mysresult = newDigit + myresult ==> "23"

next comes 3 + 8 and don't forget the carry + 1 goes to
12 so: "223" and a carry...

and so forth. You can easlity generalize this algorithm
to a routine that will do any sized numbers and add
a "find the decimal point" feature for decimals.

hope this helps, if not give an e-mail i'll generalize
the algo for you.

-----Original Message-----
Hello!

I wonder if there's a good way to make a function that

calculates 2
big numbers (whole numbers) expressed as strings and

return a result
string with the value of a number?

For example:

string MyAdd(string s1, string s2);
s = MyAdd("10", "20"); //=> returns "30"

When the numbers are small, no problem.
However, if the numbers are like
"123456789012345678901234567890123456789012345678 90123456

7890", how
can I convert it to number?
The number is longer than the significance digits of

double or
decimal.

TIA.
Sam
.

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.