Floating point 浮动不';我不能正常工作

Floating point 浮动不';我不能正常工作,floating-point,Floating Point,我不知道当我执行这个程序时,为什么浮点没有打印出精确的值 #include <iostream> using namespace std; int main(){ int a, b; float x; cout << "Input the value for a: "; cin >> a; cout << "Input the value for b: "; cin >> b; x = - b / a; printf("The

我不知道当我执行这个程序时,为什么浮点没有打印出精确的值

#include <iostream>
using namespace std;

int main(){

int a, b;
float x;

cout << "Input the value for a: ";
cin >> a;
cout << "Input the value for b: ";
cin >> b;

x = - b / a;

printf("The value of x is: %.2f",x);
//cout << "The value of x is: " << x;
#包括
使用名称空间std;
int main(){
INTA,b;
浮动x;
cout>a;
cout>b;
x=-b/a;
printf(“x的值为:%.2f”,x);

//cout您需要将操作数强制转换为浮点数

    ex: x = (float)a / (float)b
请查看此处以了解更多信息:


首先,我认为您期望的输出是错误的

它应该是
-5.00
-10/2=-5。并且带有
%.2f
=>
-5.00

假设。如果
a
=10和
b
=2,则按照您的代码

 `x= - 2/10` 
这应该给

 x = 0. Notice the `\`.
由于您使用
%.2f
打印,因此系统会给出
0.00


我希望这能澄清你的疑问。

不会给出商。所以结果仍然是5,所以如果我的操作数是整数,我无法得到像1.5这样的浮点答案?是的,这就是结果,但我想看到的是像0.20这样的EXACT答案。打印EXACT值需要添加什么吗?你可以通过键入castin获得@King Arthur's答案漂浮