January 19, 2021
On 1/19/21 10:28 AM, 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?

I think so. I just remembered FloatRep as a cleaner tool compared to shifting bits explicitly.

Ali


January 19, 2021
On Tuesday, 19 January 2021 at 11:42:17 UTC, Per Nordlöw wrote:
> I want to convert a double to a long if conversion is lossless (without fractional part, non-nan, non-inf, within long-range, etc).

And then? I mean: What do you want to do with the long what could not
be done with the double in the first place?

BTW: Has anybody noticed that there already might have been a loss of precision
even before the conversion to long, since the mantissa of the double only
entails 53 bit?

   long q = 1L << 53;
   long r = q + 1;
   assert (q != r);
   double s = q, t = r;
   assert (s == q);
   assert (t == r);
   assert (s != t); // fail

January 19, 2021
On Tuesday, 19 January 2021 at 21:04:51 UTC, kdevel wrote:
> And then? I mean: What do you want to do with the long what could not
> be done with the double in the first place?

Motivated question.

We want to propagate to our system locally that a double received from a network communication actually is an integer and should be stored and treated as such in an algebraic type.
January 20, 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.
1 2
Next ›   Last »