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

Paul Jurczak pauljurczak at yahoo.com
Wed Aug 28 23:50:47 PDT 2013


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?


More information about the Digitalmars-d-learn mailing list