Skip to content

New rule: Floating point numbers should not be compared directly #71

Open
@fourls

Description

@fourls

Prerequisites

  • This rule has not already been suggested.
  • This should be a new rule, not an improvement to an existing rule.
  • This rule would be generally useful, not specific to my code or setup.

Suggested rule title

Floating point numbers should not be compared directly

Rule description

This rule would identify instances of a floating point value being checked for equality with = or <>.

Rationale

Two floating point numbers may appear to have the same value, but be slightly different due to rounding errors in calculation. This is unpredictable and leads to buggy code.

The following code would print They are not the same...:

A := (0.3 * 3) + 0.1;
B := 1.0;

if A = B then begin
  WriteLn('They are the same!');
end
else begin
  WriteLn('They are not the same...');
end;

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or requestruleImprovements or additions to rules

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions