|
-
Jul 28th, 2011, 08:22 PM
#1
Thread Starter
Junior Member
[RESOLVED] How to store 40 digit number
Can i add, subtract, times, divide 40 digit number to 40 digit number ...
example:
dim a as double = 1111111111111111111111111111111111111
dim b as double = 1111111111111111111111111111111111111
dim c as double
c = a + b
msgbox(c)
output: 1.11122222222222E+28
is there a way to get the acurate answer???? plz help ...
-
Jul 28th, 2011, 08:42 PM
#2
Re: How to store 40 digit number
Ahh, very interesting problem. I don't think there are any base types that can handle 40-significant digits like this. However let me give you a hint. (This will be more fun than someone here just handing you the keys).
If you're going to want to add and subtract numbers larger than your variables will hold it's time to write a class! Develop an object that can handle your 40-digit number by using multiple variables to represent different sections of the variable. For example, let's just say that a standard Integer variable can represent just 8 digits. Your class should have 5 variables that each handle 8 of the digits. You will need to write the logic so that it will be able to handle a carry digit between the 5 segments, etc.
Keep in mind that this will not necessarily be trivial, particularly in the multiplication and division case. However it can be done. This sounds exactly like the kind of problem I'd have gotten back in my college IT classes back in 19 ... well, a long time ago!
-Max
The name's "Peck" .... "Max Peck"
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." - Red Adair
-
Jul 28th, 2011, 08:53 PM
#3
Re: How to store 40 digit number
Prior to .NET 4.0, the numeric type capable of storing the largest values is Decimal, but it can only go to 29 digits. .NET 4.0 adds the BigInteger type, which can handle integral values of any size. The BigInteger type is less efficient than other integral data types so it should only be used if absolutely necessary. I would recommend that you upgrade to VS 2010 and .NET 4.0 if you possibly can.
-
Jul 28th, 2011, 09:05 PM
#4
Fanatic Member
Re: How to store 40 digit number
Why would you want it. Who would be interested in it. You have 15 significant digits there. Why would you ever want more. If you wanted to know how many inches it is to the nearest star would it really help you to know what it is to the nearest inch? I'm sure there's some way to do it. Decimal is 128 bit whereas double is 64 bit. Decimal values go +/- 79,228 x 10 ^ 24.
I just tried out your code in Visual Studio 2010 and it isn't allowed for me. I could only go with 19 1's whether I use double or decimal. Is there some option that you adjusted?
If you don't need all that precision down among the least significant digits then you can use E.
Code:
Dim a As Double = 1.111111111111111E+40
Dim b As Double = 1.111111111111111E+40
Dim c As Double
c = a + b
MsgBox(c)
That gives an answer of 2.22222222222222E+40
Double can give you from - to + 1.7976....E308
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
Jul 28th, 2011, 09:11 PM
#5
Re: How to store 40 digit number
I guess one thing that's important to understand, for us and for you, is whether you mean total digits or significant digits. As EntityX has shown, Double can store values that have many more than 40 digits total, but the number of significant digits is much less than that.
-
Jul 28th, 2011, 10:08 PM
#6
Thread Starter
Junior Member
Re: How to store 40 digit number
Tnx for the quick replies ... i guess i will try the big integer ... hmmm need to download vb.net 2010 ...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|