I wanted to know if we could improve the precision of log10 function
Also another thing is the double to long conversion.
For example I assigned double t = log 8 and after sometime I did t=pow(10,t).
When I display t it comes as 8.000000
but when I do long ans=t and display ans it displays as 7.
How to remove/rectify these precision problems ??
Conversion from double to long is only removing decimal part. So it seems impossible that:
double t = 8.0;
long ans = t;
cout << t << " " << ans;
will output 8 7. It correctly display 8 8. I believe that you should provide code so you can get more precise answer.
You may try something like this which might work.
From your question, what I understand is you want integral value of pow(10,t) where t is log of some value.
You may try something of this sort.
ans = (int) (pow((double)10.0,(double)t) + EPS);
EPS could be a very small value. I would recommend to take it as 1e-9.
This should solve your problem mostly as to what I understood from the question.
To add further,
“pow is a floating-point function: double pow(double x, double y). Like most of the other standard floating-point functions, the result of pow is only guaranteed to be approximately correct. pow(10.0, 2.0) can be exactly 100.0, slightly greater, or slightly less. If pow(10.0, 2.0) is slightly less than 100.0, casting it to the type int converts it to 99.”
This link may help you understand more about power function precision.
1 Like
Yes if you assign as in your code it occurs. But if you implement the following code the output is 8 7
double q=log10(8);
cout<<pow(10,1.0q)<<" "<<long(pow(10,1.0q))<<endl;
1 Like
Thanks for the reply. I am now able to understand the mistake.
You are right. That’s why I wanted to see code. It seems strange, but @lohit_97 found great solution.