You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Matt Juntunen (Jira)" <ji...@apache.org> on 2020/02/02 03:08:00 UTC

[jira] [Commented] (GEOMETRY-50) Overflow in Vector norm and distance

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

Matt Juntunen commented on GEOMETRY-50:
---------------------------------------

I ran some benchmarks on this with the performance tests in my PR for GEOMETRY-75. The results below compare the current {{norm}} implementation with one using {{SafeNorm}} for JDK 8 and 9.
{noformat}
Current - JDK 8

Benchmark                      (size)  (type)  Mode  Cnt      Score      Error  Units
VectorPerformance.baseline       1000  random  avgt    5    4186.666 ±   156.418  ns/op
VectorPerformance.norm2D         1000  random  avgt    5  393488.671 ± 41310.297  ns/op
VectorPerformance.norm3D         1000  random  avgt    5    4923.344 ±   217.937  ns/op


SafeNorm - JDK 8 

Benchmark                      (size)  (type)  Mode  Cnt      Score      Error  Units
VectorPerformance.baseline       1000  random  avgt    5   4163.560 ±  121.299  ns/op
VectorPerformance.norm2D         1000  random  avgt    5  11169.608 ±  416.842  ns/op
VectorPerformance.norm3D         1000  random  avgt    5  12489.558 ±  287.116  ns/op


Current - JDK 9

Benchmark                      (size)  (type)  Mode  Cnt      Score      Error  Units
VectorPerformance.baseline       1000  random  avgt    5   4191.927 ±  262.073  ns/op
VectorPerformance.norm2D         1000  random  avgt    5  20630.762 ±  905.460  ns/op
VectorPerformance.norm3D         1000  random  avgt    5   5026.333 ±  375.147  ns/op


SafeNorm - JDK 9

Benchmark                      (size)  (type)  Mode  Cnt      Score      Error  Units
VectorPerformance.baseline       1000  random  avgt    5   4111.371 ±   55.975  ns/op
VectorPerformance.norm2D         1000  random  avgt    5  11334.412 ±  117.637  ns/op
VectorPerformance.norm3D         1000  random  avgt    5  12742.333 ±  401.728  ns/op

{noformat}
Some observations:
 1. {{SafeNorm}} is considerably faster than {{Math.hypot}} in JDK 8 and moderately faster than that in JDK 9.
 2. {{SafeNorm}} applies a 2-3x performance penalty in 3D, which is to be expected since the current implementation does no overflow/underflow checks whatsoever.
 3. The performance between the different dimensions is pretty consistent with {{SafeNorm}}.

Overall, {{SafeNorm}} seems like a good choice. It's a performance gain in 2D and a modest performance hit in 3D.

> Overflow in Vector norm and distance
> ------------------------------------
>
>                 Key: GEOMETRY-50
>                 URL: https://issues.apache.org/jira/browse/GEOMETRY-50
>             Project: Apache Commons Geometry
>          Issue Type: Bug
>            Reporter: Baljit Singh
>            Priority: Major
>
> In Euclidean Vector classes (Vector2D, Vector3D), norm() and distance() rely on Math.sqrt(), which can overflow if the components of the vectors are large. Instead, they should rely on SafeNorm.



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