Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an implicit type promotion in "float = float - float"?

We are using QA-C for MISRA C++ conformance, but the tool spews out an error for code like this:

float a = foo();
float b = bar();
float c = a - b;

As far as I understand, this has no implicit type promotion as everything will happen in float-sized chunks, but the tool tells me that the subtraction causes one. Is there any situation where there might be implicit promotion?

like image 968
Ken Y-N Avatar asked Apr 26 '19 07:04

Ken Y-N


People also ask

Can int be promoted to float?

Why not promote both the int and the float to a double ? float can represent the range of int values, it just can't represent those longer that 7 digits exactly (and double can't represent more than 15 digits exactly).

Can we multiply int and float in C++?

C++ Multiplication of Integer and Floating Point NumberYou can multiply an integer and floating point number using multiplication operator.

What is difference between float and double in C++?

double has 2x more precision than float. float is a 32-bit IEEE 754 single precision Floating Point Number – 1 bit for the sign, 8 bits for the exponent, and 23* for the value. float has 7 decimal digits of precision.

What is the use of type conversion?

Type Conversion refers to conversion from one type to another. The main idea behind type conversion is to make variable of one type compatible with variable of another type to perform an operation. For example, to find the sum of two variables, one of int type & other of float type.

What is type promotion in C?

Type promotion in C is a method to convert any variable from one datatype to another. C allows variables of different datatypes to be present in a single expression. There are different types of type conversions available in C. They are Implicit type conversion and Explicit type conversion.

Is implicit type promotion the biggest flaw in C language?

The rationale behind this is to prevent accidental overflows during arithmetic, but also to allow operands with different signedness to co-exist in the same expression. Unfortunately, the rules for implicit type promotion cause much more harm than good, to the point where they might be one of the biggest flaws in the C language.

What is the integer promotion rule?

Whenever a small integer type is used in an expression, it is implicitly converted to int which is always signed. This is known as the integer promotions or the integer promotion rule.

What is implicit type conversion in C language?

Just like that in C language, different data types are different objects so they cannot be added or any arithmetic operation is not possible until and unless we convert them to the same datatype. The implicit type conversion takes place when more than one data type is present in an expression.


Video Answer


1 Answers

There is no implicit promotion involved here.

When conversions involving binary operators are involved, they are called usual arithmetic conversions.

From C++ standard, [expr]/11:

11 Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
...
(11.4) — Otherwise, if either operand is float, the other shall be converted to float.

Since both operands are float in your example, there is no such conversion or promotion.
So this could be a false positive from the tool.

like image 190
P.W Avatar answered Oct 14 '22 16:10

P.W