Skip to content

Commit be0a4ea

Browse files
committed
Implementation of Math.modf
1 parent e28b358 commit be0a4ea

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,3 +1216,26 @@ def testRadians(self):
12161216
self.assertRaises(TypeError, math.radians, 'ahoj')
12171217
self.ftest('radians(BIG_INT)', math.radians(BIG_INT), 1.7453279312865818e+29)
12181218

1219+
def testModf(self):
1220+
self.assertRaises(TypeError, math.modf)
1221+
1222+
def testmodf(name, result, expected):
1223+
(v1, v2), (e1, e2) = result, expected
1224+
if abs(v1-e1) > eps or abs(v2-e2):
1225+
self.fail('%s returned %r, expected %r'%\
1226+
(name, result, expected))
1227+
1228+
testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
1229+
testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
1230+
1231+
self.assertEqual(math.modf(INF), (0.0, INF))
1232+
self.assertEqual(math.modf(NINF), (-0.0, NINF))
1233+
1234+
modf_nan = math.modf(NAN)
1235+
self.assertTrue(math.isnan(modf_nan[0]))
1236+
self.assertTrue(math.isnan(modf_nan[1]))
1237+
1238+
# test of specializations
1239+
testmodf('modf(MyFloat())', math.modf(MyFloat()), (0.6, 0.0))
1240+
self.assertRaises(TypeError, math.modf, 'ahoj')
1241+
testmodf('modf(BIG_INT)', math.modf(BIG_INT), (0.0, 9.999992432902008e+30))

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,43 @@ public double ldexpOO(Object mantissa, Object exp) {
966966

967967
}
968968

969+
@Builtin(name = "modf", fixedNumOfArguments = 1)
970+
@TypeSystemReference(PythonArithmeticTypes.class)
971+
@ImportStatic(MathGuards.class)
972+
@GenerateNodeFactory
973+
public abstract static class ModfNode extends MathUnaryBuiltinNode {
974+
975+
@Specialization
976+
public PTuple modfD(double value) {
977+
if (!Double.isFinite(value)) {
978+
if (Double.isInfinite(value)) {
979+
return factory().createTuple(new Object[]{Math.copySign(0., value), value});
980+
} else if (Double.isNaN(value)) {
981+
return factory().createTuple(new Object[]{value, value});
982+
}
983+
}
984+
double fraction = value % 1;
985+
double integral = value - fraction;
986+
return factory().createTuple(new Object[]{fraction, integral});
987+
}
988+
989+
@Specialization
990+
public PTuple modfL(long value) {
991+
return modfD(value);
992+
}
993+
994+
@Specialization
995+
public PTuple frexpPI(PInt value) {
996+
return modfD(value.doubleValue());
997+
}
998+
999+
@Specialization(guards = "!isNumber(value)")
1000+
public PTuple frexpO(Object value,
1001+
@Cached("create()") ConvertToFloatNode convertToFloatNode) {
1002+
return modfD(convertToFloatNode.execute(value));
1003+
}
1004+
}
1005+
9691006
@Builtin(name = "acos", fixedNumOfArguments = 1, doc = "Return the arc cosine (measured in radians) of x.")
9701007
@GenerateNodeFactory
9711008
public abstract static class AcosNode extends MathDoubleUnaryBuiltinNode {

0 commit comments

Comments
 (0)