posted 11 years ago
System.out.println(5.0 == 5L);
the output is true.
i do not understand why is it so.
5.0 would be treated as a double, where as 5L is treated as a long
so how can comparison of a double and a long be true ??
Pl explain.
Also System.out.println(5.1 == 5.1F);
gives the output as false.
the output is true.
i do not understand why is it so.
5.0 would be treated as a double, where as 5L is treated as a long
so how can comparison of a double and a long be true ??
Pl explain.
Also System.out.println(5.1 == 5.1F);
gives the output as false.
posted 11 years ago
In the case of a double and a long, the long will be promoted to a double.
If both operands are byte, char, short, or int, then both operands are promoted to int before the operation.
If one operand is a long and the other a lesser type, both operands are promoted to long.
If one operand is a float and the other a lesser type, both operands are promoted to float.
If one operand is a double and the other a lesser type, both operands are promoted to double.
If both operands are byte, char, short, or int, then both operands are promoted to int before the operation.
If one operand is a long and the other a lesser type, both operands are promoted to long.
If one operand is a float and the other a lesser type, both operands are promoted to float.
If one operand is a double and the other a lesser type, both operands are promoted to double.
Prasad Shindikar
Ranch Hand
Posts: 114
Keith Lynn
Ranch Hand
Posts: 2410
posted 11 years ago
Some floating point numbers cannot be stored exactly in a computer.
Consider this example.
The output on my machine is
So a better way to compare two floating point numbers would be to look at the absolute value of their difference.
[ March 12, 2007: Message edited by: Keith Lynn ]
Consider this example.
The output on my machine is
So a better way to compare two floating point numbers would be to look at the absolute value of their difference.
[ March 12, 2007: Message edited by: Keith Lynn ]
posted 11 years ago
I tried the above and have given the corresponding result on my machine.
In 1. In the case of Double and Float, (from the results above) there is no promotion taking place.
In 2., 5.1 with no F or D at the end, defaults to Double and therefore false like case 1.
In 3., it is true because 5.1 is actually 5.1D.
I tried the above and have given the corresponding result on my machine.
In 1. In the case of Double and Float, (from the results above) there is no promotion taking place.
In 2., 5.1 with no F or D at the end, defaults to Double and therefore false like case 1.
In 3., it is true because 5.1 is actually 5.1D.
Keith Lynn
Ranch Hand
Posts: 2410
posted 11 years ago
I'm not sure what you mean by no promotion taking place because you have a double and a float.
This is from the Java Language Specification 5.6.2.
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order, using widening conversion (�5.1.2) to convert operands as necessary:
* If any of the operands is of a reference type, unboxing conversion (�5.1.8) is performed. Then:
* If either operand is of type double, the other is converted to double.
* Otherwise, if either operand is of type float, the other is converted to float.
* Otherwise, if either operand is of type long, the other is converted to long.
* Otherwise, both operands are converted to type int.
Binary numeric promotion is performed on the operands of certain operators:
* The multiplicative operators *, / and % (�15.17)
* The addition and subtraction operators for numeric types + and  (�15.18.2)
* The numerical comparison operators <, <=, >, and >= (�15.20.1)
* The numerical equality operators == and != (�15.21.1)
* The integer bitwise operators &, ^, and  (�15.22.1)
* In certain cases, the conditional operator ? : (�15.25)
The reason 5.1 prints out exactly in the last two statements is that there is no conversion taking place.
But consider this example.
The output is
This is from the Java Language Specification 5.6.2.
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order, using widening conversion (�5.1.2) to convert operands as necessary:
* If any of the operands is of a reference type, unboxing conversion (�5.1.8) is performed. Then:
* If either operand is of type double, the other is converted to double.
* Otherwise, if either operand is of type float, the other is converted to float.
* Otherwise, if either operand is of type long, the other is converted to long.
* Otherwise, both operands are converted to type int.
Binary numeric promotion is performed on the operands of certain operators:
* The multiplicative operators *, / and % (�15.17)
* The addition and subtraction operators for numeric types + and  (�15.18.2)
* The numerical comparison operators <, <=, >, and >= (�15.20.1)
* The numerical equality operators == and != (�15.21.1)
* The integer bitwise operators &, ^, and  (�15.22.1)
* In certain cases, the conditional operator ? : (�15.25)
The reason 5.1 prints out exactly in the last two statements is that there is no conversion taking place.
But consider this example.
The output is
I am mighty! And this is a mighty small ad:
Rocket Oven Kickstarter  from the trailboss
https://coderanch.com/t/695773/RocketOvenKickstartertrailboss
