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.
Related
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 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.
This question already has answers here:
Strange behaviour of the pow function
(5 answers)
Closed 5 years ago.
int a=pow(100,2);//line 1
int b=ceil(pow(100,2));//line 2
cout<<pow(100,2);//line 3
line 1 gives a=9999 on printing the value of a
line 2 gives b=10000 on printing value of b
line 3 prints 10000
I understood that pow give value 9999.9999 so ceil func. in line 2 gives it the upper value.
But why didn't cout print 9999.9999
Can anyone explain why pow behave like this return decimal value pow is just a power function why doesn't it simply give 100*100 as answer?
In the cout statement, pow(100, 2) is indeed a double-precision value and slightly below 10000 (by a well-known effect of the pow function), but the default accuracy setting of cout causes rounding and output as an integer.
The reason is due to pow taking two double values as arguments (and returning a double) and is typically implemented such that pow(x, y) is exp(y log x). This "goes off" for even seemingly trivial input values. See Is floating point math broken?
Note that std::pow has overloads for integral types which can be helpful when working in integer arithmetic.
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:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
Why when I use multiplication
float a= 1.0500000f;
float b= a*100.0f;
why b is 104.99999 but not 105.0 ?
and when I
int f= (int)b;
f is 104
Floating point numbers are not infinitely accurate -- the wikipedia page on Float numbers is quite interesting to read.
On your second question: (int)b truncates anything that occurs after the comma. This means that, in your case, 104.99999 becomes 104. So when you create a rounding error, and then cast it to an integer, you are indeed running the risk of getting a lower number.