为什么不是C具有无符号的花车? [英] Why doesn't C have unsigned floats?

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

问题描述

我知道,这个问题似乎是陌生的。程序员有时候想太多。请往下看......

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

在C I使用符号和无符号整数很多。我喜欢这样的编译器警告我,如果我不喜欢的东西,以一个无符号的变量分配一个符号整数的事实。我得到警告,如果我比较无符号整数,并签署更多。

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.

我喜欢这些警告。他们帮助我保持code正确。

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.

顺便说一句 - 我真的不热衷于precision,我可以从彩车拆卸招牌标志得到的一个额外位。我与浮动能够像他们现在超开心。我只想有时标记持股量无符号和得到同样的警告,我和整数得到的。

Btw - I'm not really keen about the single extra bit of precision that I could get by removing the sign-flag 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执行不等同机code这样的操作。因此,这将是非常低效的支持。

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 ++那样支持它,那么你就有时用一个无符号float和没有意识到,你的表现刚刚被杀害。如果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.

您可以避开不通过创建一个封装float或double,如果你尝试在一个负数传递抛出警告无符号浮动类,但是有一个无符号的浮点类型。这是效率较低,但可能,如果你不使用它们强烈,你不会在乎那轻微的性能损失。

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天全站免登陆