Bug #17114
closedFloat is not properly kept as integer when integer is added without space
Description
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
. With -1
, I get:
1.0.floor -1 # => 0
Best regards,
Tammo
Updated by jeremyevans0 (Jeremy Evans) over 4 years ago
- Status changed from Open to Rejected
This is expected. 1.0.floor + 1
is parsed as ((1.0).floor).+(1)
. 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)
and (1.0).floor(-1)
are both 0
, since (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ý) over 4 years 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.
https://ruby-doc.org/core-2.7.1/Float.html#method-i-floor
Returns a floating point number when ndigits is positive, otherwise returns an integer.
Updated by tammo (tammo tjarks) over 4 years ago
Hello,
my fault. Thank you for clarification.
Best regards,
Tammo