diff --git a/py/float.go b/py/float.go index 1f9e1f99..4f47759b 100644 --- a/py/float.go +++ b/py/float.go @@ -394,6 +394,20 @@ func (a Float) M__ge__(other Object) (Object, error) { return NotImplemented, nil } +// Properties +func init() { + FloatType.Dict["is_integer"] = MustNewMethod("is_integer", func(self Object) (Object, error) { + if a, ok := convertToFloat(self); ok { + f, err := FloatAsFloat64(a) + if err != nil { + return nil, err + } + return NewBool(math.Floor(f) == f), nil + } + return cantConvert(self, "float") + }, 0, "is_integer() -> Return True if the float instance is finite with integral value, and False otherwise.") +} + // Check interface is satisfied var _ floatArithmetic = Float(0) var _ conversionBetweenTypes = Float(0) diff --git a/py/tests/float.py b/py/tests/float.py index 7f4cc7e7..93dd7fcc 100644 --- a/py/tests/float.py +++ b/py/tests/float.py @@ -34,4 +34,8 @@ assert str(float("1.00")) == "1.0" assert str(float("2.010")) == "2.01" +doc="is_integer" +assert (1.0).is_integer() == True +assert (2.3).is_integer() == False + doc="finished"