ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "mrkn (Kenta Murata)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:105668] [Ruby master Bug#18187] Float#clamp() returns ArgumentError (comparison of Float with 1 failed)
Date: Tue, 19 Oct 2021 04:48:07 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-94167.20211019044807.25672@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18187.20210922172338.25672@ruby-lang.org

Issue #18187 has been updated by mrkn (Kenta Murata).


I think it's OK to return NaN for all the cases of `Float::NAN.clamp`.

----------------------------------------
Bug #18187: Float#clamp() returns ArgumentError (comparison of Float with 1 failed)
https://bugs.ruby-lang.org/issues/18187#change-94167

* 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/

  parent reply	other threads:[~2021-10-19  4:48 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-22 17:23 [ruby-core:105377] [Ruby master Bug#18187] Float#clamp() returns ArgumentError (comparison of Float with 1 failed) SouravGoswami (Sourav Goswami)
2021-09-22 21:04 ` [ruby-core:105380] " mame (Yusuke Endoh)
2021-09-22 21:30 ` [ruby-core:105382] " SouravGoswami (Sourav Goswami)
2021-09-23 17:55 ` [ruby-core:105398] " jeremyevans0 (Jeremy Evans)
2021-10-19  4:48 ` mrkn (Kenta Murata) [this message]
2021-10-19 11:12 ` [ruby-core:105679] " nobu (Nobuyoshi Nakada)
2021-10-21  8:11 ` [ruby-core:105718] " matz (Yukihiro Matsumoto)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-94167.20211019044807.25672@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).