Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Store Really Big / Small Numbers

Status
Not open for further replies.

JTBorton

Technical User
Jun 9, 2008
345
DE
so, I'm working on a unit converter, and there are some conversion factors that are really big friggin' (or small friggin') numbers. Take for example, the conversion factor for bytes into terabytes: 1 byte = 9.09494702 × 10^(-13) terabytes. Other engineering conversions can be equally significant or more. These do not fit into a double. How can I store these conversion factors, other than breaking them down until they fit into a double (example: terabyte conversion factor = TB / 10000; but that might still cause problems)?

-JTBorton
Another Day, Another Disaster
 
These do not fit into a double
Really ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I would have thought it should, but it keeps giving me an overflow message.

-JTBorton
Another Day, Another Disaster
 
Yes, I double checked, the variable is declared as a double.

-JTBorton
Another Day, Another Disaster
 
it keeps giving me an overflow message
With which code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Both lines give an overflow message:
Code:
Const TB As Double =  9.09494702E-13 'Terabytes conversion factor
Const Knots As Double = 3600 / (1609.344 * 1.15077945) 'nmi/hr conversion factor

-JTBorton
Another Day, Another Disaster
 
I don't know what the problem is, but the following code works in Excel VBA:

Code:
Function UConvert(FromVal As Double, UType As Long) As Double
Const TB As Double = 9.09494702E-13  'Terabytes conversion factor
Const Knots As Double = 3600 / (1609.344 * 1.15077945) 'nmi/hr conversion factor

Select Case UType
Case 1
UConvert = FromVal * TB
Case 2
UConvert = FromVal * Knots
End Select

End Function
 
Yes. It works fine here as well.

It is worth pointing out that sometimes VBA identifies the wrong line as the source of an error. It may be worth having a closer look at the code in its entirety to see if anything else could be causing an error
 
All,

I copied to code and placed it into a new module. It worked fine after that. I wonder if the original somehow got corrupted.

But, to answer the original question, supposing this numbers were indeed to large for a double, how could I store them?

-JTBorton
Another Day, Another Disaster
 
There is nothing larger than a double. You could probably find something if you searched the net, otherwise you would have to write your own code to handle them in multiple parts - or maybe you could just use some scaling, depending on how much accuracy you needed. It is an unusual situation that calls for such things in VBA and it tends to perform badly, as every calculation is a whole load of VBA logic for handling overflows and the like.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Since there are estimated to be something between 10^72 and 10^87 particles in the entire observable universe ( I'm struggling to think of a real application where the upper limit on a double (about 10^308 I think) would not be enough.

There are some (rare) occasions when a double provides insufficient precision. For these cases the decimal data type provides twice as many significant figures, and if you need more than that you are probably better off with a different programming language.
 
The universe is so trivially small when one plays with numbers [smile]

I have never found doubles to be insufficient in size but I have on occasion wanted accuracy greater than a decimal type would give me, which is primarily why I know that performance is dreadful - and, as you rightly say, VBA is not really the right language for such applications.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top