To give you an overview of why your lecturer answers better, while your answer is not as simple as you think, consider the following:
3.1444440 and 3.1444441
Are they equal? Well, in Java, if you compare them simply with == , then you get false . This is why you should never compare double / float types with == . The best way to compare floating types is to use tolerance value . You want to verify that the result of the subtraction of the two floating-point types is WITHIN, which is the tolerance value. For example, the tolerance value your teacher used is 1e-14 . Therefore, if sum-3.14 is within the acceptable range, then two numbers are considered equal.
Also note that Math.abs gives you an absolute subtraction value, so you will never get a negative number, otherwise you will not get the correct result.
Hope this gives you an overview.
source share