Solved

precision lost

Posted on 2004-09-27
14
244 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
Comment Utility
>> 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
Comment Utility
java.lang.Math.round()

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

Accepted Solution

by:
girionis earned 20 total points
Comment Utility
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
Comment Utility
...although you should be able to cast a double to int.
0
 
LVL 35

Expert Comment

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

Expert Comment

by:zzynx
Comment Utility
       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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 86

Expert Comment

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

Expert Comment

by:cjjclifford
Comment Utility
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
Comment Utility
oops, sorry, zzynx, forgot to refresh before submit...
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
Comment Utility
athours = authors
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
Comment Utility
:)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

771 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

10 Experts available now in Live!

Get 1:1 Help Now