1 / (1 + inf) gives nan instead of 0
SaC version
sac2c 1.3.3-MijasCosta-1161-gb543c build-type: DEBUG built-by: "thomas" at 2024-02-21T12:07:11
Test program
/* We need expf so Stdlib is necessary */
use Array: all;
use Math: all;
inline
float[*] logistics(float[*] x)
{
return {iv -> 1f / (1f + exp(-x[iv]))};
}
noinline
float[d:shp] id(float[d:shp] x)
{
return x;
}
int main()
{
c11 = id(with {}: genarray([6, 24, 24], -1875001.750000f));
/* Note that this is well defined under IEEE-754:
exp(-x[iv]) = +inf
1 + +inf = +inf
1 / +inf = 0+
*/
c1 = logistics(c11);
StdIO::print(c1);
return 0;
}
Output
When compiling without arguments, prints all NaN (with gcc 13.2.0 and clang 14.0.6). When passing -Xc -O2
it does give the correct result, which is all 0
s.
Stepping through with gdb
shows that the + 1
and division are optimised away from O3 onwards. It is possible that the generated C code is incorrect, but -fsanitize=undefined
finds nothing. I may have to boil this down to a minimal example and file a bug with gcc
and clang
if it is not on our end.