Fab
Fab

Reputation: 1215

JAVA - Comparing a Double value with Float.MIN_VALUE or Integer.MIN_VALUE

I have the following issue.

I have some data stored in memory (coming from a database query) by using a hash table:

Map<String,MyObject>

where MyObject consists of 3 arrays: float[], int[] and double[]. I use Float.MIN_VALUE, Integer.MIN_VALUE and Double.MIN_VALUE to store values having a NULL value in the database.

I need to process the input hash table and store the output in a hash table

Map<String, Double> output

which must contain only NON NULL values.

So, I perform a comparison like this:

Double value = ... (get a value from MyObject)
if(value != Float.MIN_VALUE && value != Double.MIN_VALUE && value != Integer.MIN_VALUE){
    // add to OUTPUT hash table

but the if statement doesn't work properly when value equals to Float.MIN_VALUE: the first component always returns TRUE (meaning the two values are different).

I also tried something like that

if(value != new Double(Float.MIN_VALUE))

but the problem is still there.

Anyone could suggest me a solution to properly compare the values?


EDIT: Is it safe to use something like that:

String.valueOf(value).equals(String.valueOf(Float.MIN_VALUE))) ?

It seems to work.

Upvotes: 1

Views: 634

Answers (2)

Meziane
Meziane

Reputation: 1669

This post ist 9 months old, but my response may help other users having to do with doubles and floats. Fab I don't know how you get a float/double/int value from your Object, but as you kann check it here: If an Object gets the Float.MIN_VALUE assigned, its value gotten again as float or as double will surely match Float.MIN_VALUE:

Object obj =  Float.MIN_VALUE; //1.40129846432481707E-45;
float floatVal = ((Number) obj).floatValue();

System.out.println("obj is a " + obj.getClass().getSimpleName()); // obj is a Float 
boolean result  = floatVal == Float.MIN_VALUE;
System.out.printf("%.60f == %.60f: %s.\n" , floatVal, Float.MIN_VALUE, result ); // true

Even if we assign the value gotten from the object to a variable declared as double, the match still holds true:

    double doubleVal = ((Number) obj).doubleValue();
    result  = doubleVal == Float.MIN_VALUE;
    System.out.printf("%.60f == %.60f: %s.\n" , doubleVal, Float.MIN_VALUE, result ); //true

If we assign the value of Float.MIN_VALUE not explicitly as a Float:

obj =  Float.MIN_VALUE; //1.40129846432481707E-45;

The macths till holds, even though the value of obj is interpreted as a Double:

floatVal = ((Number) obj).floatValue();
doubleVal = ((Number) obj).doubleValue();
Formatter fmt = new Formatter(); 

System.out.println("obj is a " + obj.getClass().getSimpleName()); // obj is a Double
result  = floatVal == Float.MIN_VALUE;
System.out.println(fmt.format("%18.17e", floatVal) + " == " + fmt.format("%18.17e", Float.MIN_VALUE) + "? " + result ); // true
result  = doubleVal == Float.MIN_VALUE;
System.out.println(fmt.format("%18.17e", doubleVal) + "= = " + fmt.format("%18.17e",Float.MIN_VALUE) + "? " + result ); // true

I hope this will help someone!

Upvotes: 0

You should use the static methods the double class is offering:

Double.isFinite(value);
Double.isInfinite(value);
Double.isNaN(value);
Double.compare(value, Double.MIN_VALUE)

Upvotes: 2

Related Questions