• Post Reply Bookmark Topic Watch Topic
  • New Topic

Maths Problem  RSS feed

 
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question one == what is Math.round
Question two == what is happening here >>   
        x = Math.round(x * 1000);
        x = x / 1000;
****************************************************************************
import java.util.Scanner;
public class Exercise6 {

   
  public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Input floating-point number: ");
        double x = in.nextDouble();
        System.out.print("Input floating-point another number: ");
        double y = in.nextDouble();

        x = Math.round(x * 1000);
        x = x / 1000;

        y = Math.round(y * 1000);
        y = y / 1000;

        if (x == y)
        {
            System.out.println("They are the same up to three decimal places");
        }
        else
        {
            System.out.println("They are different");
        }
    }
}
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think the answers are? You can find out about the methods of the Math class here. There is a hint about your second question in the program.
 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, read the documentation: Math.round(a) rounds a to the nearest integer.
Also note that these two are identical:
So, i
If x=123.4567, then
Math.round(x * 1000) is equal to Math.round(123456.7) which is equal to 123457.
Then dividing that by 1000 gives (approximately) 123.457 which is different than 123.4567.

Note that I say approximately, since neither 1234567 nor 123.457 can be expressed exactly using a double.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!