Re: checking whether a double is in the range of long

2023-04-23 Thread Ben Pfaff
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

Re: checking whether a double is in the range of long

2023-04-22 Thread Paul Eggert
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

Re: checking whether a double is in the range of long

2023-04-22 Thread Ben Pfaff
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

Re: checking whether a double is in the range of long

2023-04-22 Thread Bruno Haible
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

Re: checking whether a double is in the range of long

2023-04-22 Thread Ben Pfaff
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'. > >

checking whether a double is in the range of long

2023-04-22 Thread Ben Pfaff
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