From 318fec0eb9eac51a299c27cf18c6d2f3814f6ae0 Mon Sep 17 00:00:00 2001 From: DoDaek Date: Wed, 2 Oct 2019 19:34:02 +0900 Subject: [PATCH 1/3] Implement float is_integer method --- py/float.go | 15 +++++++++++++++ py/tests/float.py | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/py/float.go b/py/float.go index 1f9e1f99..7459b5ec 100644 --- a/py/float.go +++ b/py/float.go @@ -394,6 +394,21 @@ 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 + } + b := math.Abs(f - math.Round(f)) + return NewBool(b < math.SmallestNonzeroFloat64), nil; + } + return nil, AttributeError + }, 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" From d78d5155a325c544bcf0ceec98e8b0c3bc92b0de Mon Sep 17 00:00:00 2001 From: DoDaek Date: Fri, 4 Oct 2019 14:40:19 +0900 Subject: [PATCH 2/3] float: modified is_integer method --- py/float.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/py/float.go b/py/float.go index 7459b5ec..72f912e2 100644 --- a/py/float.go +++ b/py/float.go @@ -398,14 +398,14 @@ func (a Float) M__ge__(other Object) (Object, error) { 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 + f, _ := FloatAsFloat64(a) + if math.Floor(f) == f { + return True, nil } - b := math.Abs(f - math.Round(f)) - return NewBool(b < math.SmallestNonzeroFloat64), nil; + return False, nil } - return nil, AttributeError + _, e := cantConvert(self, "float") + return nil, e }, 0, "is_integer() -> Return True if the float instance is finite with integral value, and False otherwise.") } From 3b0574d44d991ee1b1201d28a5d4924c06684268 Mon Sep 17 00:00:00 2001 From: DoDaek Date: Fri, 4 Oct 2019 16:35:11 +0900 Subject: [PATCH 3/3] float: modified return values of is_integer method --- py/float.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/py/float.go b/py/float.go index 72f912e2..4f47759b 100644 --- a/py/float.go +++ b/py/float.go @@ -398,14 +398,13 @@ func (a Float) M__ge__(other Object) (Object, error) { func init() { FloatType.Dict["is_integer"] = MustNewMethod("is_integer", func(self Object) (Object, error) { if a, ok := convertToFloat(self); ok { - f, _ := FloatAsFloat64(a) - if math.Floor(f) == f { - return True, nil + f, err := FloatAsFloat64(a) + if err != nil { + return nil, err } - return False, nil + return NewBool(math.Floor(f) == f), nil } - _, e := cantConvert(self, "float") - return nil, e + return cantConvert(self, "float") }, 0, "is_integer() -> Return True if the float instance is finite with integral value, and False otherwise.") }