std::isnan, std::isinf not working

65 views
Skip to first unread message

Александр Гурьянов

unread,
Mar 1, 2022, 11:38:11 AM3/1/22
to emscripte...@googlegroups.com
Hi,  I faced problem that std::isnan return false for NAN. I added this debug printf to my project:

printf("res %.2f %s %s\n", res,
           std::isnan(res) ? "isnan" : "notnan",
           std::isinf(res) ? "isinf" : "notinf");

and when res is NAN, output is following:

res nan notnan notinf

I tried to make a simple test case, but in test case it works fine. So, I think this is a sort of UB, but I run project with valgrind - no errors. I tried -s ASSERTIONS=1 -s SAFE_HEAP=2, also no errors.

Any suggestions what it can be?

Sam Clegg

unread,
Mar 1, 2022, 12:18:10 PM3/1/22
to emscripte...@googlegroups.com
Perhaps if you print the actual bitpattern at the point of failure you can use that to create a repro case?   Without a repro case I think this will be hard to make progress on.  Once you have a repro please open an bug report on github.

chers,
sam


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAKOm%3DVHMsym8-XQXp7fb5xz7kaG_fV-M19aj-kUzbdAEbxG7Yg%40mail.gmail.com.

Александр Гурьянов

unread,
Mar 1, 2022, 8:47:08 PM3/1/22
to emscripte...@googlegroups.com
I tried to do this but in empty project it works correctly. I converted the float to int32_t like this:

int32_t* val = (int32_t) &res;
printf("%d\n", *val);

and the number was 2143289344, I created testcase:

#include <cmath>
#include <cstdio>

int main(int argc, char** argv) {
    int32_t nan = 2143289344;
    float* val = (float *) &nan;

    printf("val '%.2f', isnan:%s, isinf:%s\n", *val,
           std::isnan(*val) ? "yes" : "no",
           std::isinf(*val) ? "yes" : "no");

    return 0;
}

but it works correctly:

val 'nan', isnan: yes, isinf: no

Moreover in native build of project there is no NAN in problem place, it appears only in emscripten build. So, probably error happens somewhere else... Trying to understand.

ср, 2 мар. 2022 г. в 00:18, 'Sam Clegg' via emscripten-discuss <emscripte...@googlegroups.com>:

Александр Гурьянов

unread,
Mar 2, 2022, 12:49:02 AM3/2/22
to emscripte...@googlegroups.com
Okay I found the answer it's a result of using optimization flags -Oz --ffast-math. If you remove one of it - works correctly. Also behavior is exactly same in clang++, so I think there is no error from emscripten.
My last test case:

#include <cstdio>
#include <cmath>
#include <limits>

int main(int argc, char **argv) {
  float val = std::numeric_limits<float>::quiet_NaN();

  printf("val '%.2f', isnan:%s\n", val,
         std::isnan(val) ? "yes" : "no");

  return 0;
}



ср, 2 мар. 2022 г. в 08:46, Александр Гурьянов <caii...@gmail.com>:

Sam Clegg

unread,
Mar 2, 2022, 8:55:18 PM3/2/22
to emscripte...@googlegroups.com
Great news!  Thanks for investigating.

Reply all
Reply to author
Forward
0 new messages