简体   繁体   English

C ++在数学计算中将double转换为int

[英]C++ converting double to int in mathematical calculations

When I write cout<<3.0/2.0; 当我写cout<<3.0/2.0; I get 1.5 but when I write cout<< 3.0*2.0; 我得到1.5,但当我写cout<< 3.0*2.0; I get 6. And if 3.0 and 2.0 are double values, shouldn't my result be a double value like 6.0? 我得到6.如果3.0和2.0是双值,我的结果不应该像6.0这样的双倍值吗? According to what is the result int or double? 根据int或double的结果是什么?

The result of 3.0 * 2.0 is very much a double (a) . 3.0 * 2.0的结果非常 (a) However, the presentation of a value is not the value. 然而,价值的呈现是不是值。 You'll find that 6 , 6.0 , 6.00000 , 0.06E2 , 6000E-3 and even the symbolic -6( e pi × i ) are all the same value, with different presentations. 你会发现, 66.06.000000.06E26000E-3甚至是象征性的-6( e pi × i ) 都是相同价值,用不同的表现形式。

If you don't want the default presentation (b) , the iostream and iomanip headers have facilities to format numbers in specific formats, such as using this to get 6.0 : 如果您不想使用默认演示文稿(b)iostreamiomanip标头可以设置格式化特定格式的数字,例如使用它来获得6.0

#include <iostream>
#include <iomanip>
int main() {
    auto x = 3.0 * 2.0;
    std::cout << std::setprecision(1) << std::fixed << x << '\n';
}

(a) The usual arithmetic conversions specified in the standard come into play here (eg, C++17 8 Expressions /11 ): (a)标准中规定的通常算术转换在这里发挥作用(例如, C++17 8 Expressions /11 ):

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. 许多期望算术或枚举类型的操作数的二元运算符会以类似的方式引起转换并产生结果类型。 The purpose is to yield a common type, which is also the type of the result. 目的是产生一个通用类型,它也是结果的类型。 This pattern is called the usual arithmetic conversions, which are defined as follows: 这种模式称为通常的算术转换,定义如下:

  • If either operand is of scoped enumeration type, no conversions are performed; 如果任一操作数是作用域枚举类型,则不执行任何转换; if the other operand does not have the same type, the expression is ill-formed. 如果另一个操作数的类型不同,则表达式格式不正确。
  • If either operand is of type long double, the other shall be converted to long double. 如果任一操作数的类型为long double,则另一个操作数应转换为long double。
  • Otherwise, if either operand is double, the other shall be converted to double. 否则,如果任一操作数为double,则另一个操作数应转换为double。
  • Other irrelevant stuff in the context of this question. 在这个问题的背景下其他无关紧要的东西。

(b) The actual rules for what gets printed by default are specified in the standard, but complex and locale-aware enough that it's probably easier for most people to just do explicit formatting :-) (b)默认情况下打印的内容的实际规则在标准中指定,但复杂且区域设置足够大,以至于大多数人可能更容易进行显式格式化:-)

The rules for what the output data type is from a given arithmetic operation are well defined in C++. 输出数据类型来自给定算术运算的规则在C ++中有明确定义。 You can find it out at compile time by using typeid . 您可以使用typeid在编译时找到它。

std::cout << typeid(3.0 * 2.0).name() << std::endl; // Prints 'd' for double instead of 'i' for integer

You might be surprised to find out that adding two values of one data type (char) can result in an output of a different data type: 您可能会惊讶地发现,添加一个数据类型(char)的两个值可能会导致输出不同的数据类型:

std::cout << typeid('a'+'b').name() << std::endl; // Prints 'i' for integer instead of 'c' for character

In this case, you are explicitly stating that 2.0 and 3.0 are floating point types. 在这种情况下,您明确声明2.0和3.0是浮点类型。 The result is, unsurprisingly, also a floating point type. 不出所料,结果也是浮点类型。 However, the cout utility is omitting the decimal point because the result is a whole number. 但是,cout实用程序省略了小数点,因为结果是整数。

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

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