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

Alex Herbert commented on MATH-1685:
------------------------------------

Apache Commons Math provides functions with the same signature as the JDK Math 
class (currently at the JDK 8 API).
||Class||Notes||
|java.lang.Math|JDK implementation. May use native system functions and so can 
differ between platforms.|
|org.apache.commons.math3.util.FastMath|Pure Java based library using IEEE754 
floating-point math. Should not differ between platforms.|
|org.apache.commons.math4.core.jdkmath.AccurateMath|Configurable math interface 
using a system property to switch between JDK Math and the FastMath backend. 
Switching is performed during class initialisation and the choice is frozen 
within the same JVM.|

If you are seeing differences in JDK's Math class then this is due to the 
underlying platform implementation. Typically these implementations will have a 
specification for the tolerance allowed to the exact answer. Most functions are 
allowed to be within 1 ULP of the correct double precision representation. IIRC 
only the sqrt function is specified as exact (0 ULP). Your difference here is 
within tolerance as these answers are adjacent numbers:
{noformat}
jshell
|  Welcome to JShell -- Version 21.0.8
|  For an introduction type: /help intro

jshell> (0.9599817810077942 - 0.9599817810077941) / Math.ulp(0.9599817810077942)
$1 ==> 1.0{noformat}
If you require the same answer on all platforms then you can try FastMath, or 
configure AccurateMath to use the java implementation.

Note that FastMath was renamed to AccurateMath as the performance against 
current JDKs is variable, and can be measurably slower. However the accuracy is 
still within the JDK ULP tolerance specifications for the Math class. 

 

> Math.sin() evaluates to different results on arm64 and x64
> ----------------------------------------------------------
>
>                 Key: MATH-1685
>                 URL: https://issues.apache.org/jira/browse/MATH-1685
>             Project: Commons Math
>          Issue Type: Bug
>         Environment: aarch64
> x64
>            Reporter: Philipp Rebsamen
>            Priority: Minor
>
> When using Math.sin(), certain inputs evaluate to different results on arm64 
> and x64 architectures. We have tested it on several Apple M4, M3 and M2 
> powered MacBooks as well as some Windows notebooks (all running Windows 11). 
> Here are the respective outputs (with comparison to FastMath and StrictMath 
> libraries as well):
> x64:
> Math.sin(1.8546554965762145) = 0.9599817810077{*}941{*}
> _FastMath.sin(1.8546554965762145) = 0.9599817810077941_
> _StrictMath.sin(1.8546554965762145) = 0.9599817810077941_
>  
> aarch64:
> Math.sin(1.8546554965762145) = 0.9599817810077{*}942{*}
> _FastMath.sin(1.8546554965762145) = 0.9599817810077941_
> _StrictMath.sin(1.8546554965762145) = 0.9599817810077941_
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to