Question Details

No question body available.

Tags

mathjs

Answers (2)

May 21, 2026 Score: 2 Rep: 36,003 Quality: Medium Completeness: 100%

In EcmaScript, the Math.sin and other trancendental functions are specified to be "implementation approximated".

This means that the results are supplied by an implementation facility, and are supposed to be approximately the same on all systems - but not guaranteed to be exactly the same.

To understand why that is, you should consider that in practice, at least some implementations are delegating to the C runtime library for these functions, and the C standard also specifies that the accuracy of these functions is implementation defined. In practice C runtime libraries use different algorithms, and may differ by 1 or 2 units in the last place.

Per the C23 draft https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf at 5.2.4.2.2 Characteristics of floating types paragraph 14

The accuracy of the floating-point operations (+,-, *, /) and of most of the library functions in and that return floating-point results is implementation-defined, as is the accuracy of the conversion between floating-point internal representations and string representations performed by the library functions in , , and . The implementation may state that the accuracy is unknown.

In practice, I believe major engines such as V8 and SpiderMonkey use code originally derived from fdlibm, as does Java. However this does not mean that their implementation is in fact identical today, as improving these algorithms is an ongoing project even today. (Ref How does C compute sin() and other math functions?)

What if I need an deterministic implementation

The math.js library delegates floating point sin directly to the underlying engine implementation. However the BigNumber implementation is implemented in the library in javascript, so ought to give you a deterministic answer.

Alternatively, you might implement your own, perhaps using one of the sources used by glibc as a reference:

I got these links from the answer on this question How does C compute sin() and other math functions?

May 21, 2026 Score: 1 Rep: 1,270 Quality: Low Completeness: 80%

To show an example for my previous comment, here is a quick example using decimal.js. In all the browsers I have on my Debian 13, Opera, Firefox, and Chrome, the results were identical: 0.59847214410395649405.

P.S.: I didn't want to spend my time on mastering decimal.js for a simple example, so I asked an AI to provide some code.

decimal.js sin(2.5)

// Set global precision to 30 significant digits Decimal.set({ precision: 30 });

const x = new Decimal('2.5'); const s = Decimal.sin(x);

// Truncate to 20 digits after the decimal point const truncated = s.toDecimalPlaces(20, Decimal.ROUND_DOWN);

console.log(truncated.toFixed(20));