On 04/09/12 11:01, jballanc (Joshua Ballanco) wrote:
>
> Issue #6973 has been updated by jballanc (Joshua Ballanco).
>
>
> Just a suggestion, but I don't think "integral" really explains what
> you want this method to do, and it is confusingly close to
> "integer?". Why not Numeric#whole_number?

I could live with that.  I think the problem is actually that #integer?
is unfortunately-named, but let's put that to one side :-)

> Personally, I'm not entirely convinced this method is a good idea.
> With Floats, you can't really guarantee that "4.0" is the same as
> "4". It could actually be "4.00000000000000001" but the precision is
> lost.

This is actually not true.  If you have a float with a value of 4.0,
then that genuinely *is* the same as 4.  The whole number value "4" has
a precise representation in floating point format, and if you have that
value then it makes no sense to say that what you have might not be
equal to 4.

Floats are precise, just like Integers are.  *Operations* on floats are
not accurate, and that's where the confusion comes from.  That's
inherent to the representation and something you've got to deal with
anyway, so I certainly don't see this test as making that problem worse.

> If you want to know if `my_float.floor == my_float`, that check
> isn't so hard to do and also has a very specific meaning. I could
> *maybe* see including this sort of method in BigDecimal or Rational
> (though in that case it would essentially be the same as
> `my_rational.denomenator == 1`). What sort of use case were you
> picturing?

This came up in #6958, specifically because BigDecimal's #integer? as
inherited from Numeric is confusing.

>
> Finally, just a note on the patch: there are easier (i.e. without
> allocation) ways to determine if a float, bigdecimal, or rational is
> a whole number. Why not give all of the subclasses in the stdlib
> specific implementations in addition to the generic implementation in
> Numeric?

Sure.  Interface before implementation.

--
Alex

> an #integral? method to Numeric to test for whole-number values
> https://bugs.ruby-lang.org/issues/6973#change-29171
>
> Author: regularfry (Alex Young) Status: Open Priority: Normal
> Assignee: Category: Target version: 2.0.0
>
>
> Numeric#integer? checks whether an instance is an Integer.  It is
> often useful to check whether the value of a non-Integer variable is
> actually a whole number, and the #integer? method doesn't help here.
>
> This patch adds Numeric#integral?, which performs this check.
>
>