VB.net variable type Double is too small

xphantg0d

Gawd
Joined
Jun 9, 2004
Messages
633
Im using Visual Studio 2003, in vb.net the variable type double is to my knowledge the largest number variable but it only goes to 1.79769313486231570E+308 or basically ~1.8 * 10 ^ 308. I am writing some math programs and any number bigger than that returns the value inifinity. Is there a way around this limitation?

Thanks in advance.
 
you can try using the special microsoft only data types, otherwise you'll have to scheme up some way to do floating points yourself.
 
You could try writing your code such that intermediate calculations don't lead to such large numbers... Which begs the question of how you're managing to run into it anyways... theoretical math? Because such large numbers currently have no practical use, AFAIK.
 
The code doesn't really have any practically applications beyond my own personal entertainment. I am just writing some code to demonstrate some stuff I have learned in calculus. Dealing with some partial infinite series there are some rather large factorial values which exceed doubles limitations (from my experiments I think double could only handle 170! ).
Also is there a way to return the exact value of a number in scientific notation ie. instead of 2.393848 * 10 ^13 get 2393848567382

Whatsisname you mentioned some Microsoft specific variables, do you know where I could learn more about those.
 
Floating point numbers have limited precision, so getting "exact" values isn't going to happen with them.

What you can do is create a "hugeint" class. In the implementation, string together as many 32 bit unsigned integers as it takes to get the job done. When you overflow out of one, carry over into the next. Also, you can add functions to the class to handle outputting the number however you want.
 
Note that for no loss of precision, you'll need approximately 10 bits for every 3 places in decimal. When calculating a huge factorial, break it up into runs that will fit into conventional integers and at the end, sum up all the conventional integers into a "hugeint". Otherwise, if you use a "hugeint" all the way through the calculation of the factorial, you'll suffer a major performance penalty.

Also, if you'd be calculating similar factorials over and over, save yourself some calculating and build a lookup table of all the factorials from 0 to whatever maximum number you need (note that you should do it in this order, as to generate n! you'd only need to multiply n by (n-1)!, which was the previous number generated... no need to recalculate it.) Then, instead of calculating factorials later, all you'd need to do is 1 lookup per factorial. (You might want to persist it to a file so you don't need to recalculate it every time you run the program.)
 
These are some quality ideas. Thanks alot Cardboard Hammer. I will look into this hugeint implementation you mentioned but if you know of any place with some good examples and information on this it would be much appreciated.
 
Cardboard Hammer said:
If you understand C++ at least somewhat, this might help a bit: http://www.ece.utexas.edu/~tjwagner/EE351K/Notes/ExactPokerCalculations/HugeInt.cp

If you Google on "hugeint", most everything you get back is C/C++ (as they've been around longer/ offer better performance than VB.NET).

This does look like an interesting implementation. The header you'll need is at http://www.ece.utexas.edu/~tjwagner/EE351K/Notes/ExactPokerCalculations/HugeInt.h, but was trashed by poor text-to-HTML conversion.

There's a few problems with the class, though. It's neat that he does his internal math in binary instead of using BCD, since that makes the math operations pretty efficient. But converting from a string to a HugeInt and back is very expensive. There's plenty of interesting optimizations that could be done in order to get lots of performance.

Also, his integer constructor doesn't work because it truncates data without warning. You'd expect this to print 35242342

Code:
	HugeInt h;
	h = 35242342;
	cout << h;

but it prints 49510.

It seems like a great starting point, as most of the algorithms themselves look correct.

.B ekiM
 
mikeblas said:
C#, you mean? I've heard the CSharp Station tutorial is good. You can find lots more if you search the web.

.B ekiM

Ya, the reason I linked that is because it can be used from your VB app without learning another language.

Any .NET project can use modules written in other .NET languages. So all you have to do is put the code in a new C# library project and build it. You can then add a reference to it in your VB project and use it like normal.
 
Back
Top