This question already has answers here:
Why is my double or int value is always 0 after division?
(5 answers)
Why does division result in zero instead of a decimal?
(5 answers)
Closed 2 years ago.
double res = (double)((a*b)/(a+b));
After trying the above code with inputs 80,70 I am getting the output 37 not 37.333 but after removing one parenthesis i got the right answer.
The right code is:
double res = (double)(a*b)/(a+b);
I am using:
gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)
double res = (double)((a*b)/(a+b));
Casting to double after integer math is applied, where values might have been truncated. Resort to operator precedence.
double res = (double)(a*b)/(a+b);
Here the cast is performed before the divison.
Related
This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
return value of pow() gets rounded down if assigned to an integer
(4 answers)
Is floating point math broken?
(31 answers)
Strange behaviour of the pow function
(5 answers)
Closed 6 months ago.
So in code i need to check wheather a no is perfect square or not ,
bool issquare(int x){
int root=(int)pow(x,0.5);
int y=pow(root,2);
if(y==x){
return true;
}
return false;
}
Now,
issquare(25);
is returning false. I tried debugging and this is weird,
root:5
x:25
y:24
why is y=24?.
This question already has answers here:
Dividing two integers to produce a float result [duplicate]
(1 answer)
What is the behavior of integer division?
(6 answers)
Closed 9 months ago.
I'm new at programming stuff and i'm practicing with variables, so i made a code in C++ where basically i do something like this:
float a;
a=4/3;
printf("%f", a);
return 0;
i expected to get the result as 1.333, but instead i got 1 as value
But if i do this:
float a, b=4, c=3;
a=b/c;
printf("%f", a);
return 0;
It gives back the right value (1.3333)
can somenone please explain that to me?
Ah, C++ being less than helpful. This is going to annoy you.
4 / 3
This is INTEGER math, not floating point. This would work if you did one of these:
4.0 / 3
4 / 3.0
4.0 / 3.0
C++ won't upscale your values to floating point while doing the calculation unless there are floating point values somewhere in the calculation itself.
This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 5 years ago.
I'm still a beginner in C++ so I seek some help with the basics.
Here, in the following code, I'm using type-casting to find value of 122/65 but I'm getting only the integer part even with double data type.
#include <iostream>
using namespace std;
int main()
{
double a=(double)('z'/'A');
cout<<a;
return 0;
}
Can someone provide me a good reason for this??
Thank you.
You make an integer division and then you typecast the result to double. Basically you have:
(double) (122/65) = (double) (1) = 1.0
^ truncated -> integer division
If you want a floating point division you can do it this way:
double a = (double)'z' / (double)'A';
// a = 122.0 / 65.0
This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 8 years ago.
when i try to calculate any expression, I always get an integer result, it's like:
float k= 5/12;
std::cout<< k<<std::endl;
the output in the console is always 0.
In C/C++, this is an integer division:
5/12
What you want is a floating point division:
5.0/12.0
Please note that this has absolutely nothing to do with GLUT or OpenGL.
This question already has answers here:
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 8 years ago.
Using some version of minGW, the following code will print 99.
int high;
high = pow(10,2);
std::cout<<high<<std::endl;
The parameter of pow function is double, but why i get 99? Someone who can tell me the process hidden inside pow function ?
Converting a double to an integer truncates the fractional part. pow(10,2) produces a slightly inaccurate result; if it's slightly high, you'll get 100 and if it is slightly low you'll get 99.
Moral: if you mean i*i, write i*i.