[
https://issues.apache.org/jira/browse/NUMBERS-161?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17356662#comment-17356662
]
Matt Juntunen commented on NUMBERS-161:
---------------------------------------
bq. ... one of the assertions had to be commented out. Please check.
The issue is that {{x + Math.PI == below + Math.PI}}. So, when we run through
normalization, the result comes out exactly equal to the upper bound, and we
subtract the period, Math.PI. However, this gives us a number that is actually
less than the lower bound. I think in these cases, we need to return the lower
bound directly.
{code:java}
// Angle
/**
* @param a Angle.
* @return {@code = a - k} where {@code k} is an integer that satisfies
* {@code lo <= a - k < lo + period}.
*/
@Override
public double applyAsDouble(double a) {
if (lo <= a &&
a < hi) {
// Already within the main interval.
return a;
}
final double normalized = reduce.applyAsDouble(a) + lo;
return normalized < hi ?
normalized :
// If value is too small to be representable compared to the
// floor expression above (ie, if value + x = x), then we may
// end up with a number exactly equal to the upper bound here.
// In that case, return the lower bound.
lo;
}
// AngleTest
@Test
void testNormalizePreciseLowerBound() {
final double x = Math.PI / 3;
final double above = Math.nextUp(x);
final double below = Math.nextDown(x);
final UnaryOperator<Angle.Rad> normalizer =
Angle.Rad.normalizer(Angle.Rad.of(x));
Assertions.assertEquals(x,
normalizer.apply(Angle.Rad.of(x)).getAsDouble());
Assertions.assertEquals(above,
normalizer.apply(Angle.Rad.of(above)).getAsDouble());
// "below" is so close to "x" that below + Math.PI = x + Math.PI
// In this case, we can't return below + Math.PI because that is
exactly equal to the
// upper bound of the range. Instead, we must return the lower bound of
x.
Assertions.assertEquals(x,
normalizer.apply(Angle.Rad.of(below)).getAsDouble());
}
{code}
I've plugged a little bit of this class into commons-geometry-spherical and it
fixes the regressions from before. I also suspect that it will increase the
accuracy of the library in general.
However, some operations that used to be very simple are now more verbose. For
example, there is no simple double constant for 2pi; you have to use
{{Angle.Rad.TWO_PI.getAsDouble()}}. Also, normalizing doubles is quite involved:
{code:java}
// current
double normalized =
Rad.normalizer(Rad.of(lower)).apply(Rad.of(azimuth)).getAsDouble();
// previous
double normalized = PlaneAngleRadians.normalizer(lower).applyAsDouble(azimuth);
{code}
Could we add convenience constants and methods for working directly with
doubles?
> 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)