rspec/rules/S2127/java/rule.adoc

53 lines
1.5 KiB
Plaintext

== Why is this an issue?
`Double.longBitsToDouble` converts the bit pattern into its corresponding floating-point representation.
The method expects a 64-bit long argument to interpret the bits as a double value correctly.
When the argument is a smaller data type, the cast to `long` may lead to a different value than expected due to
the interpretation of the most significant bit, which, in turn, results in `Double.longBitsToDouble` returning an incorrect value.
=== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
----
int i = 0x80003800;
Double.longBitsToDouble(i); // Noncompliant - NaN
----
=== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
long i = 0x80003800L;
Double.longBitsToDouble(i); // Compliant - 1.0610049784E-314
----
== Resources
=== Documentation
* https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/Double.html#doubleToLongBits(double)[Oracle Java SE - Double.doubleToLongBits]
=== Articles & blog posts
* https://en.wikipedia.org/wiki/Double-precision_floating-point_format[Wikipedia - Double Precision floating point format]
* https://en.wikipedia.org/wiki/Single-precision_floating-point_format[Wikipedia - Single Precision floating point format]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this "Double.longBitsToDouble" call.
'''
== Comments And Links
(visible only on this page)
=== on 10 Oct 2014, 14:07:37 Freddy Mallet wrote:
Perfect !
endif::env-github,rspecator-view[]