Solved

precision lost

Posted on 2004-09-27
14
247 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
type mismatch (Object[] to double[] 4 33
eclipse buid path vs tomcat lib path 10 34
null output 3 35
Java: The Public Class Main 4 30
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

809 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