Skip to content

BUG: Fix float-precision division in BresenhamLine::BuildLine#6057

Merged
hjmjohnson merged 1 commit intoInsightSoftwareConsortium:mainfrom
hjmjohnson:fix-bresenham-float-precision
Apr 14, 2026
Merged

BUG: Fix float-precision division in BresenhamLine::BuildLine#6057
hjmjohnson merged 1 commit intoInsightSoftwareConsortium:mainfrom
hjmjohnson:fix-bresenham-float-precision

Conversation

@hjmjohnson
Copy link
Copy Markdown
Member

Fix float-precision division in BuildLine(Direction, length). The expression IdentifierType / float was evaluating in float precision despite storing the result in a double. Adds explicit static_cast<double>() on both operands.

Companion to the release-5.4 backport in #6054 where this was caught by Greptile review.

Add explicit static_cast<double>() on both operands of the division
computing euclideanLineLen. Because LType = Vector<float>, the
expression `IdentifierType / float` previously evaluated in float
precision; the double result was only a widened float quotient.

Assisted-by: Greptile — identified float-precision division in BuildLine
@github-actions github-actions Bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances area:Core Issues affecting the Core module labels Apr 14, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 14, 2026

Greptile Summary

This PR fixes a floating-point precision bug in BresenhamLine::BuildLine(Direction, length) where the division IdentifierType / float (from LType = Vector<float>) was evaluated in float precision before being stored in a double. The fix adds explicit static_cast<double>() on both operands, ensuring the division is performed at double precision.

Confidence Score: 5/5

  • This PR is safe to merge — it is a minimal, correct precision fix with no side effects.
  • The change is a single targeted cast on both sides of one division. LType = Vector<float> confirms the original operand types (IdentifierType / float), so the fix is correct and complete. No other code paths are affected. All remaining considerations are informational only (no P0/P1 findings).
  • No files require special attention.

Important Files Changed

Filename Overview
Modules/Core/Common/include/itkBresenhamLine.hxx Adds explicit static_cast<double>() on both operands of the division to ensure double-precision arithmetic instead of float-precision. LType = Vector<float>, so without the cast the division was IdentifierType / float → float, then implicitly widened to double with precision already lost.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["mainDirectionLen\n(IdentifierType / integer)"]
    B["Direction[maxDistanceDimension]\n(float, from LType = Vector&lt;float&gt;)"]
    C["itk::Math::Absolute(...)"]

    subgraph before["Before fix (float precision)"]
        D["integer ÷ float\n→ float result"]
        E["Implicit widen to double\n(precision already lost)"]
        D --> E
    end

    subgraph after["After fix (double precision)"]
        F["static_cast&lt;double&gt;(mainDirectionLen)"]
        G["static_cast&lt;double&gt;(Absolute(...))"]
        H["double ÷ double\n→ double result (full precision)"]
        F --> H
        G --> H
    end

    A --> D
    B --> C --> D
    A --> F
    B --> C --> G

    E --> R1["euclideanLineLen (degraded)"]
    H --> R2["euclideanLineLen (correct)"]
Loading

Reviews (1): Last reviewed commit: "BUG: Fix float-precision division in Bre..." | Re-trigger Greptile

@hjmjohnson hjmjohnson merged commit 5eb6cad into InsightSoftwareConsortium:main Apr 14, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants