Solved

precision lost

Posted on 2004-09-27
14
245 Views
Last Modified: 2012-05-05
Suppose I have two double values that I wanna compare them for equality. How can I lose the precision so that it returns true once all numbers before the dot are equal (ignoring the decimal)? I cant conver them to int becuz it reports double value cannot be dereferenced error.

0
Comment
Question by:jtcy
  • 6
  • 3
  • 3
  • +2
14 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12160123
>> I cant conver them to int becuz it reports double value cannot be dereferenced error.
???

Can you give you code snippet?
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12160133
java.lang.Math.round()

long a = Math.round( 10.0001 );
0
 
LVL 35

Accepted Solution

by:
girionis earned 20 total points
ID: 12160145
Try to convert it to float and then cast it to int:

double d = 1.2345;
float f = new Double(d).floatValue();
System.out.println((int) f);
0
 
LVL 35

Expert Comment

by:girionis
ID: 12160159
...although you should be able to cast a double to int.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12160235
I think he wasn't using the cast...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12160289
       double d1 = 1.2345;
        double d2 = 1.5432;
        Double dOne = new Double(d1);
        Double dTwo = new Double(d2);
        int i1 = (int)dOne.doubleValue();
        int i2 = (int)dTwo.doubleValue();
        System.out.println("i1=" + i1 + " - i2=" + i2);

Nicely prints:
  i1=1 - i2=1
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12160309
And this too:

        double d1 = 1.2345;
        double d2 = 1.5432;
        int i1 = (int)d1;
        int i2 = (int)d2;
        System.out.println("i1=" + i1 + " - i2=" + i2);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12160356
if ((int)d1 == (int)d2) {
    // do it
}
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12160357
Sorry, meant to say Math.floor(), as Math.round() may round up as well as down, but Math.floor() will return the lower, even when Math.round() goes up (e.g. for 1.51 Math.round() returns 2, Math.floor() returns 1).

So...

double a = 1.2;
double b = 1.51;
System.out.println( "a = " + Math.floor( a ) );
System.out.println( "b = " + Math.floor( b ) );

will print:

a = 1
b = 1

cleaner than wrapping, and casting...

btw, zzynx, why do you need the Double object, as the call to doubleValue() simply returns the "double" primitive - you should have just cast "d1" and "d2" directly....
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12160370
oops, sorry, zzynx, forgot to refresh before submit...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12160409
>> zzynx, why do you need the Double object
When question athours talk about "a double" you're never sure if it is a Double object or a double.
That's why I gave it for both situations.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12160417
athours = authors
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12160480
jtcy, can you explain why you choose that comment instead of the more "intuitive"/"logical"

       int i = (int)dOne.doubleValue();

?
0
 
LVL 35

Expert Comment

by:girionis
ID: 12163772
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 76
solarwind tftp server 2 44
java 8 lambda expresssions exception handling 3 72
Apps blocked by Java 9 55
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now