What is a concise way to test if floating point value is integral?

Jonathan M Davis jmdavisProg at gmx.com
Wed Aug 28 23:23:05 PDT 2013


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


More information about the Digitalmars-d-learn mailing list