From: "mame (Yusuke Endoh)" Date: 2021-09-22T21:04:30+00:00 Subject: [ruby-core:105380] [Ruby master Bug#18187] Float#clamp() returns ArgumentError (comparison of Float with 1 failed) Issue #18187 has been updated by mame (Yusuke Endoh). On my machine, the code raises `comparison of Float with 0 failed`, instead of `... with 1 failed`. I have no idea where `1` comes from. ``` $ ruby -ve 'Float::NAN.clamp(0, 100)' ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux] -e:1:in `clamp': comparison of Float with 0 failed (ArgumentError) from -e:1:in `
' ``` BTW, I have no opinion about what `Float::NAN.clamp(0, 100)` should return or raise. ---------------------------------------- Bug #18187: Float#clamp() returns ArgumentError (comparison of Float with 1 failed) https://bugs.ruby-lang.org/issues/18187#change-93794 * Author: SouravGoswami (Sourav Goswami) * Status: Open * Priority: Normal * ruby -v: ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux] * Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN ---------------------------------------- When I have a Float::NAN as a number, I expect all the method to work properly. For example, `Float::NAN - 1` gives NAN. But Float::NAN.to_i raises FloatDomainError. But in case of clamp(), Float::NAN.clamp(0, 100) returns `ArgumentError (comparison of Float with 1 failed)` This error doesn't explain what's actually wrong. I didn't write the comparison to compare Float with 1. I didn't pass any invalid argument either. This error is a reflection of what's going on in the C level, which shouldn't appear to the user. If I write a vanilla clamp() in ruby: ``` Float.define_method(:clamp2) { |min, max| self < min ? min : self > max ? max : self } ``` In this case, I can call it like this: ``` > 8.0.clamp2(10, 100) => 10 > 80.0.clamp2(10, 100) => 80.0 > 800.0.clamp2(10, 100) => 100 > Float::NAN.clamp2(10, 100) => NaN ``` As you can see, it just returns NAN. But in case of the built-in clamp, it raises the ArgumentError, even though my arguments are just correct. So this should handle this clamp() correctly, either returning the min value or `Float::NAN`. -- https://bugs.ruby-lang.org/ Unsubscribe: