为什么 C 没有无符号浮点数? [英] Why doesn't C have unsigned floats?

查看:23
本文介绍了为什么 C 没有无符号浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,这个问题似乎很奇怪.程序员有时想得太多.请继续阅读...

I know, the question seems to be strange. Programmers sometimes think too much. Please read on...

在 C 中,我经常使用 signedunsigned 整数.我喜欢这样一个事实,即编译器会在我执行诸如将有符号整数分配给无符号变量之类的操作时警告我.如果我将有符号整数与无符号整数等进行比较,我会收到警告.

In C I use signed and unsigned integers a lot. I like the fact that the compiler warns me if I do things like assigning a signed integer to an unsigned variable. I get warnings if I compare signed with unsigned integers and much much more.

我喜欢这些警告.他们帮助我保持代码正确.

I like these warnings. They help me to keep my code correct.

为什么我们对花车没有同样的奢侈?平方根绝对不会返回负数.还有其他地方,负浮点值没有意义.无符号浮点数的完美候选.

Why don't we have the same luxury for floats? A square-root will definitely never return a negative number. There are other places as well where a negative float value has no meaning. Perfect candidate for an unsigned float.

顺便说一句 - 我并不真正热衷于通过从浮点数中删除符号位而获得的额外精度.我对 float 非常满意,因为它们现在.我有时只是想将浮点数标记为无符号,并得到与整数相同的警告.

Btw - I'm not really keen about the single extra bit of precision that I could get by removing the sign bit from the floats. I'm super happy with floats as they are right now. I'd just like to mark a float as unsigned sometimes and get the same kind of warnings that I get with integers.

我不知道有任何支持无符号浮点数的编程语言.

I'm not aware of any programming language that supports unsigned floating-point numbers.

知道为什么它们不存在吗?

Any idea why they don't exist?

我知道 x87 FPU 没有处理无符号浮点数的指令.让我们只使用带符号的浮点指令.误用(例如低于零)可以被视为未定义行为,就像未定义有符号整数溢出一样.

I know that the x87 FPU has no instructions to deal with unsigned floats. Lets just use the signed float instructions. Misuse (e.g. going below zero) could be considered undefined behaviour in the same way as overflow of signed integers is undefined.

推荐答案

为什么 C++ 不支持无符号浮点数是因为没有等效的机器代码操作供 CPU 执行.所以支持它会非常低效.

Why C++ doesn't have support for unsigned floats is because there is no equivalent machine code operations for the CPU to execute. So it would be very inefficient to support it.

如果 C++ 确实支持它,那么您有时会使用无符号浮点数,而没有意识到您的性能刚刚被扼杀.如果 C++ 支持它,则需要检查每个浮点运算以查看它是否已签名.而对于执行数百万次浮点运算的程序来说,这是不可接受的.

If C++ did support it, then you would be sometimes using an unsigned float and not realizing that your performance has just been killed. If C++ supported it then every floating point operation would need to be checked to see if it is signed or not. And for programs that do millions of floating point operations, this is not acceptable.

所以问题是为什么硬件实现者不支持它.我认为答案是最初没有定义无符号浮点标准.由于语言喜欢向后兼容,即使添加了语言也无法使用它.要查看浮点规范,您应该查看 IEEE 标准 754 浮点.

So the question would be why don't hardware implementers support it. And I think the answer to that is that there was no unsigned float standard defined originally. Since languages like to be backwards compatible, even if it were added languages couldn't make use of it. To see the floating point spec you should look at the IEEE standard 754 Floating-Point.

您可以通过创建一个无符号浮点类来解决没有无符号浮点类型的问题,该类封装了浮点数或双精度数,并在您尝试传入负数时抛出警告.这效率较低,但可能如果您不大量使用它们,您就不会关心轻微的性能损失.

You can get around not having an unsigned floating point type though by creating a unsigned float class that encapsulates a float or double and throws warnings if you try to pass in a negative number. This is less efficient, but probably if you aren't using them intensely you won't care about that slight performance loss.

我肯定看到了无符号浮点数的用处.但是 C/C++ 倾向于选择对每个人都最好的效率而不是安全性.

I definitely see the usefulness of having an unsigned float. But C/C++ tends to chose efficiency that works best for everyone over safety.

这篇关于为什么 C 没有无符号浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆