-
Notifications
You must be signed in to change notification settings - Fork 898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid direct float comparisons with 0.0 in storage.rb #20871
Conversation
app/models/storage.rb
Outdated
@@ -581,12 +581,12 @@ def used_space | |||
alias_method :v_used_space, :used_space | |||
|
|||
def used_space_percent_of_total | |||
total_space.to_f == 0.0 ? 0.0 : (used_space.to_f / total_space.to_f * 1000.0).round / 10.0 | |||
total_space.to_f.zero? ? 0.0 : (used_space.to_f / total_space * 1000.0).round / 10.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to cast total_space.to_f
if we're just going to check for .zero?
? Wonder if total_space.zero? ? 0.0 : (used_space.to_f / total_space * 1000.0).round / 10.0
would be cleaner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, updated.
Checked commits https://github.com/djberg96/manageiq/compare/a950a904541618be3be24d9cff83b18831fc99cd~...1b76d191843d09ae8df9715343150a1ccea6ca31 with ruby 2.6.3, rubocop 0.82.0, haml-lint 0.35.0, and yamllint |
Yeah, this broke CI on ui-classic:
Apparently |
Currently the rubocop linter gives us a handful of warnings for the Storage class like this:
app/models/storage.rb:621:5: W: Lint/FloatComparison: Avoid (in)equality comparisons of floats as they are unreliable. used_space.to_f == 0.0 ? 0.0 : (debris_size.to_f / used_space.to_f * 1000.0).round / 10.0
So, this PR just switches them to use the
zero?
method. Core Ruby appears to have its own code for checking floats vs. zero (which the zero? method uses):https://github.com/ruby/ruby/blob/master/numeric.c#L1122-L1126
If we feel this isn't worthwhile, then I would vote to update our rubocop config to exclude these.
Update: originally I was only fixing the linter warnings, but I also went ahead and fixed the
Style/FloatDivision
warnings as well while I was here.