Home >Backend Development >C++ >Why does Printf() promote floats to doubles?

Why does Printf() promote floats to doubles?

DDD
DDDOriginal
2024-11-17 08:09:03531browse

Why does Printf() promote floats to doubles?

Deep dive: Why Printf() promotes floating point numbers to double precision floating point numbers

Why Printf() promotes float parameters to double, not just because it is a variadic function. According to the C Standard and the C Standard, all float arguments to variadic functions are promoted to double before being passed.

Section 6.5.2.2 of the C99 standard clearly states: "[...] Floating-point type parameters are promoted to double. These are called default parameter promotions. [...]"

C Draft Standard Section 5.2.2 The section also clarifies: "[...] If a floating-point type parameter is affected by floating-point promotion (4.6), the parameter value will be converted to the promoted type before the call. [...]"

Additionally, section 4.6 states: "A pr value of type float can be converted to a pr value of type double. The value remains unchanged."

Cppreference summarizes C well The default conversion of variable parameter functions: "

  • std::nullptr_t is converted to void*
  • float parameter is converted to double, consistent with floating point promotion
  • bool , char, short and domainless enumerations are converted to int or wider integer type, consistent with integer promotion

In C, this conversion is for compatibility with the K&R C International Standard (Programming Language - C) specification. It clearly states: "For compatibility with past practice, in the absence of prototype declarations, all parameter promotions are performed as described by K&R, including float to double promotions ." "

The above is the detailed content of Why does Printf() promote floats to doubles?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn