Skip to content

0 < double.Epsilon checks return false #1924

@pantosh

Description

@pantosh

Several expressions in OxyPlot make a zero value check using the idiom: value < double.Epsilon.
For example the range check in CalculateActualInterval in Axis.cs

The problem is that 0 < double.Epsilon is not a safe comparison in .NET. It may evaluate to false, if double.Epsilon is expressed as 0 by the compiler.
The consequence in Axis.CalculateActualInterval is that it gets stuck in an infinite while(true) loop later in the method.

            // This is the actual code. It does not throw if range == 0
            if (Math.Abs(range) < double.Epsilon)
            {
                throw new ArgumentException("Range cannot be zero.", "range");
            }

            // This does not throw either
            if (range < double.Epsilon && range > -double.Epsilon)
            {
                throw new ArgumentException("Range cannot be zero.", "range");
            }

               // This throws!!!
            if (range == 0)
            {
                throw new ArgumentException("Range cannot be zero.", "range");
            }

The issue is observed with Visual Studio Pro 2022 17.2.6 using an x64 build, target .NET 4.8.

Setting ActualMaximum to ActualMinimum and forcing a CalculateActualInterval will reproduce the issue.
ActualMaximum / ActualMinimum needs to be set indirectly, not quite sure how. Perhaps Zoom(0) will do the trick.

Suggestion:
Change all value < double.Epsilon to simply value == 0 or at least value <= double.Epsilon

The value < double.Epsilon idiom occurs several places in the codebase.

Steps to reproduce

  1. Setting ActualMaximum to ActualMinimum and forcing a CalculateActualInterval will reproduce the issue.
    ActualMaximum / ActualMinimum needs to be set indirectly, not quite sure how. Perhaps Zoom(0) will do the trick.

Platform:
.NET version: 4.8

Expected behaviour

Exception thrown for range == 0

Actual behaviour

Oxyplot gets stuck in infinite loop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions