Float is not properly kept as integer when integer is added without space
Hello dear ruby community,
I observed the following behaviour:
1.0.floor +1 # => 1.0 (1.0).floor +1 # => 1.0 (1.0.floor) +1 # => 2 1.0.floor + 1 # => 2
I think this is due to
+1 being taken as an argument to
floor. It is strange that
1.0.floor is an integer but
1.0.floor +1 is a float. Is this intended behaviour? I do not have the latest ruby version installed, so I do not know if it has been changed. I observed it only with
-1, I get:
1.0.floor -1 # => 0
Updated by jeremyevans0 (Jeremy Evans) about 1 year ago
- Status changed from Open to Rejected
This is expected.
1.0.floor + 1 is parsed as
1.0.floor +1 is parsed as
(1.0).floor(+1). This is also true for
-1, it's just that
(1.0).floor(-1) are both
(floor).floor(-1) means the floor of the 10s column. You'll get different results for
10.0.floor -1 and
10.0.floor - 1.
Updated by ahorek (Pavel Rosický) about 1 year ago
Hi, it's not a bug. +-1 is a precision argument.
irb> 12.34.floor(-1) => 10 irb> 12.34.floor(+1) => 12.3
take a look at the documentation, it describes this exact behavior well.
Returns a floating point number when ndigits is positive, otherwise returns an integer.