On Sat, Apr 22, 2023 at 10:40 PM Paul Eggert wrote:
> On 2023-04-22 16:34, Ben Pfaff wrote:
> > determine whether converting 'd' to 'long' would
> > yield a 'long' with the same value as 'd'
>
> LONG_MIN - 1.0 < d && d < LONG_MAX + 1.0 && d == (long) d
>
> On all practical platforms this should av
On 2023-04-22 16:34, Ben Pfaff wrote:
determine whether converting 'd' to 'long' would
yield a 'long' with the same value as 'd'
LONG_MIN - 1.0 < d && d < LONG_MAX + 1.0 && d == (long) d
On all practical platforms this should avoid undefined behavior and
works correctly even if rounding occur
On Sat, Apr 22, 2023 at 5:52 PM Bruno Haible wrote:
>
> Ben Pfaff wrote:
> > determine whether converting 'd' to 'long' would
> > yield a 'long' with the same value as 'd'.
>
> Maybe
> d == (double) (long) d
> ?
>
> Just a wild guess. I haven't tested it.
I don't trust the undefined behavior in
Ben Pfaff wrote:
> determine whether converting 'd' to 'long' would
> yield a 'long' with the same value as 'd'.
Maybe
d == (double) (long) d
?
Just a wild guess. I haven't tested it.
Bruno
On Sat, Apr 22, 2023 at 4:34 PM Ben Pfaff wrote:
> Before this afternoon, I thought that a check like this for a double 'd':
> d == floor (d) && d >= LONG_MIN && d <= LONG_MAX
> was sufficient to determine whether converting 'd' to 'long' would
> yield a 'long' with the same value as 'd'.
>
>
Before this afternoon, I thought that a check like this for a double 'd':
d == floor (d) && d >= LONG_MIN && d <= LONG_MAX
was sufficient to determine whether converting 'd' to 'long' would
yield a 'long' with the same value as 'd'.
Now I realize that this is wrong. In particular, take a look