Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

precision lost

Posted on 2004-09-27
14
Medium Priority
?
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 80 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

596 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