From 02f1e51c3bcb411b2c8b70b177ec704d8aaf4b39 Mon Sep 17 00:00:00 2001 From: apoorva-01 Date: Fri, 10 Jul 2026 00:17:54 +0530 Subject: [PATCH] Fix asin losing precision near x = 1 --- decimal.js | 3 ++- decimal.mjs | 3 ++- test/modules/asin.js | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/decimal.js b/decimal.js index f7ecafb..1bb9a4a 100644 --- a/decimal.js +++ b/decimal.js @@ -936,7 +936,8 @@ Ctor.precision = pr + 6; Ctor.rounding = 1; - x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan(); + // See https://github.com/MikeMcl/decimal.js/issues/249 + x = x.div(new Ctor(1).minus(x).times(new Ctor(1).plus(x)).sqrt().plus(1)).atan(); Ctor.precision = pr; Ctor.rounding = rm; diff --git a/decimal.mjs b/decimal.mjs index 18dc640..522effa 100644 --- a/decimal.mjs +++ b/decimal.mjs @@ -932,7 +932,8 @@ P.inverseSine = P.asin = function () { Ctor.precision = pr + 6; Ctor.rounding = 1; - x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan(); + // See https://github.com/MikeMcl/decimal.js/issues/249 + x = x.div(new Ctor(1).minus(x).times(new Ctor(1).plus(x)).sqrt().plus(1)).atan(); Ctor.precision = pr; Ctor.rounding = rm; diff --git a/test/modules/asin.js b/test/modules/asin.js index a5f7cd2..75f40c3 100644 --- a/test/modules/asin.js +++ b/test/modules/asin.js @@ -69,6 +69,12 @@ T('asin', function () { t('-0.5110799', 65, 2, '-0.53644070128325494453237503822033662227949443391067147724566337039'); t('-0.579334355419195148', 31, 3, '-0.617911801796609232511609252874'); + // Arguments very close to 1. See https://github.com/MikeMcl/decimal.js/issues/249 + t('0.9999999999999999995', 31, 1, '1.570796325794896619231321691598'); + t('0.99999999999999999999995', 36, 6, '1.5707963267848966192313216916397514'); + t('-0.9999999999999999999', 30, 4, '-1.57079632634768302373136375235'); + t('0.99999999999999999999999999999', 45, 0, '1.57079632679489214709536669206035862375124724'); + /* t('0.63', 1, 5, '0.7'); t('-0.42', 4, 0, '-0.4335');