Skip to content

Commit ab9c3b1

Browse files
committed
Implementation of cmath.log10
1 parent 0c2acef commit ab9c3b1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class CmathModuleBuiltins extends PythonBuiltins {
4747

4848
static final double largeDouble = Double.MAX_VALUE / 4.0; // used to avoid overflow
4949
static final double ln2 = 0.6931471805599453094; // natural log of 2
50+
static final double ln10 = 2.302585092994045684; // natural log of 10
5051

5152
@Override
5253
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
@@ -360,6 +361,8 @@ private PComplex rect(double r, double phi) {
360361
@GenerateNodeFactory
361362
abstract static class LogNode extends PythonBinaryBuiltinNode {
362363

364+
abstract PComplex executeComplex(VirtualFrame frame, Object x, Object y);
365+
363366
// @formatter:off
364367
@CompilerDirectives.CompilationFinal(dimensions = 2)
365368
private static final ComplexConstant[][] SPECIAL_VALUES = {
@@ -373,6 +376,10 @@ abstract static class LogNode extends PythonBinaryBuiltinNode {
373376
};
374377
// @formatter:on
375378

379+
static LogNode create() {
380+
return CmathModuleBuiltinsFactory.LogNodeFactory.create();
381+
}
382+
376383
@Specialization(guards = "isNoValue(y)")
377384
PComplex doComplexNone(PComplex x, @SuppressWarnings("unused") PNone y) {
378385
return log(x);
@@ -431,6 +438,25 @@ private double computeRealPart(double real, double imag) {
431438
}
432439
}
433440

441+
@Builtin(name = "log10", minNumOfPositionalArgs = 1)
442+
@TypeSystemReference(PythonArithmeticTypes.class)
443+
@ImportStatic(MathGuards.class)
444+
@GenerateNodeFactory
445+
abstract static class Log10Node extends PythonUnaryBuiltinNode {
446+
@Child LogNode logNode = LogNode.create();
447+
448+
@Specialization
449+
PComplex doComplex(VirtualFrame frame, PComplex z) {
450+
PComplex r = logNode.executeComplex(frame, z, PNone.NO_VALUE);
451+
return factory().createComplex(r.getReal() / ln10, r.getImag() / ln10);
452+
}
453+
454+
@Specialization
455+
PComplex doGeneral(VirtualFrame frame, Object zObj, @Cached CoerceToComplexNode coerceXToComplex) {
456+
return doComplex(frame, coerceXToComplex.execute(frame, zObj));
457+
}
458+
}
459+
434460
@Builtin(name = "sqrt", minNumOfPositionalArgs = 1)
435461
@GenerateNodeFactory
436462
abstract static class SqrtNode extends CmathComplexUnaryBuiltinNode {

0 commit comments

Comments
 (0)