为什么 (int)[@"-1" floatValue] 不返回 -1

发布于 2024-12-13 05:22:16 字数 272 浏览 4 评论 0 原文

我在 gdb 中测试了这个表达式:

(gdb) p (int)[@"-1" floatValue]
$2 = 1

(gdb) p (int)((float)[@"-1" floatValue])
$7 = -1

结果符合我的预期

为什么第一个表达式不返回 -1?另外,[@"-1" floatValue]的返回类型是什么?

I tested this expression in gdb:

(gdb) p (int)[@"-1" floatValue]
$2 = 1

but

(gdb) p (int)((float)[@"-1" floatValue])
$7 = -1

comes out as I expect

Why does the first expression not return -1? Also, what is the return type of [@"-1" floatValue]?

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

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

发布评论

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

评论(1

_失温 2024-12-20 05:22:16

gdb 不知道方法(或函数)的返回类型:

(gdb) p [@"-1" floatValue]
Unable to call function "objc_msgSend" at 0x155d08c: no return type information available.
To call this function anyway, you can cast the return type explicitly (e.g. 'print (float) fabs (3.0)')

当您将表达式转换为 int 时,gdb 假定该方法返回 int。所以它知道它可以使用 objc_msgSend 发送消息并处理从 objc_msgSend 返回一个 int 值。

当您将表达式转换为 float 时,gdb 假定该方法返回一个 float。所以它知道应该使用 objc_msgSend_fpret 发送消息并处理返回浮点数形式的值。

这很重要,因为:

在 i386 平台上,返回浮点的函数的 ABI
值与返回整数的函数的值不兼容
类型。因此,在 i386 平台上,必须使用 objc_msgSend_fpret
对于返回非整数类型的函数[原文如此]的函数。

gdb doesn't know the return type of methods (or functions):

(gdb) p [@"-1" floatValue]
Unable to call function "objc_msgSend" at 0x155d08c: no return type information available.
To call this function anyway, you can cast the return type explicitly (e.g. 'print (float) fabs (3.0)')

When you cast the expression to int, gdb assumes that the method returns an int. So it knows it can use objc_msgSend to send the message and treat the return value from objc_msgSend as an int.

When you cast the expression to a float, gdb assumes that the method returns a float. So it knows that it should use objc_msgSend_fpret to send the message and treat the return value as a float.

This is important because:

On the i386 platform, the ABI for functions returning a floating-point
value is incompatible with that for functions returning an integral
type. On the i386 platform, therefore, you must use objc_msgSend_fpret
for functions that for functions [sic] returning non-integral type.

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