Why the result of pow(10,2) 99 instead of 100? [duplicate]

pow(10, 2) yields a value slightly under 100 because it is a low-quality implementation of pow that does not return a good result.

pow is a difficult function to implement, and all commercial implementations produce many results that are inaccurate to some degree. However, good implementations make an effort to get certain cases exactly correct, including cases where the result is exactly an integer (or, for that matter, is rational).

In pow(base, exp), if exp is known to be 2 at compile time, base*base should be used instead—it is fast and accurate and avoids the pow problem. If exp is not known to be two at compile time, the code must be designed to tolerate inaccuracies in pow or other adjustments must be made. (Further advice could be given to questions that are more specific about the circumstances or requirements.)

Leave a Comment