Re: Convert double to long if lossless

2021-01-20 Thread drug via Digitalmars-d-learn
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

Re: Convert double to long if lossless

2021-01-19 Thread Per Nordlöw via Digitalmars-d-learn
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

Re: Convert double to long if lossless

2021-01-19 Thread kdevel via Digitalmars-d-learn
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

Re: Convert double to long if lossless

2021-01-19 Thread Ali Çehreli via Digitalmars-d-learn
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

Re: Convert double to long if lossless

2021-01-19 Thread Per Nordlöw via Digitalmars-d-learn
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

Re: Convert double to long if lossless

2021-01-19 Thread Per Nordlöw via Digitalmars-d-learn
On Tuesday, 19 January 2021 at 17:03:53 UTC, Ali Çehreli wrote: I think it would be a useful improvement. Indeed. Maybe Ilya could help out adding this to dmd.

Re: Convert double to long if lossless

2021-01-19 Thread Schrom, Brian T via Digitalmars-d-learn
I'm probably missing something, but on the off chance you aren't aware, there is http://phobos.dpldocs.info/std.math.modf.html .

Re: Convert double to long if lossless

2021-01-19 Thread Ali Çehreli via Digitalmars-d-learn
On 1/19/21 8:14 AM, drug wrote: > P.S. shouldn't compiler emit the error if a literal can't be represented > lossless? I think it would be a useful improvement. Ali

Re: Convert double to long if lossless

2021-01-19 Thread drug via Digitalmars-d-learn
On 1/19/21 6:50 PM, Ali Çehreli wrote: On 1/19/21 6:04 AM, drug wrote: > Another (low level) way is to shift mantissa left by exponent value. Luckily, we already have a helper in Phobos:   https://dlang.org/phobos/std_bitmanip.html#FloatRep Ali That makes life simpler, thanks for

Re: Convert double to long if lossless

2021-01-19 Thread Ali Çehreli via Digitalmars-d-learn
On 1/19/21 6:04 AM, drug wrote: > Another (low level) way is to shift mantissa left by exponent value. Luckily, we already have a helper in Phobos: https://dlang.org/phobos/std_bitmanip.html#FloatRep Ali

Re: Convert double to long if lossless

2021-01-19 Thread drug via Digitalmars-d-learn
On 1/19/21 5:04 PM, drug wrote: On 1/19/21 4:48 PM, Per Nordlöw wrote: On Tuesday, 19 January 2021 at 13:36:58 UTC, Steven Schveighoffer wrote: Use a cast instead. const lvalue = cast(long)value; Ahh, good point. Followd by a compare of the original value I presume. don't forget to check

Re: Convert double to long if lossless

2021-01-19 Thread drug via Digitalmars-d-learn
On 1/19/21 4:48 PM, Per Nordlöw wrote: On Tuesday, 19 January 2021 at 13:36:58 UTC, Steven Schveighoffer wrote: Use a cast instead. const lvalue = cast(long)value; Ahh, good point. Followd by a compare of the original value I presume. don't forget to check by std.math.isFinite before

Re: Convert double to long if lossless

2021-01-19 Thread Per Nordlöw via Digitalmars-d-learn
On Tuesday, 19 January 2021 at 13:36:58 UTC, Steven Schveighoffer wrote: Use a cast instead. const lvalue = cast(long)value; Ahh, good point. Followd by a compare of the original value I presume.

Re: Convert double to long if lossless

2021-01-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/19/21 6:42 AM, 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). I currently have void foo() {     const double value = 10.1;     try     {     import std.conv : to;    

Convert double to long if lossless

2021-01-19 Thread Per Nordlöw via Digitalmars-d-learn
I want to convert a double to a long if conversion is lossless (without fractional part, non-nan, non-inf, within long-range, etc). I currently have void foo() { const double value = 10.1; try { import std.conv : to; const lvalue = value.to!long; if (lvalue