Convert double to long if lossless

drug drug2004 at bk.ru
Wed Jan 20 08:31:58 UTC 2021


On 1/19/21 9:28 PM, Per Nordlöw wrote:
> On Tuesday, 19 January 2021 at 16:14:17 UTC, drug wrote:
>>>    https://dlang.org/phobos/std_bitmanip.html#FloatRep
> 
> Doesn't this pattern already cover all possible cases of `value` needed?
> 
> void f(double value)
> {
>      auto lvalue = cast(long)value;
>      if (lvalue == value) // `value` lacks fraction and in range 
> [long.min .. long.max]
>      {
>          // use long lvalue
>          return;
>      }
>      // use double value
> }
> 

Sure. You don't even need to use isFinite like I wrote above because 
special values like inf and nan become some long values and will never 
equal to itself.

Your question motivated me to do a little workout for my brains. Sorry 
if it confused you.


More information about the Digitalmars-d-learn mailing list