#include<iostream>
using namespace std;
int main(int argc, char** argv){
float result;
result=(340/101);
cout<<"Result="<<result;
}
in this code result is=3, i'm curious about why is that? what is the prevention of this cause and it can be seen a minor problem but it really deprecate my results in my data analysis program.
EDIT: thanks for the answers, i was trying the code below and it also gives me 3, so that's why i confused now i'm clear.
result=(float)(340/101);
Thanks all
Because the expression (340/101) is evaluated independent of its surrounding context. int/int always results in an int. It makes no difference if you store that int in a float variable later on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With