Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute acos with less cancellation near x=1 #217

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
7 changes: 3 additions & 4 deletions decimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};


Expand Down
5 changes: 2 additions & 3 deletions decimal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};


Expand Down
3 changes: 3 additions & 0 deletions test/hypothesis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
.hypothesis
.ipynb_checkpoints
99 changes: 99 additions & 0 deletions test/hypothesis/error_hunt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from decimal import Decimal, getcontext
import json
import subprocess
import hypothesis
from hypothesis import given, settings
from hypothesis.strategies import decimals, integers, tuples
from mpmath import mp
import pytest

mp.prec = 500
getcontext().prec = 14

node = subprocess.Popen(
["node", "evaluate.mjs"], stdout=subprocess.PIPE, stdin=subprocess.PIPE
)


def get_decimal(func: str, args: list, config: dict):
arg = json.dumps({"func": func, "args": args, "config": config}).encode() + b"\r"
node.stdin.write(arg)
node.stdin.flush()
return Decimal(node.stdout.readline().strip().decode())


def assert_matches(x, mpfunc, jsfunc=None):
if jsfunc is None:
jsfunc = mpfunc
y = Decimal(str(getattr(mp, mpfunc)(x))) * Decimal("1.0")
z = get_decimal(jsfunc, [str(x)], {"precision": 14})
assert y == z


@pytest.mark.parametrize("fn", "sin cos tan atan asinh".split())
@given(
x=tuples(
decimals(
allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14
),
integers(min_value=-99, max_value=99),
).map(lambda tup: tup[0] * Decimal(10) ** tup[1])
)
@settings(max_examples=100_000)
def test_matches(x, fn):
assert_matches(x, fn)


@pytest.mark.parametrize("fn", "ln log10 sqrt".split())
@given(
x=tuples(
decimals(
allow_nan=False,
allow_infinity=False,
min_value=1e-13,
max_value=1,
places=14,
),
integers(min_value=-99, max_value=99),
).map(lambda tup: tup[0] * Decimal(10) ** tup[1])
)
@settings(max_examples=100_000)
def test_positive_domain(x, fn):
assert_matches(x, fn)


@pytest.mark.parametrize("fn", "asin acos atanh".split())
@given(
x=decimals(
allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14
)
)
@settings(max_examples=100_000)
def test_inverse_trig(x, fn):
assert_matches(x, fn)


@pytest.mark.parametrize("fn", "sinh cosh tanh exp".split())
@given(
x=tuples(
decimals(
allow_nan=False, allow_infinity=False, min_value=-1, max_value=1, places=14
),
integers(min_value=-99, max_value=3),
).map(lambda tup: tup[0] * Decimal(10) ** tup[1])
)
@settings(max_examples=100_000)
def test_small_domain(x, fn):
assert_matches(x, fn)

@given(
x=tuples(
decimals(
allow_nan=False, allow_infinity=False, min_value=1, max_value=10, places=14
),
integers(min_value=0, max_value=99),
).map(lambda tup: tup[0] * Decimal(10) ** tup[1])
)
@settings(max_examples=100_000)
def test_acosh(x):
assert_matches(x, 'acosh')
26 changes: 26 additions & 0 deletions test/hypothesis/evaluate.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// listen for test cases, and provide the results.
// give JSON of the test case, receive the result
// Example:
// > {"func":"tan", "args":["12.5"], "config":{"precision":8}}
// -0.066468242


import {Decimal} from '../../decimal.mjs';
import {createInterface} from 'readline';

const readline = createInterface({
input: process.stdin,
output: process.stdout
});

readline.on("close", () => {console.log('\n'); process.exit(0);});

readline.on("line", (line) => {
if (line) {
const {func, args, config} = JSON.parse(line);
config.defaults = true;
Decimal.set(config);
const result = Decimal[func](...args);
console.log(result);
}
});
3 changes: 3 additions & 0 deletions test/hypothesis/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hypothesis
mpmath
node
4 changes: 4 additions & 0 deletions test/modules/acos.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ T('acos', function () {
t('0.41923186648524998285699814886092351368793359300547574', 42, 3, '1.13819724675000902666504291062053681280681');
t('-0.19508761025300975791021816036', 27, 1, '1.76714310275532020878366926');
t('0.0623252416', 19, 0, '1.508430664767542249');
t('0.9999999297625', 14, 4, '0.00037479994883195');
t('0.99999999467518', 14, 4, '0.0001031970930281');
t('0.9999999999999999995', 25, 4, '0.000000001000000000000000000041667');
t('0.99999999999999999999995', 30, 4, '0.0000000000100000000000000000000000416667');

/*
t('0.95', 6, 5, '0.31756');
Expand Down
1 change: 1 addition & 0 deletions test/modules/sin.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,5 @@ T('sin', function () {
t('22011131111011111.111111611113111111111151111111', 143, 2, '0.82582504036277799386306063085803210583251158969990606609364360685569588545519071481543672724620118406694191888115120286393881609546697317692404');
t('996270725099515169352424536636186062915113219400094989.8763797268889422850038402633796294758036260533902551191769915343780424028900449342752548782035', 46, 2, '0.6613706114081017074779805460666900787572253475');
t('0.780360750628373', 37, 5, '0.7035358359376557390803090830882458906');
t('5900', 14, 4, '0.088879123681079');
});