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?
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).
C++ Multiplication of Integer and Floating Point NumberYou can multiply an integer and floating point number using multiplication operator.
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.
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.
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.
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.
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.
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.
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 isfloat
, the other shall be converted tofloat
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With