Project

General

Profile

Actions

Bug #17114

closed

Float is not properly kept as integer when integer is added without space

Added by tammo (tammo tjarks) over 1 year ago. Updated over 1 year ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
[ruby-core:99555]

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 1 year 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 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.
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 1 year ago

Hello,
my fault. Thank you for clarification.

Best regards,
Tammo

Actions #4

Updated by sawa (Tsuyoshi Sawada) over 1 year ago

  • Description updated (diff)
Actions

Also available in: Atom PDF