really small numbers

Started by
23 comments, last by Mulligan 21 years, 7 months ago
Sure it will, but you have to add .0f
1989000000000000000000000000.0f will work...
Without that the compiler thinks it''s an integer and it''s to long.
Advertisement
You might also want to rethink your units. Are you ever going to need precision to a single metric ton? Since you are dealing with cosmic bodies, I''m guessing no.


The hackers must have gotten into the system through the hyperlink!!

Invader''s Realm
Mulligan:

Maybe it would help if I explained what floating point numbers are.

floats and doubles are both floating point data types.

An integer type, essentially, can store a fixed number of digits. That''s not how floating point numbers work.

Are you familiar with scientific notation? That''s how floating point numbers work. They wtore a mantissa> and an exponent (and a sign). Whereas an integer would store the following:

0000000000000064

A floating-point number would store this:

+ 6.4000000000e00001

In the floating-point example, the first part is the sign, the second is the mantissa, and the third is the exponent. (Note the "E" reads "times ten to the").

Now, for small numbers like 64, it doesn''t matter; integers are fine. But for really tiny numbers and really huge numbers, floating point variables work whereas integers don''t.

A double, in MSVC++, can hold a number as high as 1.7e308. Now, for some perspective: there are only approximately 1e79 atoms in the universe. I think a double can hold big enough numbers!! (Chances are floats are good enough for most things too; their max is 3.4E38).

Now, as you may have realized by now, although it can store both huge and tiny numbers, a double can''t hold all 300+ digits of a number. It stores numbers with a certain precision. This is fine. If you''re storing the mass of the Sun, you don''t need to know it to the milligram. To give you some perspective on how much precision is really needed, I once read that NASA used only the first ten digits of PI to go to the moon. Floats hold seven digits and doubles hold 15. I''m sure at least the double is enough.

But let''s say that even this isn''t good enough for you. Let''s say that you need to store a really massive number with precision to the ten-thousandths place. What do you do? Use an arbitrary-precision number class. It''s computationally slow, and I''m sure you don''t need it for your purposes, but when ridiculous precision is needed, this is what people do. Note that people rarely do it, and that doubles are used for almost all scientific computing.
Amen
The way the floating point problem was explained to me (in a DSP class):
You have an elevator that can run over 20 or 30 floors (your mantissa). The elevator can start all the way in the basement or all the way at the top of the building (the exponent). So if you have a 1000-floor building, it can be anywhere along there, but it can still only run over 20 or 30 floors.

This doesn''t map 1:1 because the exponent means there''s a logarithmic relationship between the exponent and the range of the mantissa, but the problem can be stated that if you need more range than the number of floors you can travel, or if you need to get in between floors, you have a range or precision problem.

This topic is closed to new replies.

Advertisement