Skip to content

Commit 0137700

Browse files
committed
Fixed precision of math.acosh() and math.atanh()
1 parent cffb867 commit 0137700

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,13 +1456,20 @@ public double count(double value) {
14561456
@GenerateNodeFactory
14571457
public abstract static class AcoshNode extends MathDoubleUnaryBuiltinNode {
14581458

1459+
private static final double TWO_POW_P28 = 0x1.0p28;
1460+
private static final double LN_2 = 6.93147180559945286227e-01;
1461+
14591462
@Specialization
14601463
@TruffleBoundary
14611464
@Override
14621465
public double doPI(PInt value) {
14631466
BigInteger bValue = value.getValue();
14641467
checkMathDomainError(bValue.compareTo(BigInteger.ONE) < 0);
14651468

1469+
if (bValue.bitLength() >= 28) {
1470+
return Math.log(bValue.doubleValue()) + LN_2;
1471+
}
1472+
14661473
BigDecimal sqrt = SqrtNode.sqrtBigNumber(bValue.multiply(bValue).subtract(BigInteger.ONE));
14671474
BigDecimal bd = new BigDecimal(bValue);
14681475
return Math.log(bd.add(sqrt).doubleValue());
@@ -1471,6 +1478,13 @@ public double doPI(PInt value) {
14711478
@Override
14721479
public double count(double value) {
14731480
checkMathDomainError(value < 1);
1481+
if (value >= TWO_POW_P28) {
1482+
return Math.log(value) + LN_2;
1483+
}
1484+
if (value <= 2.0) {
1485+
double t = value - 1.0;
1486+
return Math.log1p(t + Math.sqrt(2.0 * t + t * t));
1487+
}
14741488
return Math.log(value + Math.sqrt(value * value - 1.0));
14751489
}
14761490
}
@@ -1564,13 +1578,23 @@ public double count(double value) {
15641578
@GenerateNodeFactory
15651579
public abstract static class AtanhNode extends MathDoubleUnaryBuiltinNode {
15661580

1581+
private static final double TWO_POW_M28 = 0x1.0p-28;
1582+
15671583
@Override
15681584
public double count(double value) {
1569-
if (value == 0) {
1570-
return 0;
1585+
double abs = Math.abs(value);
1586+
checkMathDomainError(abs >= 1.0);
1587+
if (abs < TWO_POW_M28) {
1588+
return value;
1589+
}
1590+
double t;
1591+
if (abs < 0.5) {
1592+
t = abs + abs;
1593+
t = 0.5 * Math.log1p(t + t * abs / (1.0 - abs));
1594+
} else {
1595+
t = 0.5 * Math.log1p((abs + abs) / (1.0 - abs));
15711596
}
1572-
checkMathDomainError(value <= -1 || value >= 1);
1573-
return Math.log((1 / value + 1) / (1 / value - 1)) / 2;
1597+
return Math.copySign(t, value);
15741598
}
15751599
}
15761600

0 commit comments

Comments
 (0)