PROBLEM: why it’s giving output 14 instead of 13.99999
CODE:
float log3(int n)
{
return log(n) / log(3);
}
bool isPowerOfThree(int num)
{
cout<<log3(num)<<" ";
}
PROBLEM: why it’s giving output 14 instead of 13.99999
CODE:
float log3(int n)
{
return log(n) / log(3);
}
bool isPowerOfThree(int num)
{
cout<<log3(num)<<" ";
}
for num input = 4782968
Hey @yogeshcsc20
Try this
float x=1.999999999999;
cout<<x;
This happens because :
Precision of floating point numbers is the accuracy upto which a floating point number can hold the values after decimal.
For example 10/6 = 1.6666666… these have recurring decimals which can take infinite memory spaces to be stored.
So to avoid memory overflow in such cases the compiler set a precision limit to the number. For float values in C++ this precision is set to 6-7 digit after that if the decimal recurs it will discard the value.
Is there any solution to get 13 instead of 14 as “floor(log3(4782968))” also gives me 14.
try using long double instead
You can also try trunc() function of c++
I used double and also long double but not getting the result. But When I used log10 instead of log it worked.