Arithmetic Conversion

This page was translated by a robot.

Arithmetic conversion is a mechanism built into C and C++ that converts two values ​​with different arithmetic types (integer or floating-point types) so that they then have the same type and, if possible, the original value. This mechanism is used for operators that expect two arithmetic operands.

Details

Operators always generate different assembler code depending on the type of linked operands. If an operator expects two operands, there must theoretically be a corresponding conversion in assembler for each type combination. However, especially for operators that expect arithmetic types such as integer or floating point types, there are almost exclusively only commands in assembler that can combine values ​​of the same type with each other. The arithmetic conversion of C and C++ thus ensures that values ​​of different types are converted into the best possible, same type, whereupon the operator can generate the appropriate assembler command.

The arithmetic conversion thus explicitly deals with the choice of a best possible type in order to allow operators which expect two arithmetic operands to be combined.

Choosing of the Conversion Type

Each arithmetic type has a so-called power , where long doublethe most powerful type is considered to be . The rule of thumb for arithmetic conversion is: the weaker is converted into the more powerful . Depending on the compiler, however, this rule can be different for certain cases. These special cases are not described on this page but referred to other sources. However, it should be noted, especially with regard to other sources, that no matter which compiler is used, the type to be converted is always determined at compile time.

The type of conversion is determined in the compilers by processing rules, with the first applicable rule being the determining one. They are listed in order from top to bottom in the following text. The first rule is:

The floating point types long double, doubleand floatare more powerful than all integer types and long doubleis the most powerful of them all. The rules for floating point types are as follows:

If none of the operands is a floating point type, the rules for integer types apply. Unfortunately, historically, integer types are not always consistent, which is why different sources from different compilers differ on this point. It is therefore not possible to specify uniform rules, but the author offers the following simple rules as a guide, which should be correct in today's compilers, at least in non-extreme cases:

It can be assumed that various standards are still in use today, which use slightly different mechanisms for the conversion. However, the above rules should cover (perhaps with the exception of certain edge cases) the basic principles of arithmetic integer conversions. If in doubt, it is advisable to use explicit casts .

If no rule has yet become applicable, the following rules will apply:

Promotions

These last two rules belong to the so-called type promotions . A conversion is called a promotion if the conversion type can describe the range of values ​​of the type to be converted completely and without loss. The specified types are all intconverted into one, which is considered to be the most efficient integer unit of a processor. A compiler generally carries out such type promotions automatically, however, depending on the compiler, a suitable assembler command could also be generated if it actually exists.

The conversion from floatto is also doublereferred to as a promotion. The type doubleis considered the most efficient floating-point type, which is able to represent the entire range of floatvalues ​​without loss. However, since this conversion often takes more time than the execution of the final operand, this promotion should be avoided whenever possible. To do this, the programmer should ensure that floats are identified as fixed values ​​in the source code and use explicit casts .

Promotions play a minor role in day-to-day programming. Since they guarantee lossless conversions, they usually do not lead to errors either. However, if the compiler itself is unclear about the type, it is forced to do the same promotions, which can lead to problems, especially in connection with parameters of variadic functions .

More

The actual conversion of the types into one another can be looked up in the corresponding types.

For many operators, the conversion type found using the above rules simultaneously declares the type of the return value of the operator, which is noted in the detailed descriptions of the operators.

The arithmetic conversion is always done automatically whenever the compiler deems it necessary. In the vast majority of cases, the automatic conversion of the values ​​runs without any problems, but there are cases where the programmer has to intervene. Conversions can thus also be forced explicitly by specifying casts .

The problem with such a conversion is that different types cannot generally be converted into one another without loss. For example, it is not possible to represent a number with decimal places (floating point number) with a number without decimal places (integer). It is also not possible to store a negative number using an unsigned type. And a number that goes beyond the range of a type cannot be converted correctly either.

A conceivable solution would be to convert all values ​​into a type that is as powerful as possible and that can represent as many values ​​as possible, such as long double. However, this would increase both the space consumption and the runtime of each program many times over, and floating point numbers are also unsuitable for many computer science problems.

When using fixed values ​​(literals) with arithmetic operators, it is advisable for various reasons to ensure that no implicit arithmetic conversions occur. For example, to specify the number 1as a floating point number, should be 1.written.

It should be noted that arithmetic conversions are not free, but cost a certain number of machine instructions. In particular, conversions of floating point numbers require complex commands on floating point registers. It is therefore worth paying attention to these conversions for time-critical applications.