Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Comparing Doubles?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Krocker




PostPosted: Tue Sep 17, 2013 2:39 pm   Post subject: Comparing Doubles?

hi, ok so im doing an assignment that requires the program to check two numbers for similarity up to two decimal places. However, i have no idea how to do it. All i know is that i need to use a for statement, but i dont know how to compare the two values (doubles) to two decimal places. can someone please help me?
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Sep 17, 2013 2:44 pm   Post subject: RE:Comparing Doubles?

How would you solve the problem on paper? What are the exact requirements?

Hint: you shouldn't need a for statement.
Krocker




PostPosted: Tue Sep 17, 2013 6:00 pm   Post subject: RE:Comparing Doubles?

srry i meant an if statment. its easy on paper, i would compare it in my mind, but i need java to do it, im not sure how to make it though
Insectoid




PostPosted: Tue Sep 17, 2013 6:05 pm   Post subject: RE:Comparing Doubles?

There's a mathematical solution to this.

Hint: Math.floor() will cut off the decimal portion of a number.
Krocker




PostPosted: Tue Sep 17, 2013 6:44 pm   Post subject: RE:Comparing Doubles?

math.floor() rounds the value and converts it to an int value, however, i need to compare the values to the 2nd decimal place. I was also thinking about math.ceil, but that doesnt work aswell
Zren




PostPosted: Tue Sep 17, 2013 7:16 pm   Post subject: RE:Comparing Doubles?

Math.floor() cuts off all the values after the decimal. So you have part of the solution (removing precision). Now we just have to cut it off at the right place. Is it possible to move the decimal place?
tiedye1




PostPosted: Tue Sep 17, 2013 7:32 pm   Post subject: RE:Comparing Doubles?

Hint: it is. (Think scientific notation)
Krocker




PostPosted: Thu Sep 19, 2013 10:59 am   Post subject: Re: Comparing Doubles?

ok so i converted the doubles to scientific notation, but when i compare the values, it is always saying they are not equal, even when they are. Im getting frustrated now. lol. Heres my code:

code:

import java.util.Scanner;
import java.text.DecimalFormat;

public class question1{
        public static void main (String[] args){
                Scanner userInput = new Scanner (System.in);
               
                System.out.print ("Please enter two double values: ");
                double inValue1 = userInput.nextDouble();       
                double inValue2 = userInput.nextDouble();              
               
                System.out.println ("Sum: "+ (inValue1 + inValue2));
                System.out.println ("Average: "+ (inValue1 + inValue2)/2);
                System.out.println ("Max: "+ Math.max(inValue1, inValue2));
                System.out.println ("Min: "+ Math.min(inValue1, inValue2));
               
                DecimalFormat df = new DecimalFormat("0.00E0"); 
                System.out.println(df.format(inValue1));   
                System.out.println(df.format(inValue2));
               
                if (df.format(inValue1)== df.format(inValue2)) {
                        System.out.println ("Numbers are equal to two decimal places.");
                } else if (df.format(inValue1)!= df.format(inValue2)) {
                        System.out.println ("Numbers are NOT equal to two decimal places.");
                }
               
                       
        }       
}
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Sep 19, 2013 3:42 pm   Post subject: RE:Comparing Doubles?

You should probably think about why you're doing something before you do it. Just 'cause someone says 'scientific notation' doesn't mean you have to convert it to scientific notation. The concept of scientific notation and the process of converting a number to scientific notation (on paper, not code) are certainly related subjects.
Zren




PostPosted: Thu Sep 19, 2013 6:17 pm   Post subject: Re: Comparing Doubles?

Multiply or divide a number by 10 to "move" the decimal. Scientific notation uses exponents just to simplify things.

code:

123.456
1234.56 = 123.456 * 10
1234.56 = 123.456 * 10 ^ 1

123.456
12345.6 = 123.456 * 100
12345.6 = 123.456 * 10 * 10
12345.6 = 123.456 * 10 ^ 2

123.456
12.3456 = 123.456 / 10
12.3456 = 123.456 * (1 / 10)
12.3456 = 123.456 * (1 / 10^1)
12.3456 = 123.456 * 10 ^ -1

1234.5678
12345.678 = 1234.5678 * 10 ^ 1
123456.78 = 1234.5678 * 10 ^ 2
1234567.8 = 1234.5678 * 10 ^ 3

1234.5678
123.45678 = 1234.5678 * 10 ^ -1
12.345678 = 1234.5678 * 10 ^ -2
1.2345678 = 1234.5678 * 10 ^ -3


Some music to accompany this magic: http://www.youtube.com/watch?v=cb6pJ4AEOoI
Krocker




PostPosted: Sat Sep 28, 2013 8:20 am   Post subject: Re: Comparing Doubles?

ok, so im totally lost here,
andrew.




PostPosted: Sun Sep 29, 2013 1:52 pm   Post subject: RE:Comparing Doubles?

What Zren is saying is that you can multiply or divide your numbers by 10 to move the decimal point.

I'll give you a big hint. Let's say you wanted to compare 123.451 and 123.452 to two decimal places. We could multiply both numbers by 100 to shift the decimal two places and get 12345.1 and 12345.2. Now the decimal is moved and they should be equal in this case. What can you do now to make these two numbers equal?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: