13

In C99, the term arithmetic operation appears 16 times, but I don't see a definition for it.

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
bitwise, 6.5.10, 6.5.11, 6.5.12
increment and decrement, 6.5.2.4, 6.5.3.1
multiplicative 6.5.5, G.5.1
shift, 6.5.7
unary, 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. So there must be arithmetic operations that are not just the use of arithmetic operators.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Presumably everything in `` is also "arithmetic"? – Kerrek SB Jul 10 '14 at 00:48
  • 1
    To be precise, the text says `sqrt is fully specified as a basic arithmetic operation in IEC 60559.` – jweyrich Jul 10 '14 at 00:56
  • IMO, an arithmetic operation is one, aside from simple equality compare, the results in each byte of the answer depends on more than just the corresponding bytes in the inputs. (There is a _carry_) – chux - Reinstate Monica Jul 10 '14 at 01:45
  • 1
    @chux so, you'd exclude the bitwise operators? – M.M Jul 10 '14 at 01:49
  • 1
    @Matt Yes, bitwise operators are better described as logical operators than arithmetic. Of course this is in the realm of opinion. – chux - Reinstate Monica Jul 10 '14 at 01:52
  • What exactly is the purpose behind this question? Perhaps it is merely curiosity? –  Jul 10 '14 at 03:15
  • 1
    @ChronoKitsune some things are specified in terms of arithmetic operation. I was motivated to this question by 6.2.6.2#1 footnote "no arithmetic operation on valid values can generate a trap representation"; however 7.14#3, F.8.1, and H.3 at least, also specify behaviour that applies only to arithmetic operations. – M.M Jul 10 '14 at 04:15
  • I guess you can spin it many ways, but to me, anything that involves a mathematical operation (separate from a bitwise or logical operation) would fall into the realm of arithmetic. This might fall into the category of the infamous question though, "what is the definition of is" :P – James H Jul 10 '14 at 04:23
  • Having said that, footnotes are non-normative so perhaps even an accepted answer to my question won't help with my original inquiry – M.M Jul 10 '14 at 04:28
  • @MattMcNabb this [post](http://stackoverflow.com/questions/6725809/trap-representation) may be helpful. You may have been better off asking a question directly about the footnote `no arithmetic operation on valid values can generate a trap representation`. – Shafik Yaghmour Jul 14 '14 at 14:08

3 Answers3

7

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):

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:

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. [...]

So modifying an object or call a function that does that, it is an operation. What is an object? Section 3.14 says:

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:

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:

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.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Of the arithmetic operators, only the compound assignment operators actually modify an object, so this definition is faulty. – Ben Voigt Jul 10 '14 at 01:56
  • @BenVoigt well it says region of storage which if it produce a value must modify a region of storage. – Shafik Yaghmour Jul 10 '14 at 01:57
  • 1
    Operators in C don't "return" values. They appear in expressions which are evaluated. Only in C++ with user-defined operator overload functions, do the concepts of operators and returning get mixed. – Ben Voigt Jul 10 '14 at 01:58
  • @BenVoigt I changed the wording to produce. – Shafik Yaghmour Jul 10 '14 at 01:59
  • And the result of an arithmetic operation is a pure rvalue, whose address cannot be taken. It is not a region of storage. – Ben Voigt Jul 10 '14 at 02:00
  • @BenVoigt as far as I can tell the term storage is used pretty generically to refer to registers etc... so I don't see it prevents it from being a prvalue. Can you quote me a stronger restriction? – Shafik Yaghmour Jul 10 '14 at 02:04
  • In the C language, only lvalues are objects. See 6.3.2.1 and the footnotes. – Ben Voigt Jul 10 '14 at 02:19
  • Sorry, I said "only the compound assignment operators", but clearly pre- and post-increment and decrement operators also modify objects. Still, that leaves out many arithmetic operators. – Ben Voigt Jul 10 '14 at 02:23
  • @BenVoigt the language seems ambiguous to me, for example later on footnotr 75 says `75) Allocated objects have no declared type`. – Shafik Yaghmour Jul 10 '14 at 02:41
  • X + Y operates on the values of X and Y, not X and Y themselves, meaning the bit at the end about "all the arithmetic operators and any math function would modify an object of integral or floating point type" is incorrect since neither X nor Y are modified. –  Jul 10 '14 at 02:54
  • @ChronoKitsune to produce a value it must modify some region of storage. This position is supported by footnote 11 which indicates changes to floating point status flags are considered side effects. I said it was speculative, there is a lot that is not nailed down in C99. – Shafik Yaghmour Jul 10 '14 at 03:02
  • 1
    Hmm, I see your point. Apparently there are many different ways to interpret what an arithmetic operation may or may not be. –  Jul 10 '14 at 03:11
3

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 - an erroneous arithmetic operation, such as a zero divide or an operation resulting in overflow

One might then draw a conclusion that any operation that may cause SIGFPE to be raised can be considered an arithmetic operation; only arithmetic operations can result in the SIGFPE signal being raised.

That covers pretty much anything in <math.h> and the arithmetic operators, and <complex.h> if implemented. 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.

1

An arithmetic operation involve manipulation of numbers. sqrt also manipulate numbers and that could be the reason that standard says it an arithmetic operation.

haccks
  • 104,019
  • 25
  • 176
  • 264