为什么此输出为 0%
Why this output is 0%?
该程序将计算比率。正确的输出应该是 4.50492%,但我的输出是 0%。我怀疑这是尺寸问题。但是,双精度的大小为 8 个字节,因此类型长 long 是。我的程序有什么问题?谢谢。
#include <iostream>
int main()
{
using namespace std;
//cout << "Enter the world's population: ";
long long world = 6898758899;
//cin >> world;
//cout << "Enter the population of US: ";
long long us = 310783781;
//cin >> us;
double ratio = us/world * 100;
char percentage = '%';
cout << "The population of the US is " << ratio << percentage << " of the world population." << endl;
return 0;
}
因为"us"和"world"类型很长。所以"我们/世界"很长。(多头)0.0450492 == 0。
正确的拼写是
double ratio = (double)us/(double)world * 100.0;
相关文章:
- 为什么我的代码在输出中增加了93天
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 为什么rk4.do_step不输出C++中的初始条件
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 为什么我在输出端得到 nan?
- 为什么递归函数的最终输出是 5?
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 为什么输出精度没有正确舍入?
- 为什么这个程序的输出不如预期
- 有人可以向我解释为什么控制台输出 0 吗?
- 为什么文件不是由 F 流创建的,或者即使它是输出只是垃圾值?
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 为什么在这种情况下,bool 类型的输出等于 0?
- 执行此代码时,它不显示任何输出.为什么?
- 使用相同消息重新引发新异常会导致垃圾输出.为什么?
- 我的程序产生错误的输出.为什么
- 怪异的输出:为什么这段代码会给出任何有意义的输出,更不用说这个了
- %n格式说明符程序,在不同的编译器上提供不同的输出.为什么?
- 我的简单类函数中出现意外输出.为什么
- 如果语句不求值为false?程序给出奇怪的输出.为什么