简体   繁体   English

C99中“算术运算”的定义是什么?

[英]What is the definition of “arithmetic operation” in C99?

In C99, the term arithmetic operation appears 16 times, but I don't see a definition for it. 在C99中,术语算术运算出现了16次,但我没有看到它的定义。

The term arithmetic operator only appears twice in the text (again without definition) but it does appear in the Index: 术语算术运算符仅在文本中出现两次(同样没有定义)但它确实出现在索引中:

arithmetic operators 算术运算符

additive, 6.5.6, G.5.2 添加剂,6.5.6,G.5.2
bitwise, 6.5.10, 6.5.11, 6.5.12 按位,6.5.10,6.5.11,6.5.12
increment and decrement, 6.5.2.4, 6.5.3.1 递增和递减,6.5.2.4,6.5.3.1
multiplicative 6.5.5, G.5.1 乘法6.5.5,G.5.1
shift, 6.5.7 转变,6.5.7
unary, 6.5.3.3 一元,6.5.3.3

Then we have + - | 然后我们有+ - | & (binary) ++ -- * (binary) / % << >> ~ as arithmetic operators, if the Index is considered normative! & (二进制) ++ -- * (二进制) / % << >> ~作为算术运算符,如果索引被认为是规范的!

Perhaps we should identify arithmetic operation as being the use of an arithmetic operator. 也许我们应该将算术运算识别为算术运算符的使用。 But F9.4.5 says that the sqrt() function is also an arithmetic operation, and refers to IEC 60559 (aka. IEEE754) for details. 但F9.4.5表示sqrt()函数也是算术运算,详细信息请参考IEC 60559(又名IEEE754)。 So there must be arithmetic operations that are not just the use of arithmetic operators. 因此,必须有算术运算,而不仅仅是算术运算符的使用。

Since we don't have a formal definition let's see if we can piece together a rationale interpretation of what an arithmetic operation should be. 由于我们没有正式的定义,让我们看看我们是否可以拼凑出对算术运算应该是什么的基本解释。 This will be speculative but I can not find any obvious defect reports or open issues that cover this. 这将是推测性的,但我找不到任何明显的缺陷报告或未解决的问题。

I guess I would start with what are considered arithmetic types, which is covered in section 6.2.5 Types paragraph 18 says ( emphasis mine going forward ): 我想我会从所谓的算术类型开始,这将在第6.2.5节中说明类型18段说( 强调我的未来 ):

Integer and floating types are collectively called arithmetic types. 整数和浮点类型统称为算术类型。 Each arithmetic type belongs to one type domain: the real type domain comprises the real types, the complex type domain comprises the complex types. 每种算术类型属于一种类型域:真实类型域包括真实类型,复杂类型域包括复杂类型。

ok, so we know that an arithmetic operation has to operate on either an integer or a floating point type. 好的,所以我们知道算术运算必须对整数或浮点类型进行操作。 So what is an operation ? 什么是手术 It seems like we have a good go at defining that from section 5.1.2.3 Program execution paragraph 2 which says: 似乎我们很好地定义了第5.1.2.3程序执行2段中的内容:

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, 11) which are changes in the state of the execution environment. 访问易失性对象,修改对象,修改文件或调用执行任何这些操作的函数都是副作用, 11)这些是执行环境状态的变化。 [...] [...]

So modifying an object or call a function that does that, it is an operation. 因此修改对象或调用执行该操作的函数,这是一个操作。 What is an object ? 什么是对象 Section 3.14 says: 3.14节说:

region of data storage in the execution environment, the contents of which can represent values 执行环境中的数据存储区域,其内容可以表示值

Although the standard seems to use the term operation more loosely to mean an evaluation, for example in section 7.12.1 Treatment of error conditions it says: 虽然标准似乎更宽松地使用术语操作来表示评估,例如在7.12.1部分的错误条件处理中,它说:

The behavior of each of the functions in is specified for all representable values of its input arguments, except where stated otherwise. 除非另有说明,否则为其输入参数的所有可表示值指定每个函数的行为。 Each function shall execute as if it were a single operation without generating any externally visible exceptional conditions. 每个函数都应该像单个操作一样执行,而不会产生任何外部可见的异常条件。

and in section 6.5 Expressions paragraph 8 which says: 并在第6.5表达8段中说:

A floating expression may be contracted, that is, evaluated as though it were an atomic operation [...] 浮动表达式可能会被收缩,也就是说,它被评估为原子操作[...]

So this would seem to imply that an evaluation is an operation. 所以这似乎暗示评估是一项操作。

So it would seem from these sections that pretty much all the arithmetic operators and any math function would fall under a common sense definition of arithmetic operation . 因此,从这些部分看来,几乎所有的算术运算符和任何数学函数都属于算术运算的常识定义。

The most convincing bit I could find to be an implicit definition lies in 7.14 Signal Handling , paragraph 3, in the definition of the SIGFPE signal: 我能找到的最有说服力的一个隐含定义在于SIGFPE信号定义中的7.14信号处理 ,第3段:

SIGFPE - an erroneous arithmetic operation, such as a zero divide or an operation resulting in overflow SIGFPE - 错误的算术运算,例如零分频或导致溢出的运算

One might then draw a conclusion that any operation that may cause SIGFPE to be raised can be considered an arithmetic operation; 然后可以得出结论,任何可能导致SIGFPE被引发的操作都可以被认为是算术运算; only arithmetic operations can result in the SIGFPE signal being raised. 只有算术运算才能导致SIGFPE信号被引发。

That covers pretty much anything in <math.h> and the arithmetic operators, and <complex.h> if implemented. 这几乎涵盖了<math.h>和算术运算符中的任何内容,如果实现了<complex.h> While a signal may not be raised for integral types, signed overflow and other "exceptional" conditions are allowed to generate trap representations, which means no other operations may be carried out reliably until a valid value is obtained — something that can only be done via assignment. 虽然可能不会针对整数类型引发信号,但允许有符号溢出和其他“异常”条件生成陷阱表示,这意味着在获得有效值之前不能可靠地执行其他操作 - 这只能通过分配。 In other words, the definition can apply equally to operations on an integral value. 换句话说,该定义可以同样适用于对整数值的运算。

As a result, pretty much any operation other than getting the size of an object/type, dereferencing a pointer, and taking the address of an object may be considered an arithmetic operation. 因此,除了获取对象/类型的大小,取消引用指针以及获取对象的地址之外,几乎任何操作都可以被认为是算术运算。 Note that a[n] is *((a) + (n)) , so even using an array can be considered an arithmetic operation. 注意a[n]*((a) + (n)) ,因此即使使用数组也可以认为是算术运算。

An arithmetic operation involve manipulation of numbers . 算术运算涉及数字的操纵 sqrt also manipulate numbers and that could be the reason that standard says it an arithmetic operation. sqrt也操纵数字,这可能是标准称它为算术运算的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM