Skip to content

Commit 5ba1b64

Browse files
committed
asin implementation
1 parent 1512458 commit 5ba1b64

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,23 @@ def __float__(self):
193193
#self.ftest('acos(MyFloat())', math.acosh(MyFF()), 0.9272952180016123)
194194
self.assertRaises(ValueError, math.acosh, MyFloat())
195195

196+
def testAsin(self):
197+
self.assertRaises(TypeError, math.asin)
198+
self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
199+
self.ftest('asin(0)', math.asin(0), 0)
200+
self.ftest('asin(1)', math.asin(1), math.pi/2)
201+
self.assertRaises(ValueError, math.asin, INF)
202+
self.assertRaises(ValueError, math.asin, NINF)
203+
self.assertRaises(ValueError, math.asin, 1 + eps)
204+
self.assertRaises(ValueError, math.asin, -1 - eps)
205+
self.assertTrue(math.isnan(math.asin(NAN)))
206+
207+
self.assertRaises(ValueError, math.asin, 10)
208+
self.assertRaises(ValueError, math.asin, -10)
209+
self.assertRaises(ValueError, math.asin, LONG_INT)
210+
self.assertRaises(ValueError, math.asin, BIG_INT)
211+
self.assertRaises(TypeError, math.asin, 'ahoj')
212+
196213
def testIsfinite(self):
197214
self.assertTrue(math.isfinite(0.0))
198215
self.assertTrue(math.isfinite(-0.0))

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,52 @@ protected AcoshNode create() {
914914
}
915915
}
916916

917+
@Builtin(name = "asin", fixedNumOfArguments = 1, doc = "Return the arc sine (measured in radians) of x.")
918+
@TypeSystemReference(PythonArithmeticTypes.class)
919+
@ImportStatic(MathGuards.class)
920+
@GenerateNodeFactory
921+
public abstract static class AsinNode extends PythonUnaryBuiltinNode {
922+
923+
public abstract double executeObject(Object value);
924+
925+
@Specialization
926+
public double asinInt(long value,
927+
@Cached("createBinaryProfile()") ConditionProfile doNotFit) {
928+
return asinDouble(value, doNotFit);
929+
}
930+
931+
@Specialization
932+
@TruffleBoundary
933+
public double asinPInt(PInt value,
934+
@Cached("createBinaryProfile()") ConditionProfile doNotFit) {
935+
return asinDouble(value.intValue(), doNotFit);
936+
}
937+
938+
@Specialization
939+
public double asinDouble(double value,
940+
@Cached("createBinaryProfile()") ConditionProfile doNotFit) {
941+
if (doNotFit.profile(value < -1 || value > 1 )) {
942+
throw raise(ValueError, "math domain error");
943+
}
944+
return Math.asin(value);
945+
}
946+
947+
@Specialization(guards = "!isNumber(value)")
948+
public double acosh(Object value,
949+
@Cached("create(__FLOAT__)") LookupAndCallUnaryNode dispatchFloat,
950+
@Cached("create()") AsinNode asinNode) {
951+
Object result = dispatchFloat.executeObject(value);
952+
if (result == PNone.NO_VALUE) {
953+
throw raise(TypeError, "must be real number, not %p", value);
954+
}
955+
return asinNode.executeObject(result);
956+
}
957+
958+
protected AsinNode create() {
959+
return MathModuleBuiltinsFactory.AsinNodeFactory.create(new PNode[0]);
960+
}
961+
}
962+
917963
@Builtin(name = "cos", fixedNumOfArguments = 1)
918964
@GenerateNodeFactory
919965
public abstract static class CosNode extends PythonBuiltinNode {

0 commit comments

Comments
 (0)