Skip to content

Commit 274933f

Browse files
committed
Adding counting acosh for PInt, small corrections.
1 parent 80fabd4 commit 274933f

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_math.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ def __float__(self):
192192
# TODO uncomment when GR-10346 will be fixed
193193
#self.ftest('acos(MyFloat())', math.acosh(MyFF()), 0.9272952180016123)
194194
self.assertRaises(ValueError, math.acosh, MyFloat())
195+
math.acosh(BIG_INT)
196+
self.assertRaises(TypeError, math.acosh, 'ahoj')
195197

196198
def testAsin(self):
197199
self.assertRaises(TypeError, math.asin)

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

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public abstract static class SqrtNode extends PythonUnaryBuiltinNode {
8383

8484
public abstract double executeObject(Object value);
8585

86-
private static BigDecimal sqrtBigNumber(BigInteger value) {
86+
protected static BigDecimal sqrtBigNumber(BigInteger value) {
8787
BigDecimal number = new BigDecimal(value);
8888
BigDecimal result = BigDecimal.ZERO;
8989
BigDecimal guess = BigDecimal.ONE;
@@ -147,7 +147,7 @@ public double acosh(Object value,
147147
return sqrtNode.executeObject(result);
148148
}
149149

150-
protected SqrtNode create() {
150+
public static SqrtNode create() {
151151
return MathModuleBuiltinsFactory.SqrtNodeFactory.create(new PNode[0]);
152152
}
153153
}
@@ -709,9 +709,9 @@ public PTuple frexpO(Object value) {
709709
@TypeSystemReference(PythonArithmeticTypes.class)
710710
@ImportStatic(MathGuards.class)
711711
@GenerateNodeFactory
712-
public abstract static class IsNanNode extends PythonBuiltinNode {
712+
public abstract static class IsNanNode extends PythonUnaryBuiltinNode {
713713

714-
public abstract boolean execute(Object value);
714+
public abstract boolean executeObject(Object value);
715715

716716
@Specialization
717717
public boolean isNan(@SuppressWarnings("unused") long value) {
@@ -736,10 +736,10 @@ public boolean isinf(Object value,
736736
if (result == PNone.NO_VALUE) {
737737
throw raise(TypeError, "must be real number, not %p", value);
738738
}
739-
return isNanNode.execute(result);
739+
return isNanNode.executeObject(result);
740740
}
741741

742-
protected IsNanNode create() {
742+
protected static IsNanNode create() {
743743
return MathModuleBuiltinsFactory.IsNanNodeFactory.create(new PNode[0]);
744744
}
745745
}
@@ -895,9 +895,9 @@ public double ldexpOO(Object mantissa, Object exp) {
895895
@TypeSystemReference(PythonArithmeticTypes.class)
896896
@ImportStatic(MathGuards.class)
897897
@GenerateNodeFactory
898-
public abstract static class AcosNode extends PythonBuiltinNode {
898+
public abstract static class AcosNode extends PythonUnaryBuiltinNode {
899899

900-
public abstract double execute(Object value);
900+
public abstract double executeObject(Object value);
901901

902902
@Specialization
903903
public double acos(long value,
@@ -931,10 +931,10 @@ public double acos(Object value,
931931
if (result == PNone.NO_VALUE) {
932932
throw raise(TypeError, "must be real number, not %p", value);
933933
}
934-
return acosNode.execute(result);
934+
return acosNode.executeObject(result);
935935
}
936936

937-
protected AcosNode create() {
937+
protected static AcosNode create() {
938938
return MathModuleBuiltinsFactory.AcosNodeFactory.create(new PNode[0]);
939939
}
940940
}
@@ -962,6 +962,20 @@ public double acoshDouble(double value,
962962
return Math.log(value + Math.sqrt(value * value - 1.0));
963963
}
964964

965+
@Specialization
966+
@TruffleBoundary
967+
public double acoshDouble(PInt value,
968+
@Cached("createBinaryProfile()") ConditionProfile doNotFit) {
969+
BigInteger bValue = value.getValue();
970+
if (doNotFit.profile(bValue.compareTo(BigInteger.ONE) == -1)) {
971+
throw raise(ValueError, "math domain error");
972+
}
973+
974+
BigDecimal sqrt = SqrtNode.sqrtBigNumber(bValue.multiply(bValue).subtract(BigInteger.ONE));
975+
BigDecimal bd = new BigDecimal(bValue);
976+
return Math.log(bd.add(sqrt).doubleValue());
977+
}
978+
965979
@Specialization(guards = "!isNumber(value)")
966980
public double acosh(Object value,
967981
@Cached("create(__FLOAT__)") LookupAndCallUnaryNode dispatchFloat,
@@ -973,7 +987,7 @@ public double acosh(Object value,
973987
return acoshNode.executeObject(result);
974988
}
975989

976-
protected AcoshNode create() {
990+
protected static AcoshNode create() {
977991
return MathModuleBuiltinsFactory.AcoshNodeFactory.create(new PNode[0]);
978992
}
979993
}
@@ -1019,7 +1033,7 @@ public double acosh(Object value,
10191033
return asinNode.executeObject(result);
10201034
}
10211035

1022-
protected AsinNode create() {
1036+
protected static AsinNode create() {
10231037
return MathModuleBuiltinsFactory.AsinNodeFactory.create(new PNode[0]);
10241038
}
10251039
}
@@ -1058,9 +1072,9 @@ public double sin(double value) {
10581072
@TypeSystemReference(PythonArithmeticTypes.class)
10591073
@ImportStatic(MathGuards.class)
10601074
@GenerateNodeFactory
1061-
public abstract static class IsFiniteNode extends PythonBuiltinNode {
1075+
public abstract static class IsFiniteNode extends PythonUnaryBuiltinNode {
10621076

1063-
public abstract boolean execute(Object value);
1077+
public abstract boolean executeObject(Object value);
10641078

10651079
@Specialization
10661080
public boolean isfinite(@SuppressWarnings("unused") long value) {
@@ -1085,10 +1099,10 @@ public boolean isinf(Object value,
10851099
if (result == PNone.NO_VALUE) {
10861100
throw raise(TypeError, "must be real number, not %p", value);
10871101
}
1088-
return isFiniteNode.execute(result);
1102+
return isFiniteNode.executeObject(result);
10891103
}
10901104

1091-
protected IsFiniteNode create() {
1105+
protected static IsFiniteNode create() {
10921106
return MathModuleBuiltinsFactory.IsFiniteNodeFactory.create(new PNode[0]);
10931107
}
10941108
}
@@ -1097,9 +1111,9 @@ protected IsFiniteNode create() {
10971111
@TypeSystemReference(PythonArithmeticTypes.class)
10981112
@ImportStatic(MathGuards.class)
10991113
@GenerateNodeFactory
1100-
public abstract static class IsInfNode extends PythonBuiltinNode {
1114+
public abstract static class IsInfNode extends PythonUnaryBuiltinNode {
11011115

1102-
public abstract boolean execute(Object value);
1116+
public abstract boolean executeObject(Object value);
11031117

11041118
@Specialization
11051119
public boolean isinf(@SuppressWarnings("unused") long value) {
@@ -1124,10 +1138,10 @@ public boolean isinf(Object value,
11241138
if (result == PNone.NO_VALUE) {
11251139
throw raise(TypeError, "must be real number, not %p", value);
11261140
}
1127-
return isInfNode.execute(result);
1141+
return isInfNode.executeObject(result);
11281142
}
11291143

1130-
protected IsInfNode create() {
1144+
protected static IsInfNode create() {
11311145
return MathModuleBuiltinsFactory.IsInfNodeFactory.create(new PNode[0]);
11321146
}
11331147
}
@@ -1366,7 +1380,7 @@ public double log(PInt value, Object base,
13661380
return logNode.executeObject(value, resultBase);
13671381
}
13681382

1369-
protected LogNode create() {
1383+
public static LogNode create() {
13701384
return MathModuleBuiltinsFactory.LogNodeFactory.create(new PNode[0]);
13711385
}
13721386
}

0 commit comments

Comments
 (0)