0

So I have this piece of code that prompts for space delimited input of the two sides and the hypotenuse of a triangle. The method below main is supposed to return true if the triangle is right, false if not.

For some reason, it still prints that a triangle is not a right one even if I know the measurments I entered are for a right triangle.

I've been trying to detect a logic error in this code for a while, can comeone help/call me out?

import java.util.Scanner;

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

    System.out.print("Please enter the two sides and the hypotenuse: ");
    String input = sc.nextLine();

    String[] values = input.split(" ");

    int[] nums = new int[values.length];

    for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(values[i]);
    }

    double s1 = nums[0];
    double s2 = nums[1];
    double hyp = nums[2];

    System.out.printf("Side 1: %.2f Side 2: %.2f Hypotenuse: %.2f\n", s1, s2, hyp);

    boolean result = isRightTriangle(s1, s2, hyp);

    if (result == true) {
        System.out.println("The given triangle is a right triangle.");
    } else if (result == false) {
        System.out.println("The given triangle is not a right triangle.");
    }
}

/**
 * Determine if the triangle is a right triangle or not, given the shorter side, the longer
 * side, and the hypotenuse (in that order), using the Pythagorean theorem.
 * @param s1 the shorter side of the triangle
 * @param s2 the longer side of the triangle
 * @param hyp the hypotenuse of the triangle
 * @return true if triangle is right, false if not
 */

private static boolean isRightTriangle(double s1, double s2, double hyp) {
    double leftSide = s1 * s1 + s2 * s2;
    double rightSide = hyp * hyp;
    if (Math.sqrt(leftSide) == Math.sqrt(rightSide)) {
        return true;
    } else {
        return false;
    }
}

}

7
  • 2
    Floating points are not precise, and you introduce more error by taking square roots. There will be inputs that work, and inputs that introduce errors from the floating point operations. If you allow only integer inputs, then your algorithm should work if you remove the sqrt call before comparison. Commented Sep 2, 2012 at 17:08
  • sqrt is not necessary anyway,you can just compare leftSide and rightSide. Commented Sep 2, 2012 at 17:11
  • 2
    No need to take the Math.sqrt; if it's a right triangle leftSide == rightSide. Commented Sep 2, 2012 at 17:12
  • can you provide the sample output? Commented Sep 2, 2012 at 17:25
  • 1
    Can you provide an example of where your method doesn't work, because I tried a few example and it didn't have a problem. (See my answer) Commented Sep 2, 2012 at 17:35

3 Answers 3

1

Floating point calculations are not precise, thats why the numbers never match, try using BigDecimal instead of double

Edit: On second thoughts as you are already parsing integer nums[i] = Integer.parseInt(values[i]);

simply use int in place of double and don't use Math.sqrt, simply use leftSide==rightside

Sign up to request clarification or add additional context in comments.

4 Comments

Good catch there, didn't see he was only using ints for input.
For all possible int values using double is precise.
@PeterLawrey ya you are right, then why do think its not working? Math.sqrt should also work fine for integers.
sqrt isn't precise for integers (except squares), but it should be consistently imprecise.
1

You have to check for s1*s1 + s2*s2 == hyp*hyp without using sqrt. See pythagorean theorem.

Comments

1

Given the inputs are int values you shouldn't have an error. If I test

public static void main(String... args) {
    for (int i = 3; i <= 11; i += 2) {
        int side1 = (i * i - 1) / 2;
        int side2 = side1 + 1;
        System.out.println(i + "," + (side1 - 1) + " and " + (side2 - 1) + " is " + isRightTriangle(i, side1 - 1, side2 - 1));
        System.out.println(i + "," + side1 + " and " + side2 + " is " + isRightTriangle(i, side1, side2));
        System.out.println(i + "," + (side1 + 1) + " and " + (side2 + 1) + " is " + isRightTriangle(i, side1 + 1, side2 + 1));
    }
}

it prints

3,3 and 4 is false
3,4 and 5 is true
3,5 and 6 is false
5,11 and 12 is false
5,12 and 13 is true
5,13 and 14 is false
7,23 and 24 is false
7,24 and 25 is true
7,25 and 26 is false
9,39 and 40 is false
9,40 and 41 is true
9,41 and 42 is false
11,59 and 60 is false
11,60 and 61 is true
11,61 and 62 is false

which is correct.

Can you provide a use case where this method fails?


Try this instead to allow for a small error.

private static boolean isRightTriangle(double s1, double s2, double hyp) {
    double leftSide = s1 * s1 + s2 * s2;
    double rightSide = hyp * hyp;
    return Math.abs(leftSide - rightSide) < 1e-9;
}

if the sqrt of two values are equal, then the two values are equal.

Math.sqrt() has some error, so if you can avoid it, you should.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.