Skip to content

Commit 720c16a

Browse files
DoDaekcorona10
authored andcommitted
Implement float is_integer method (#112)
* Implement float is_integer method * float: modified is_integer method * float: modified return values of is_integer method
1 parent 6ba973e commit 720c16a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

py/float.go

+14
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,20 @@ func (a Float) M__ge__(other Object) (Object, error) {
394394
return NotImplemented, nil
395395
}
396396

397+
// Properties
398+
func init() {
399+
FloatType.Dict["is_integer"] = MustNewMethod("is_integer", func(self Object) (Object, error) {
400+
if a, ok := convertToFloat(self); ok {
401+
f, err := FloatAsFloat64(a)
402+
if err != nil {
403+
return nil, err
404+
}
405+
return NewBool(math.Floor(f) == f), nil
406+
}
407+
return cantConvert(self, "float")
408+
}, 0, "is_integer() -> Return True if the float instance is finite with integral value, and False otherwise.")
409+
}
410+
397411
// Check interface is satisfied
398412
var _ floatArithmetic = Float(0)
399413
var _ conversionBetweenTypes = Float(0)

py/tests/float.py

+4
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@
3434
assert str(float("1.00")) == "1.0"
3535
assert str(float("2.010")) == "2.01"
3636

37+
doc="is_integer"
38+
assert (1.0).is_integer() == True
39+
assert (2.3).is_integer() == False
40+
3741
doc="finished"

0 commit comments

Comments
 (0)