File tree Expand file tree Collapse file tree 5 files changed +53
-0
lines changed
Expand file tree Collapse file tree 5 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 99import org .python .core .stringlib .InternalFormatSpecParser ;
1010import java .io .Serializable ;
1111import java .math .BigDecimal ;
12+ import java .math .RoundingMode ;
1213
1314import org .python .expose .ExposedClassMethod ;
1415import 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. );
Original file line number Diff line number Diff 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. );
Original file line number Diff line number Diff 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__ ();
Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments