[ 
https://issues.apache.org/jira/browse/NUMBERS-161?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17356430#comment-17356430
 ] 

Matt Juntunen commented on NUMBERS-161:
---------------------------------------

Also, here are some unit tests that I would like to have pass:

{code:java}
    @Test
    void testNormalize_retainsInputPrecision() {
        final double aboveZero = Math.nextUp(0);
        final double belowZero = Math.nextDown(0);

        Assertions.assertEquals(aboveZero,
                
Angle.Rad.normalizer(Angle.Rad.ZERO).apply(Angle.Rad.of(aboveZero)).getAsDouble());
        Assertions.assertEquals(aboveZero,
                
Angle.Rad.normalizer(Angle.Rad.PI).apply(Angle.Rad.of(aboveZero)).getAsDouble());

        Assertions.assertEquals(belowZero,
                
Angle.Rad.normalizer(Angle.Rad.ZERO).apply(Angle.Rad.of(belowZero)).getAsDouble());
        Assertions.assertEquals(0,
                
Angle.Rad.normalizer(Angle.Rad.PI).apply(Angle.Rad.of(belowZero)).getAsDouble());
    }

    @Test
    void testNormalize_preciseLowerBound() {
        final double x = Math.PI / 3;
        final double above = Math.nextUp(x);
        final double below = Math.nextDown(x);

        final double center = x + Math.PI;

        final UnaryOperator<Angle.Rad> normalizer = 
Angle.Rad.normalizer(Angle.Rad.of(center));

        Assertions.assertEquals(x, 
normalizer.apply(Angle.Rad.of(x)).getAsDouble());
        Assertions.assertEquals(above, 
normalizer.apply(Angle.Rad.of(above)).getAsDouble());
        Assertions.assertEquals(below + (2 * Math.PI), 
normalizer.apply(Angle.Rad.of(below)).getAsDouble());
    }
{code}

For the first case, we should check if the input already meets the requirements 
of the range before performing any operations on it. That will retain the 
precision of the input.

The second case is something that I would like to have in order to guarantee 
that normalized values are strictly greater than or equal to a specified lower 
bound. This will require another normalization method that accepts the lower 
bound directly.  The center normalization method can be implemented in terms of 
this new method, since the lower bound is the value that is actually stored in 
the {{Reduce}} class.

> PlaneAngle Numerical Accuracy
> -----------------------------
>
>                 Key: NUMBERS-161
>                 URL: https://issues.apache.org/jira/browse/NUMBERS-161
>             Project: Commons Numbers
>          Issue Type: Improvement
>            Reporter: Matt Juntunen
>            Assignee: Gilles Sadowski
>            Priority: Major
>
> The current {{PlaneAngle}} class introduces unnecessary round-off errors 
> during use since it internally converts all values to turns. For example, it 
> is currently impossible to represent the radians value {{Double.MIN_NORMAL}} 
> exactly since the internal multiplication introduces errors: 
> {{PlaneAngle.ofRadians(Double.MIN_NORMAL).toRadians()}} does not equal 
> {{Double.MIN_NORMAL}}. We should restructure this API so that values are 
> stored exactly and multiplication is only performed when required.
> This issue was discovered during work on GEOMETRY-124. Use of the new 
> {{Reduce}} class added two more floating point operations to angle 
> normalization and caused a regression in commons-geometry-spherical.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to