my dog learned polymorphism
The moose likes Beginning Java and the fly likes minimum float/double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "minimum float/double" Watch "minimum float/double" New topic

minimum float/double

Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Hi all!
I am extending JTextField to allow only certain types and ranges of input (for example, only allow integers in the range 0-255).
If no range is specified in the constructor, I want the default range to be the range allowed by the type. For integer, I have used Integer.MIN_VALUE and Integer.MAX_VALUE to get this range.
When I get to float and double, this won't work because Float.MIN_VALUE returns the smallest positive value of type float instead of smallest overall value. Same for Double.MIN_VALUE.
Does anyone know of methods I could call to obtain the minimum values for float and double, or must I hardcode it?
Any advice is much appreciated.
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Stephanie.
Can't you just refer to -Float.MAX_VALUE and -Double.MAX_VALUE? According to Java in a Nutshell, the range for floats is
+/-1.4E-45 to +/-3.4028235E+38
and for doubles is
+/-4.9E-324 to +/-1.7976931348623157E+308.
So if you're looking for the maximum negative value (in magnitude), I think it'd be safe to simply use the negative of MAX_VALUE. Note, for the integral cases (byte, short, etc.), this approach does not apply.
Is this what you're looking for?
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Thanks for your input.
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
I think the absolute value of the Minimun for each type is one more than the maximum. For example, byte range is -128 to 127.
So, you may want to add 1 to Max before you use the negative of it.
[This message has been edited by herb slocomb (edited March 21, 2001).]
I agree. Here's the link:
subject: minimum float/double
It's not a secret anymore!