What is a concise way to test if floating point value is integral?
Jonathan M Davis
jmdavisProg at gmx.com
Thu Aug 29 00:51:26 PDT 2013
On Thursday, August 29, 2013 08:50:47 Paul Jurczak wrote:
> On Thursday, 29 August 2013 at 06:23:18 UTC, Jonathan M Davis
>
> wrote:
> > On Thursday, August 29, 2013 07:47:16 Paul Jurczak wrote:
> >> I'm writing this rather ugly:
> >>
> >> sqrt(cast(float)D) != round(sqrt(cast(float)D)
> >>
> >> line and I'm looking for more concise notation without
> >> introducing a meaningless variable to hold expression being
> >> tested. Is there an equivalent of std.math.trunc(), which would
> >> return fractional portion instead, maybe frac()?
> >
> > There may be something in std.math which makes it cleaner, but
> > I would have
> > thought that the to test whether a floating point value is an
> > integral value,
> > you'd just cast it to an integral type and then compare that
> > against the
> > original. e.g.
> >
> > auto isIntegral = cast(int)my_float == my_float;
> >
> > - Jonathan M Davis
>
> Isn't type conversion:
>
> auto isIntegral = to!int(my_float) == my_float;
>
> more recommended/idiomatic D way to deal with built-in types?
Not really IMHO. std.conv.to does fancier conversions than casting does, and
it throws in cases of overflow, neither of which are of any benefit when
converting a float to an int, as any integral value in a float will fit in an
int. Using to would just create extra overhead for no extra benefit in this
case.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list