rspec/rules/S1244/description.adoc

17 lines
895 B
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
Floating point math is imprecise because of the challenges of storing such values in a binary representation. Even worse, floating point math is not associative; push a ``++float++`` or a ``++double++`` through a series of simple mathematical operations and the answer will be different based on the order of those operation because of the rounding that takes place at each step.
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
Even simple floating point assignments are not simple:
2020-06-30 12:47:33 +02:00
----
float f = 0.1; // 0.100000001490116119384765625
double d = 0.1; // 0.1000000000000000055511151231257827021181583404541015625
----
(Results will vary based on compiler and compiler settings.)
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
Therefore, the use of the equality (``++==++``) and inequality (``++!=++``) operators on ``++float++`` or ``++double++`` values is almost always an error.
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
This rule checks for the use of direct and indirect equality/inequality tests on floats and doubles.