Relational Operators - Floating Point Precision and Truncation


#1

Hello, I was working through the exercises (see link below) and started experimenting with the behavior of the relational operator.

https://www.codecademy.com/courses/learn-java/lessons/introduction-to-java/exercises/relational-operators?action=lesson_resume

In the System.out.println statement below, the floating point number to the left of the less-than-or-equal-to operator has exactly 16 digits to the right of the decimal point. It is in theory larger than 10. However, it is evaluated and printed as TRUE. When I remove a zero, now consisting of only 15 digits to the right of the decimal point, the result is evaluated as FALSE.

public class RelationalOperators {
	public static void main(String[] args) {
		System.out.println(10.0000000000000001 <= 10);
	}
}

In the code snippet below, I changed the less-than-or-equal-to operator to a less-than operator. Now the result evaluates to FALSE no matter how many zeros are added or removed, which is correct. So, in the above code, with the less-than-or-equal-to operator, the precision is for some reason being truncated and appears to be saying that 10.0000000000000001 is equal to 10? So confused…

public class RelationalOperators {
	public static void main(String[] args) {
		System.out.println(10.0000000000000001 < 10);
	}
}

I am curious as to what is going on in the background that explains this behavior. Does anybody know? Is this in relation to the IEEE Standard for Floating-Point Arithmetic (IEEE 754)?

I hope others find this as interesting as I do.

Best,

-Qrev


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.