++ / - 具有浮点数和双精度数的运算符 [英] ++ / -- operators with floats and doubles

查看:101
本文介绍了++ / - 具有浮点数和双精度数的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我在一段代码中发现一个浮点数用++递增。

出于某种原因(错误,我猜......)我认为只有整数类型

可以这样递增,我从未见过浮动或双打

之前增加。只是为了确保我做了一个玩具程序和

用gcc -ansi -pedantic -Wall编译,它没有任何错误

或警告,显示正确的结果。

用++和 - 增加浮点数或双精度是否正确?是定义了
还是我进入了未定义行为的领域?如果它是正确的,那你能想到一个有用的地方吗?

非常感谢你的时间,

Al

Yesterday I found in a piece of code a float being incremented with ++.
For some reason (wrong, I guess...) I thought that only integer types
could be incremented that way, and I had never seen floats or doubles
incremented that way before. Just to be sure I did a toy program and
compiled with gcc -ansi -pedantic -Wall and it worked without any error
or warning, showing the correct result.
Is it correct to increment floats or doubles with ++ and --? Is it
defined or am I getting into the realm of undefined behaviour? If it''s
correct, can you think of a place where it could be useful?
Many thanks for your time,
Al

推荐答案

我的************** @ gmail.com 写道:
my**************@gmail.com wrote:
用+增加浮点数或加倍数是否正确+和 - ?它是定义还是我进入未定义行为的领域?


任何标量类型都可以递增。

你也可以增加指针。

如果它是
是的,你能想到一个有用的地方吗?
Is it correct to increment floats or doubles with ++ and --? Is it
defined or am I getting into the realm of undefined behaviour?
Any scalar type may be incremented.
You can also increment pointers.
If it''s
correct, can you think of a place where it could be useful?




任何时候你可能想要将浮点数的值增加一个。

可能会发生,对吗?


-

pete



Any time that you might want to increase the value of a float by one.
It could happen, right?

--
pete


>任何时候你可能想要将浮点数的值增加一个。
> Any time that you might want to increase the value of a float by one.
它可能会发生,对吧?




我认为这个问题浮动就是这个,取决于浮动的大小,它可能是一个noop。


Jon

- -

学习使用Linux汇编语言进行编程
http://www.cafeshops.com/bartlettpublish.8640017




" Jonathan Bartlett" <乔***** @ eskimo.com>写了

"Jonathan Bartlett" <jo*****@eskimo.com> wrote
任何时候你可能想要将浮点数的值增加一个。
它可能会发生,对吧?
Any time that you might want to increase the value of a float by one.
It could happen, right?



我认为浮动的问题在于,取决于浮动的大小,它可能是一个noop。



I think the problem with floats is that, depending on the magnitude of the
float, it may be a noop.



另一个问题是添加一个与递增是不一样的。

递增就是你在计算时所做的,增加一个就是加一个常数

恰好是统一的。

所以,如果我计算观众,因为他们将十字转门传递给一场足球比赛,那就说明了

count ++

每次有人通过时。

另一方面,目前的规则是胜利是3分,而

平局是一分,亏损没有分。如果(goalfor == goalagainst)

积分++;

这应该是

是不对的。要点+ = 1;

要明确1是一个可以改变的外部定义的常数。

规则可能会改变,因此平局得分为零而损失为例如,减去

一个。


浮点数值通常不用于计数,这就是我们

有整数。因此增加它们通常没有意义。


The other problem is that adding one isn''t the same as incrementing.
Incrementing is what you do when you count, adding one is adding a constant
that happens to be unity.
So if I am counting spectators as they pass throught he turnstiles to a
football match, it makes sense to say
count++
every time someone passes through.
On the other hand current rules are that a win is three points, whilst a
draw is one point, and a loss no points. It is not right to say
if(goalsfor == goalsagainst)
points++;
It should be
points += 1;
To make clear that 1 is an externally-defined constant that could change.
The rules could change so that a draw scores zero whilst a loss is minus
one, for example.

Floating point values aren''t generally used for counting, that''s what we
have integers for. So it doesn''t usually make sense to increment them.


这篇关于++ / - 具有浮点数和双精度数的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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