Boost logo

Boost :

From: Greg Chicares (chicares_at_[hidden])
Date: 2001-08-26 19:20:52


scleary_at_[hidden] wrote:
>
> > > int_traits::is_zero(x) -- returns true if x == 0
> > > int_traits::is_positive(x) -- returns true if x >= 0
> > > int_traits::is_odd(x) -- returns true if (x % 2) == 1
> >
> > These predicates are not integer specific but number specific.
> > If something like this is done it should be applicable to any
> numeric type.
>
> is_zero and is_positive, I would agree. I would argue that is_odd is
> integer specific, since !is_odd implies is_even.

Suppose
  long double i;
is used to capture the integer part of a long double 'd'
by using modfl() [1]. This is then an integer that is not
necessarily representable in any integer type. But it is
nevertheless useful to be able to tell whether it's odd or
even, for instance to implement bankers rounding on 'd'.

I think the right way to do this is

  long double half_of_i = floorl(0.5L * i);
  bool even = i == 2.0L * half_of_i;

Of course, this would make sense only if the floating-
point number is integer-valued; that could be another
useful trait.

[1] I use the C99 names modfl(), floorl() for clarity;
C++ requires equivalent functions [26.5/6] although
not by those names.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk