one question regarding double comparison in Java

B

Bo Sun

hi,

I have the following piece of code:

public class test
{
//-----------------------------------------------------------------
// Reads the number of hours worked and calculates wages.
//-----------------------------------------------------------------
public static void main (String[] args)
{

double a = 1.0;

if (a == 1.0)
System.out.println("Hello");


}
}


why Hello is printed out? since ?i am comparing two double values?

Thanks in advance,

Bo
 
V

VisionSet

double a = 1.0;

if (a == 1.0)
System.out.println("Hello");
why Hello is printed out? since ?i am comparing two double values?

1.0 literal is of type double
variable a is of type double, simple assignment of 1.0D to a
is double a equal to literal double 1.0 ?
yes of course
 
Z

zero

hi,

I have the following piece of code:

public class test
{
//-----------------------------------------------------------------
// Reads the number of hours worked and calculates wages.
//-----------------------------------------------------------------
public static void main (String[] args)
{

double a = 1.0;

if (a == 1.0)
System.out.println("Hello");


}
}


why Hello is printed out? since ?i am comparing two double values?

Thanks in advance,

Bo

You probably read somewhere that you shouldn't rely on the == operator to
compare doubles. This is very true. An example, with arbitrary double
values:

double d = 64.9963;
double d1 = Math.pow(d, 34.591);
double d2 = Math.pow(d1, 1.0/34.591);
if(d2 == d)
System.out.println(d + " == " + d2);
else
System.out.println(d + " != " + d2);

Mathematically speaking, this should print

64.9963 == 64.9963

However, on my test case, it printed:

64.9963 != 64.99630000000002

This is because of rounding errors when comparing floating point numbers to
binary. If you want more information about this, google for "floating
point comparison".

In your case however, there is no rounding error. You are comparing the
exact value 1.0 with the exact value 1.0 - and these are of course equal.

In general when comparing floating point numbers you should use an error
margin instead of comparing them directly - but this does not mean all
pairs of floating point numbers are unequal by definition.
 
G

Googmeister

zero said:
hi,

I have the following piece of code:

public class test
{
//-----------------------------------------------------------------
// Reads the number of hours worked and calculates wages.
//-----------------------------------------------------------------
public static void main (String[] args)
{

double a = 1.0;

if (a == 1.0)
System.out.println("Hello");


}
}


why Hello is printed out? since ?i am comparing two double values?

Thanks in advance,

Bo

double d = 64.9963;
double d1 = Math.pow(d, 34.591);
double d2 = Math.pow(d1, 1.0/34.591);
if(d2 == d)
System.out.println(d + " == " + d2);
else
System.out.println(d + " != " + d2);

Mathematically speaking, this should print

64.9963 == 64.9963

However, on my test case, it printed:

64.9963 != 64.99630000000002

This is because of rounding errors when comparing floating point numbers to
binary. If you want more information about this, google for "floating
point comparison".

There's no rounding error when comparing floating point values. The
roundoff error occurs in several places prior to the comparison. First
the numbers 64.9963, 34.591, 1.0/34.591 are not representable using
double in Java, so they are approximated by the nearest IEEE floating
point value. Second, using Math.pow leads to more roundoff error, again
because the result here is not representable using double.
 
Z

zero

hi,

I have the following piece of code:

public class test
{
//---------------------------------------------------------------
-- // Reads the number of hours worked and calculates wages.
//---------------------------------------------------------------
-- public static void main (String[] args)
{

double a = 1.0;

if (a == 1.0)
System.out.println("Hello");


}
}


why Hello is printed out? since ?i am comparing two double values?

Thanks in advance,

Bo

double d = 64.9963;
double d1 = Math.pow(d, 34.591);
double d2 = Math.pow(d1, 1.0/34.591);
if(d2 == d)
System.out.println(d + " == " + d2);
else
System.out.println(d + " != " + d2);

Mathematically speaking, this should print

64.9963 == 64.9963

However, on my test case, it printed:

64.9963 != 64.99630000000002

This is because of rounding errors when comparing floating point
numbers to binary. If you want more information about this, google
for "floating point comparison".

There's no rounding error when comparing floating point values. The
roundoff error occurs in several places prior to the comparison. First
the numbers 64.9963, 34.591, 1.0/34.591 are not representable using
double in Java, so they are approximated by the nearest IEEE floating
point value. Second, using Math.pow leads to more roundoff error,
again because the result here is not representable using double.

Right, I meant "rounding errors when converting floating point numbers
to binary."

Sorry for the confusion
 
P

P.Hill

Roedy said:
Some double values are exact. Some are not. See


Curiously enough, it doesn't matter if the double of this particular
number is exact, because in this trivial case, the same exact or inexact
conversion to a binary double will occur, so the result will be the
same. Many if not most real uses of results from some a series of
calculations will exhibit the problem inexact representation.

I guess this shows a danger of testing with top simplistic a case.

-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top