尽管存储为双精度数,为什么除法 (3/5) 结果为零?

发布于 2024-11-09 05:58:41 字数 203 浏览 0 评论 0原文

我正在做其他事情,但所有结果都是零,所以我做了这个简约的例子,输出仍然是 0。

#include <iostream>

int main(int argc, char** argv)
{
  double f=3/5;
  std::cout << f;
  return 0;
}

我错过了什么?

I was working on something else, but everything came out as zero, so I made this minimalistic example, and the output is still 0.

#include <iostream>

int main(int argc, char** argv)
{
  double f=3/5;
  std::cout << f;
  return 0;
}

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时光礼记 2024-11-16 05:58:41

您忽略了 3 和 5 是整数这一事实,因此您得到的是整数除法。要使编译器执行浮点除法,请将其中之一设为实数:

 double f = 3.0 / 5;

You are missing the fact that 3 and 5 are integers, so you are getting integer division. To make the compiler perform floating point division, make one of them a real number:

 double f = 3.0 / 5;
懵少女 2024-11-16 05:58:41

它不需要是 .0,您也可以使用 3./53/5. > 或 3e+0 / 53 / 5e-00xCp-2 / 5 或...只需要一个涉及的指示器,以便编译器知道它应该以浮点方式执行除法 观点。

另一种可能性:double f=double(3)/5。打字量要大得多,但毫无疑问你在做什么。

或者简单地使用double f=.6,这也能达到目的......

It doesn't need to be .0, you can also do 3./5 or 3/5. or 3e+0 / 5 or 3 / 5e-0 or 0xCp-2 / 5 or... There only needs to be an indicator involved so that the compiler knows it's supposed to perform the division as floating point.

Another possibility: double f=double(3)/5. That's much more typing, but it leaves no doubt to what you are doing.

Or simply use double f=.6, that also does the trick...

已下线请稍等 2024-11-16 05:58:41

试试这个:

double f = 3.0/5.0;

这应该可以解决你的问题

try this:

double f = 3.0/5.0;

this should fix your problem

半夏半凉 2024-11-16 05:58:41

尝试在除数之一后面添加 .0。这会将它们转换为浮点文字。

Try putting a .0 after one of the divisors. This will convert them into floating point literals.

拒绝两难 2024-11-16 05:58:41

如果您使用 int 保存通用变量,并希望获得 double 的比率:

using namespace std;
int main()
{
   int x = 7;
   int y = 4;
   double ratio;
   ratio = static_cast<double>(x)/static_cast<double>(y);
   cout << "ratio =\t"<<ratio<< endl;
}

In case, you save your generic variables with int and would like to obtain the ratio as double:

using namespace std;
int main()
{
   int x = 7;
   int y = 4;
   double ratio;
   ratio = static_cast<double>(x)/static_cast<double>(y);
   cout << "ratio =\t"<<ratio<< endl;
}
只涨不跌 2024-11-16 05:58:41

您正在使用整数。您可以通过很多方法使常量加倍,例如 leftaroundabout 状态,但这并不是很好。它很难阅读并且令人困惑。如果您想要 3 和 5,请将它们设为 3.0 和 5.0。如果他们被迫阅读你的代码,每个人都会明白你的意思。他/她所说的大部分内容确实需要您了解 C/C++ 以及如何存储浮点数来得出正面或反面。

You are using integers. You can many things to make your constants double like leftaroundabout states, however that is not good clean good. It is hard to read and confusing. If you want 3 and 5 make them 3.0 and 5.0. Everyone will know what you mean if they are forced to read your code. Much of what he/she states really requires you to know C/C++ and how floats are storage to make heads or tails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文