Solved

precision lost

Posted on 2004-09-27
14
246 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 85
print map entry 34 69
servlet example issue 6 40
Error in @AspectJ Based AOP with Spring 2 13
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

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