programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Geeky Math Question

Robert Wright
Greenhorn
Posts: 19
Consider the following:
System.out.println(new BigDecimal(Float.MAX_VALUE));
produces the following:
340 282 346 638 528 859 811 704 183 484 516 925 440 (spaces added by me)
the line:
System.out.println(new BigDecimal(Integer.MAX_VALUE));
produces the following :
2 147 483 647 (spaces added)
Why is Float.MAX_VALUE so much larger than Integer.MAX_VALUE? Both are signed, 32 bit numbers, it seems that they would be about the same with Float holding a little less information since it has to keep track of where its decimal point is. This makes no sense to me.
This does come from a practical application, I am writing a routine to automatically extract size and precision information from db2 and then set up appropriate variables to receive the data.

Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Hi Robert,
You know what scientific notation is? Numbers that look like 1.3 x 10^3? The "1.3" part is called the "mantissa" and the "3" is the exponent. The value of the number is the mantissa times 10 raised to the power of the exponent.
Floating-point numbers are stored this same way (except instead of 10, they use 2 raised to the power of the exponent.). Some of the bits are used to store the mantissa, and some are used for the exponent. As a result, you can store really big numbers, but with less than 32 bits of precision.
Integers, on the other hand, are basically all mantissa. You get the full 32 bits of precision, but the range is smaller.
Make sense?

Michael Chermside
Greenhorn
Posts: 11
Robert:
Another good thing to consider is doing the following:
long lo = Long.MAX_VALUE;
float fl = Float.MAX_VALUE;
lo = lo + -1;
fl = fl + -1;
System.out.println("lo = " + lo);
System.out.println("fl = " + fl);
Does this do what you expected? If not, then you probably don't
understand how floating point works.