简体   繁体   English

K&R C章节练习int浮动吗?

[英]K&R C Chapter exercise int to float?

Section 1.3 of Kernighan and Ritchie uses the following temperature conversion program to introduce For statements: Kernighan和Ritchie的1.3节使用以下温度转换程序引入For语句:

#include <stdio.h>

/* print Fahrenheit-Celsius table */

int main(){

        int fahr;
        for (fahr = 0; fahr <= 300; fahr = fahr + 20)
            printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}

What it doesn't explain is how this program can make use of an int variable to produce float values without an error. 它没有解释的是该程序如何利用int变量生成浮点值而不会出错。 What makes this valid? 是什么使它有效? Don't all the variables in an equation have to be of the same type? 方程中的所有变量都不必都属于同一类型吗? I'm hesitant to move forward without understanding this. 我犹豫不决,不了解这一点。

The conversion are implicit, but enabling warnings should give you a message explaining that int is being converted to double. 转换是隐式的,但是启用警告应该会给您一条消息,说明int正在转换为double。

Here (5.0/9.0)*(fahr-32)) , a double is being multiplied by an int, and the int is converted to double using usual arithmetic conversions. (5.0/9.0)*(fahr-32)) ,将double乘以一个int,然后使用常规算术转换将int转换为double。

The types in arithmetic don't necessarily have to match. 算术中的类型不一定必须匹配。 C has a whole chapter dedicated to conversions: 6.3 Conversions, 6.3.1 Arithmetic operands. C有一整章专门介绍转换:6.3转换,6.3.1算术操作数。

Actually it doesn't: (5.0/9.0)*(fahr-32) is an expression of type double , not a float . 实际上它不是: (5.0/9.0)*(fahr-32)double类型的表达式, 而不是 float类型。

The reason being that 5.0 and 9.0 are double literals and the (fahr - 32) is promoted to that type before the multiplication takes place. 原因是5.0和9.0是double字面量,并且(fahr-32)在乘法发生之前被提升为该类型。

If you really want a float then use (5.0f/9.0f)*(fahr-32) . 如果您真的想要float使用(5.0f/9.0f)*(fahr-32) Note the suffixed f on the numbers (and you will need to suffix both). 请注意数字的后缀f (并且您都需要后缀两个)。

%f is an appropriate format specifier in printf for a double and a float , so there are no problems there. %fprintf用于doublefloat的适当格式说明符,因此那里没有问题。

In an arithmetic expression, if two operands are of different types (such as int and double ), the operand with the lower precision will be converted to the same type as the operand with the higher precision. 在算术表达式中,如果两个操作数的类型不同(例如intdouble ),则精度较低的操作数将转换为与精度较高的操作数相同的类型。 The exact rules can be found in section 6.3.1.8 of the online C 2011 standard . 确切的规则可以在在线C 2011标准的 6.3.1.8节中找到。

Note that this only applies for arithmetic (integer and floating-point) types, not for pointer or aggregate types. 请注意,这仅适用于算术(整数和浮点)类型,不适用于指针或聚合类型。

The reason an int can be promoted to a double without a problem -- assuming int represents a 32-bit integer and double represents a 64-bit IEEE floating point number, which is common -- is that a double has 53 bits of precision, so it has no trouble representing a 32-bit integer without loss. 可以毫无问题地将int提升为double的原因(假设int表示32位整数, double表示64位IEEE浮点数,这很常见)是double的精度为53位,因此毫不费力地表示一个32位整数而不会丢失。 Any warning would just be informational and not a cause for concern, unlike when a 32-bit integer is coerced to a float , which only has 24 bits of precision and thus could lose up to eight bits (seven for a signed int ) of precision in the cast. 与将32位整数强制转换为float (它仅具有24位精度)因此最多可能丢失8位精度(对于带符号int为7精度)不同,任何警告都只是提供信息,而无需引起关注。在演员阵容中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM