Skip to content

Commit 9ca4fc9

Browse files
committed
math.trunc support.
1 parent b3fb03b commit 9ca4fc9

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

src/org/python/core/PyFloat.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.python.core.stringlib.InternalFormatSpecParser;
1010
import java.io.Serializable;
1111
import java.math.BigDecimal;
12+
import java.math.RoundingMode;
1213

1314
import org.python.expose.ExposedClassMethod;
1415
import org.python.expose.ExposedGet;
@@ -798,6 +799,22 @@ final PyFloat float___float__() {
798799
return getType() == TYPE ? this : Py.newFloat(getValue());
799800
}
800801

802+
@Override
803+
public PyObject __trunc__() {
804+
return float___trunc__();
805+
}
806+
807+
@ExposedMethod(doc = BuiltinDocs.float___trunc___doc)
808+
final PyObject float___trunc__() {
809+
if (value < Integer.MAX_VALUE) {
810+
return new PyInteger((int)value);
811+
} else if (value < Long.MAX_VALUE) {
812+
return new PyLong((long)value);
813+
}
814+
BigDecimal d = new BigDecimal(value);
815+
return new PyLong(d.toBigInteger());
816+
}
817+
801818
@Override
802819
public PyComplex __complex__() {
803820
return new PyComplex(getValue(), 0.);

src/org/python/core/PyInteger.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,16 @@ final PyFloat int___float__() {
867867
return new PyFloat((double) getValue());
868868
}
869869

870+
@Override
871+
public PyObject __trunc__() {
872+
return int___trunc__();
873+
}
874+
875+
@ExposedMethod(doc = BuiltinDocs.int___trunc___doc)
876+
final PyObject int___trunc__() {
877+
return this;
878+
}
879+
870880
@Override
871881
public PyComplex __complex__() {
872882
return new PyComplex(getValue(), 0.);

src/org/python/core/PyLong.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,17 @@ final PyComplex long___complex__() {
919919
return new PyComplex(doubleValue(), 0.);
920920
}
921921

922+
@Override
923+
public PyObject __trunc__() {
924+
return long___trunc__();
925+
}
926+
927+
@ExposedMethod(doc = BuiltinDocs.long___trunc___doc)
928+
final PyObject long___trunc__() {
929+
return this;
930+
}
931+
932+
922933
@Override
923934
public PyString __oct__() {
924935
return long___oct__();

src/org/python/core/PyObject.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,17 @@ public PyComplex __complex__() {
17991799
throw Py.AttributeError("__complex__");
18001800
}
18011801

1802+
/**
1803+
* Equivalent to the standard Python __trunc__ method.
1804+
* Should only be overridden by numeric objects that can reasonably
1805+
* be truncated to an Integral.
1806+
*
1807+
* @return the Integral closest to x between 0 and x.
1808+
**/
1809+
public PyObject __trunc__() {
1810+
throw Py.AttributeError("__trunc__");
1811+
}
1812+
18021813
/**
18031814
* Equivalent to the standard Python __pos__ method.
18041815
*

src/org/python/modules/math.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,10 @@ public static PyTuple frexp(double x) {
357357
return new PyTuple(new PyFloat(x), new PyInteger(exponent));
358358
}
359359

360+
public static PyObject trunc(PyObject number) {
361+
return number.__getattr__("__trunc__").__call__();
362+
}
363+
360364
public static double ldexp(double v, PyObject wObj) {
361365
if (ZERO == v) {
362366
return v; // can be negative zero

0 commit comments

Comments
 (0)