473,378 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
4 1792
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a...
6
by: jochen scheire | last post by:
Is there a way I can calculate a field in a form based on another field in the same form. When clicking submit, both values should be posted to the next page. I want to be able to type in a value...
41
by: Saurabh Saxena | last post by:
can we calculate the 73! value in c or c++ without loss of significant digits.
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
8
by: microsoft.news.com | last post by:
I have a text file that has a column of numbers that i need to add to get a total of. Is there any way to add the number column in my text file? the text file looks like this: BMW ...
7
by: p.lavarre | last post by:
Subject: announce: FAQs suggested ... That suggested FAQ is misleadingly incorrect as stated - we need help rewording it. /F correctly commented: "eval" is never a good choice if you cannot...
6
by: moshiur | last post by:
I'm a very new programmer. I have a sorted file and want to calculate delats between the numbers. That is calculate the difference between first number in line 1 and second number in line 2...
5
FishVal
by: FishVal | last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL. So, let us say the problem is to calculate...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.