简体   繁体   English

C 语言:使用计算初始化浮点变量

[英]C Language: Initializing float variable with a calculation

I haven't done a lot of floating point math programming in any language let alone in C.我没有用任何语言做过很多浮点数学编程,更不用说用 C 语言了。

I'm writing a temperature conversion program as an exercise and have a question about floating point numbers.我正在编写一个温度转换程序作为练习,并且有一个关于浮点数的问题。 I have a code frag as listed below.我有一个代码片段,如下所示。 In both cases Temp1 and Temp2 are 0.0 when P_FahrenheitTemp is <> 32.0.在这两种情况下,当 P_FahrenheitTemp <> 32.0 时,Temp1 和 Temp2 都是 0.0。 However, if I use the CF3 factor in the calculation the LOC VERRKKKS!!!但是,如果我在计算中使用 CF3 因子 LOC VERRKKKS !!! :-) :-)

This seems intuitively obvious to me but... Is this compiler dependent or is a cast operator necessary on the initialization?这对我来说似乎很直观,但是......这个编译器是依赖的​​还是初始化时必需的强制转换运算符? BTW, I'm writing this code on an IBM iSeries platform using the C/C++ compiler which strictly adheres to ASNI and ISO standards.顺便说一句,我正在使用严格遵守 ASNI 和 ISO 标准的 C/C++ 编译器在 IBM iSeries 平台上编写此代码。

Thank you in advance for any info!提前感谢您提供任何信息!

Martin Kuester马丁·库斯特

#define CF3 5/9;

float Conv2Celsius(float P_FahrenheitTemp)
{
  float Temp1, Temp2, Temp3;
  float ConvAdj = 32.0;
  float CF1 = 0.555556;
  float CF2 = 5/9;

  //[°C] = ([°F] - 32) × 5/9
  Temp1 = (P_FahrenheitTemp - ConvAdj) * CF1; 
  Temp2 = (P_FahrenheitTemp - ConvAdj) * CF2;
  Temp3 = (P_FahrenheitTemp - ConvAdj) * CF3;

  return(Temperature);
}                                

Let us look closer.让我们仔细看看。

  float CF1 = 0.555556;
  Temp1 = (P_FahrenheitTemp - ConvAdj) * CF1; 
  // same as 
  Temp1 = (P_FahrenheitTemp - ConvAdj) * (float) 0.555556;

  float CF2 = 5/9;
  Temp2 = (P_FahrenheitTemp - ConvAdj) * CF2;
  // same as 
  float CF2 = 0;  // 5/9 is integer division
  Temp2 = (P_FahrenheitTemp - ConvAdj) * 0;

  #define CF3 5/9
  Temp3 = (P_FahrenheitTemp - ConvAdj) * CF3;
  // same as 
  Temp3 = (P_FahrenheitTemp - ConvAdj) * 5 / 9;
  Temp3 = (P_FahrenheitTemp - ConvAdj) * 5.0f / 9;
  //      ^--- float  multiplication -------^             
  // same as 
  Temp3 = (P_FahrenheitTemp - ConvAdj) * 5.0f / 9.0f;
  //      ^--- float divsion ----------------------^             

Temp3 "VERRKKKS" because it is not scaling by 5/9 . Temp3 "VERRKKKS" 因为它没有按5/9缩放。 Instead it is a text substitution in the line-of-code and so multiplies by 5 and then divides by 9.相反,它是代码行中的文本替换,因此乘以 5,然后除以 9。

Temp3 is correct and best of the three. Temp3是正确的,也是三者中最好的。
Temp1 is almost correct as not as certainly precise * (float) 0.555556 as * 5.0f/9.0f . Temp1几乎是正确的,因为* (float) 0.555556不如* 5.0f/9.0f
Temp2 is wrong as the answer is always 0, even when it should not be Temp2是错误的,因为答案总是 0,即使它不应该是


I have a code frag as listed below.我有一个代码片段,如下所示。 In both cases Temp1 and Temp2 are 0.0 when P_FahrenheitTemp is <> 32.0.在这两种情况下,当 P_FahrenheitTemp <> 32.0 时,Temp1 和 Temp2 都是 0.0。

Temp1 is not 0.0. Temp1不是 0.0。


To set aside the minor additional error in the constant, use at least 9 digits with float and a f suffix .要排除常量中的小附加错误,请使用至少 9 位带有floatf后缀的数字。

//float CF1 = 0.555556;
float CF1 = 0.555555556f;

Suggested replacement建议更换

float Conv2Celsius(float P_FahrenheitTemp) {
  float ConvAdj = 32.0f;
  float CF = 0.555555556f;

  //[°C] = ([°F] - 32) × 5/9
  return (P_FahrenheitTemp - ConvAdj) * CF; 
}              

In C language the line float CF2 = 5/9;在 C 语言中行float CF2 = 5/9; will be processed as follows:将按如下方式处理:

  1. The right side of the assignment operator 5/9 is evaluated first.首先评估赋值运算符5/9的右侧。 The compiler here sees two integer values divided on each other so it will save the result into an integer temporary variable.这里的编译器看到两个整数值相互分开,因此它将结果保存到一个整数临时变量中。 This will lead to truncation of the fractional part of the actual result 0.555556 to 0 .这将导致实际结果0.555556的小数部分被截断为0
  2. The result will be assigned then to CF2.然后将结果分配给 CF2。

What to do?该怎么办?

Alot of options;很多选择; float CF2 = 5.0/9; or float CF2 = (float)5/9;float CF2 = (float)5/9; or even float CF2 = 5./9;甚至float CF2 = 5./9;

the same with CF3CF3相同

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

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