C programming: is it always automatic to convert float to double when multiplying mixed data types?

Stephen Prat’s book “C Primer Plus” has a section called “Type Conversions,” where the “General Rules” section is listed in Rule 1:

Under K&R C, but not under current C, float is automatically converted to double. 

http://www.9wy.net/onlinebook/CPrimerPlus5/ch05lev1sec5.html

Can someone explain what but not under current C means? Are there versions of C that automatically convert and versions that don't?

I'm trying to figure out if I have an expression that mixes floats and doubles, can I rely on C to advance floats to double when evaluating it?

+6
c
source share
5 answers

It should refer to the result of binary arithmetic operations of the format float * float . In pre-standard versions of operands C of such expressions, the value double was assigned, and the result was of type double .

For example, here is a quote from the "C Reference Guide"

If both operands are int or char, the result is int. If both are float or double, the result will be double.

In C89 / 90, this behavior has already been changed, and the float * float expressions express the result of the float .

  • If either operand is of type long double , the other operand is converted to long double
  • Otherwise, if one of the operands is double , the other operand is converted to double .
  • Otherwise, if any operand is float , the other operand is converted to float .
+8
source share

Look at the whole rule:

When char and short appear in the expression, both signed and unsigned automatically converted to int or, if necessary, to unsigned int . (If short is the same size as int , unsigned short larger than int , in which case unsigned short converted to unsigned int .) Under K & RC, but not under current C, float automatically converted to double . Since they are conversions for larger types, they are called promotions.

If we look at integer types when they appear, for example. arithmetic expressions, they are still being promoted, so arithmetic is not performed, usually in char or short types, but all in int , unsigned int type or type with a higher conversion rank (in as- if the rule, if the implementation can guarantee, that the result will be the same as if the promotion were actually performed, it can do arithmetic on smaller types if the platform provides instructions).

Similarly, used for float , according to the old pre-standard rules, float was increased to double for all arithmetic, etc.

This is no longer the case, arithmetic on float does not include automatic progress in standardized C.

In expressions with mixed types, as a rule, everything still advances to the largest involved type, so if you compare or add a float to double , the float converted to double before the operation.

+4
source share

Yes, there are differential versions of C, as well as different versions of most software products.

K & R is the original version described by Brian K ernighan and Dennis R itchie in his book C. Programming Language .

The first standardized version was ANSI C or C89, and there have been several new versions since then. “Current C” may mean C11 (latest version) or C99 (probably the most current version today).

+1
source share

Yes, you can rely on C to promote floats until doubling in pricing.

 708 Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double. 

I use the documentation found here.

Sorry, I quoted the wrong one before

+1
source share

The C language definition has been standardized and changed several times over the years. The original (non-standardized) version of C is known as "K & RC"; it is a language developed by Kernigan and Richie.

In 1989, ANSI created an official document of standards (adopted in 1990 by ISO) to define the language, and in this standard some things were changed and expanded; one of the changes is that the automatic promotion from float to double been removed.

Since then, there have been two revisions of the standard, one in 1999 and one in 2011.

I'm trying to figure out if I have an expression that mixes floats and doubles, can I rely on C to advance floats to double when evaluating it?

Here is the rule from the current standard :

6.3.1.8 Normal arithmetic conversions

...
First, if the corresponding real type of any operand is long double , the other operand is converted without changing the domain type to a type whose corresponding real type is long double .

Otherwise, if the corresponding real type of any operand is double , the other operand is converted without changing the domain type to the type corresponding to the real double type.

Otherwise, if the corresponding real type of any operand is float , the other operand is converted without changing the domain type to a type corresponding to the real type of float . 62)
62) For example, adding a double _Complex and a float entails simply converting the float double operand (and gives the result double _Complex ).

Basically, if you have an expression with two different types, then an operand with a narrower / less accurate type will advance to the type of the operand with a wider / more precise type.

+1
source share

All Articles