Slow floating point comparisons

Hi,

I've noticed that there's a strange performance cap when you use floating points numbers close to the maximum of double/float.

Here my testcode for floats:

float min = 1.4e-38f;

// float min = 1.4e-39f; < evil

float max = 5;

float test = 1.499f;

boolean b;

long start = System.currentTimeMillis();

for (int i = 0; i < 10000000; i++){

b = (test >= min && test <= max);

}

long end = System.currentTimeMillis();

System.out.println(end - start);

This will run quite fast. But if you change min to "1.4e-39f" it will about as 20 times slower.

Same with doubles:

double min = 4.9e-308;

// double min = 4.9e-309; < evil

double max = 5;

double test = 1.499;

boolean b;

long start = System.currentTimeMillis();

for (int i = 0; i < 10000000; i++){

b = (test >= min && test <= max);

}

long end = System.currentTimeMillis();

System.out.println(end - start);

With min=4.9e-308 it's fast, with min=4.9e-309 it's about 20 times slower.

If you start the vm with "-server" everything runs as fast as expected. First of all, can anyone tell me why there's this performance gap? And what exactly does "-server" to increase the performance like this?!

Thanks

Stefan

[2078 byte] By [slottiesa] at [2007-10-3 5:26:36]
# 1

Read this: [url=http://cch.loria.fr/documentation/IEEE754/ACM/goldberg.pdf#search=%22what%20every%20scientist%22]What every computer scientist should know about floating point arithmetic[/url].

Your 'evil' numbers are denormalized numbers and can cause quite a

bit of shifting of the mantissa before they can be compared to normalized

numbers. If I'm not mistaken (don't take my word for it) they can even

cause an internal trap that could propagate outwards.

kind regards,

Jos

JosAHa at 2007-7-14 23:33:52 > top of Java-index,Java Essentials,Java Programming...