简体   繁体   English

在 C 中添加 2 个浮点数

[英]Adding 2 floats in C

If I have 2 floats and add them together, is the result a float?如果我有 2 个浮点数并将它们加在一起,结果是浮点数吗? Or is a double?或者是双? Or is it compiler defined behavior?还是编译器定义的行为?

9.0f + 8.0f <--- is the result float, double or compiler defined behavior? 9.0f + 8.0f <--- 结果是浮点型、双精度型还是编译器定义的行为?

A citation of the correct place in the C specifications would be helpful, if possible.如果可能的话,引用 C 规范中的正确位置会有所帮助。

9.0f + 8.0f 

is the result a float ?结果是float吗?

Yes, the type is float .是的,类型float

First, if the corresponding real type of either operand is long double , the other operand is converted, without change of type domain, to a type whose corresponding real type is long double .首先,如果任一操作数的对应实数类型为long double ,则另一个操作数将在不更改类型域的情况下转换为其对应实数类型为long double的类型。

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 .否则,如果任一操作数的对应实数类型为double ,则另一个操作数将在不更改类型域的情况下转换为其对应实数类型为double的类型。

Otherwise, if the corresponding real type of either operand is float , the other operand is converted, without change of type domain, to a type whose corresponding real type is float .否则,如果任一操作数的对应实数类型为float ,则另一个操作数将在不更改类型域的情况下转换为其对应实数类型为float的类型。

C23x dr § 6.3.1.8 1 C23x dr § 6.3.1.8 1


Although the type is float , depending on FLT_EVAL_METHOD , the sum may have been calculated using float , double or long double math.尽管类型float ,但取决于FLT_EVAL_METHOD ,总和可能是使用floatdoublelong double数学计算的。 This potentially has arithmetic impact on a larger/different valued expression.这可能会对更大/不同值的表达式产生算术影响。 This part is "compiler implementation defined behavior".这部分是“编译器实现定义的行为”。

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

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