From 2b1298cd69fe014dc61b73db47daefacf0328cce Mon Sep 17 00:00:00 2001 From: Alden Date: Mon, 13 Feb 2023 03:50:30 -0500 Subject: [PATCH] use a method less prone to cancellation near x=1 --- decimal.js | 7 +++---- decimal.mjs | 5 ++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/decimal.js b/decimal.js index aa6526a..8eabf43 100644 --- a/decimal.js +++ b/decimal.js @@ -744,14 +744,13 @@ Ctor.precision = pr + 6; Ctor.rounding = 1; - - x = x.asin(); - halfPi = getPi(Ctor, pr + 4, rm).times(0.5); + + x = (new Ctor(1)).minus(x).div(x.plus(1)).sqrt().atan(); Ctor.precision = pr; Ctor.rounding = rm; - return halfPi.minus(x); + return x.times(2); }; diff --git a/decimal.mjs b/decimal.mjs index 909300c..293ee8e 100644 --- a/decimal.mjs +++ b/decimal.mjs @@ -741,13 +741,12 @@ P.inverseCosine = P.acos = function () { Ctor.precision = pr + 6; Ctor.rounding = 1; - x = x.asin(); - halfPi = getPi(Ctor, pr + 4, rm).times(0.5); + x = (new Ctor(1)).minus(x).div(x.plus(1)).sqrt().atan(); Ctor.precision = pr; Ctor.rounding = rm; - return halfPi.minus(x); + return x.times(2); };