LynxOS strtod 与 Linux 不一样

发布于 2024-10-07 19:24:10 字数 407 浏览 0 评论 0原文

看来 LynxOS 的 strtod 实现不能处理与 Linux 或 Solaris 相同的所有情况。我遇到的问题是我正在尝试解析一些可能包含十进制或十六进制数字的文本。

在 Linux 上,我调用

a = strtod(pStr, (char **)NULL);

并在 a 中获取输入字符串的预期值,例如 1.2345670x40

在 LynxOS 上,十进制数字可以正确解析,但十六进制解析为 0,因为它在遇到“x”时停止。查看手册页,LynxOS 的 strtod 似乎仅支持输入中的十进制字符串。

这里有人知道可以在 Lynx 和 Linux 上运行的替代方案吗?

It seems that LynxOS's implementation of strtod doesn't handle all the same cases as that of Linux, or for that matter Solaris. The problem I have is that I am trying to parse some text that can have decimal or hexadecimal numbers in it.

On Linux I call

a = strtod(pStr, (char **)NULL);

and I get the expected values in a for input strings such as 1.234567 and 0x40.

On LynxOS, the decimal numbers parse correctly, but the hex parses simply as 0 due to stopping when it hits the 'x'. Looking at the man pages, it seems that LynxOS's strtod only supports decimal strings in the input.

Does anyone here know of an alternative that will work on both Lynx and Linux?

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

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

发布评论

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

评论(2

绮烟 2024-10-14 19:24:10

引自标准 (7.20.1.3) ( http:// /www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf

主题序列的预期形式是可选的加号或减号,然后是以下之一
以下:
— 十进制数字的非空序列,可选地包含小数点
字符,然后是 6.4.4.2 中定义的可选指数部分;
— 0x 或 0X,然后是一个非空的十六进制数字序列,可选地包含
小数点字符,然后是 6.4.4.2 中定义的可选二进制指数部分;
— [...]

因此,您在 LynxOS 上使用的编译器不是 C99 编译器。


我的 C89 标准副本没有引用 0x 前缀:

4.10.1.4 strtod 函数

[...]

主题序列的预期形式是可选的加号或
减号,然后是一个非空数字序列,可选地包含
小数点字符,然后是可选的指数部分 [...]

Quote from the Standard (7.20.1.3) ( http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf )

The expected form of the subject sequence is an optional plus or minus sign, then one of
the following:
— a nonempty sequence of decimal digits optionally containing a decimal-point
character, then an optional exponent part as defined in 6.4.4.2;
— a 0x or 0X, then a nonempty sequence of hexadecimal digits optionally containing a
decimal-point character, then an optional binary exponent part as defined in 6.4.4.2;
— [...]

So, the compiler you're using on LynxOS is not a C99 compiler.


My copy of the C89 Standard has no reference to the 0x prefix:

4.10.1.4 The strtod function

[...]

The expected form of the subject sequence is an optional plus or
minus sign, then a nonempty sequence of digits optionally containing a
decimal-point character, then an optional exponent part [...]

疯到世界奔溃 2024-10-14 19:24:10

strtod 采用 3 个参数,而不是两个。如果您通过包含正确的标头 (stdlib.h) 对其进行原型设计,您的编译器将会发出错误。由于您使用错误的签名调用函数,因此您的程序具有未定义的行为。解决这个问题,一切都会好起来的。

strtod takes 3 arguments, not two. If you had prototyped it by including the correct header (stdlib.h), your compiler would have issued an error. Since you're calling a function with the wrong signature, your program has undefined behavior. Fix this and everything will be fine.

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