无法从 MSVCRT strtod/sscanf/atof 函数获取 NaN

发布于 2024-11-28 07:15:32 字数 2077 浏览 1 评论 0原文

有没有办法将 Windows CRT string 中的 NaN 转换为 float 函数?


原因:我正在用 C 编写一个 IEEE floatstring 转换器,不会丢失信息(strtod,< (code>sscanf 或 atof 返回原始 float),前提是舍入模式不改变。

我使用的是 MinGW 或 Visual C++,因此这些调用会转到 MSVC++ 运行时。问题是我无法让它解析任何特殊值(例如 "Inf""NaN")。 Inf 没问题(它在解析不适合 float 的值后返回,例如 "1e999")。

  /* Return the shortest string representation of a float with a successful scanf round-trip.
   * Guaranteed to fit in 13 chars (including the final '\0').
   */
  char* ftoa(char* res, float f) {
     float r = 0;
     int i, j, len, e = floor(log10(f)) + 1;
     char fmt[8];
     union { float f; int32_t i; } u = { f } ;

     if (f > FLT_MAX) { sprintf(res, "1e999"); return res; }
     if (f < -FLT_MAX) { sprintf(res, "-1e999"); return res; }

     if ((u.i & 0x7F800000) == 0x7F800000) {  // NaN
        sprintf(res, u.i == 0x7FC00000 ? "%sNaN" : "%sNaN%d", u.i<0 ? "-" : "", u.i & 0x7FFFFF);
        return res;
     }  

     // compute the shortest string without exponent ("123000", "0.15")
     if (!f || e>-4 && e<10) {
        for (i=0; i<=10; i++) {
           sprintf(fmt, "%%.%df", i);
           sprintf(res, fmt, f);
           sscanf(res, "%f", &r); if (r==f) break;
        }
     }
     if (r==f) len = strlen(res);
     else len = 1e9;

     if (!f) return res;  // handle 0 and -0

     // compute the shortest string with exponent ("123e3", "15e-2")
     for (i=0; i<9; i++) {
        sprintf(res, "%.0fe%d", f * pow(10,-e), e); sscanf(res, "%f", &r); if (r==f) break;
        j = strlen(res); if (j >= lenF) break;
        while (res[j] != 'e') j--;
        res[j-1]--; sscanf(res, "%f", &r); if (r==f) break;  // try +-1
        res[j-1]+=2; sscanf(res, "%f", &r); if (r==f) break;
        e--;
     }
     if (len <= strlen(res)) sprintf(res, fmt, f);
     return res;
  }

Is there any way to get NaNs from the Windows CRT string to float functions?


Why: I'm writing an IEEE float to string converter in C with no information loss (strtod, sscanf or atof return the original float) provided the rounding mode doesn't change.

I'm under MinGW or Visual C++, so these calls go to the MSVC++ runtime. The problem is that I can't get it to parse any special values (like "Inf" or "NaN"). Inf is OK (it's returned after parsing a value that doesn't fit in a float, such as "1e999").

  /* Return the shortest string representation of a float with a successful scanf round-trip.
   * Guaranteed to fit in 13 chars (including the final '\0').
   */
  char* ftoa(char* res, float f) {
     float r = 0;
     int i, j, len, e = floor(log10(f)) + 1;
     char fmt[8];
     union { float f; int32_t i; } u = { f } ;

     if (f > FLT_MAX) { sprintf(res, "1e999"); return res; }
     if (f < -FLT_MAX) { sprintf(res, "-1e999"); return res; }

     if ((u.i & 0x7F800000) == 0x7F800000) {  // NaN
        sprintf(res, u.i == 0x7FC00000 ? "%sNaN" : "%sNaN%d", u.i<0 ? "-" : "", u.i & 0x7FFFFF);
        return res;
     }  

     // compute the shortest string without exponent ("123000", "0.15")
     if (!f || e>-4 && e<10) {
        for (i=0; i<=10; i++) {
           sprintf(fmt, "%%.%df", i);
           sprintf(res, fmt, f);
           sscanf(res, "%f", &r); if (r==f) break;
        }
     }
     if (r==f) len = strlen(res);
     else len = 1e9;

     if (!f) return res;  // handle 0 and -0

     // compute the shortest string with exponent ("123e3", "15e-2")
     for (i=0; i<9; i++) {
        sprintf(res, "%.0fe%d", f * pow(10,-e), e); sscanf(res, "%f", &r); if (r==f) break;
        j = strlen(res); if (j >= lenF) break;
        while (res[j] != 'e') j--;
        res[j-1]--; sscanf(res, "%f", &r); if (r==f) break;  // try +-1
        res[j-1]+=2; sscanf(res, "%f", &r); if (r==f) break;
        e--;
     }
     if (len <= strlen(res)) sprintf(res, fmt, f);
     return res;
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩一世无双 2024-12-05 07:15:32

不会。如果发生溢出,它们会返回 HUGE_VAL;如果输入无法解析或发生下溢,它们会返回 0。

No. They return HUGE_VAL if overflow would occur and 0 if the input can't be parsed or underflow occurs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文