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

anonymous anonymous at example.com
Thu Aug 29 02:19:10 PDT 2013


On Thursday, 29 August 2013 at 08:26:11 UTC, Jonathan M Davis 
wrote:
> On Thursday, August 29, 2013 10:07:31 Paul Jurczak wrote:
>> On Thursday, 29 August 2013 at 07:51:40 UTC, Jonathan M Davis
>> wrote:
>> [..]
>> 
>> > as any integral value in a float will fit in an
>> > int.
>> 
>> [..]
>> 
>> Will it? Most of them will not fit
>
> Sure, they will. float has 32 bits, just like int, so it can't 
> possibly hold a
> value larger than an int can hold.

float can't hold more distinct values than int, but a float can 
have integer values greater than int.max. (Also, there are int 
values that a float cannot store.)

> This code passes with flying colors
>
>     foreach(i; int.min .. int.max)
>         assert(cast(float)i == i);

That's because both sides are casted to float.

[...]
> float.max isn't an integral value,

It's not a value that an int can store, but it's an integer in 
the mathematical sense.


More information about the Digitalmars-d-learn mailing list