## 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.)

## Why does gcc compiler output pow(10,2) as 99 not 100? [duplicate]

Because of integer truncation. `pow()`

returns a floating point value, and due to floating point arithmetic, it is probably ~ `99.999...`

; however, due to integer truncation, even `99.999...`

gets truncated down to 99.

## I got different results with pow(10,2) and pow(10,j), j=2;

What's going on is that you have a C implementation whose standard library has a very low quality implementation of `pow`

which is returning inexact results even when the exact result is representable in the type (`double`

). The call to `pow(10,2)`

seems to producing the value just below `100.0`

, which, when rounded to an integer, yields 99. The reason you don't see this when the arguments are constant is that the compiler took the liberty to optimize out the call alltogether and replace it with a constant 100 at compiletime.

If your intent is to do integer powers, don't use the `pow`

function. Write a proper integer power function, or when the exponent is known, just write out the multiplication directly.

## Loss of precision with pow function when surpassing 10^10 limit?

The main reason of problems is pow() function. It works with double, not int. Loss of accuracy is price for representing huge numbers.

There are 3 way's to solve problem:

- For small n you can make your own
`long long int pow(int x, int pow)`

function. But there is problem, that we can overflow even`long long int`

- Use long arithmetic functions, as @rustyx sayed. You can write your own with vector, or find and include library.
- There is Math solution specific for topic's task. It solves the big numbers problem.

You can write your formula like

((10^n) - 1) * (10^n) - (10^m - 1) * (10^m)) / 2 , (here m = n-1)

Then multiply numbers in numerator. Regroup them. Extract common multiples 10^(n-1). And then you can see, that answer have a structure:`X9...9Y0...0`

for big enought n, where letter X and Y are constants.

So, you can just print the answer "string" without calculating.

## Why is std::pow(int, int) way faster than for loop with integer values?

It completely depends on the quality of `std::pow`

implementation, and the optimization capability of your compiler

For example some standard libraries calculate `pow(x, y)`

as `exp(log(x)*y)`

even for integer exponents, therefor it may be inaccurate and slow, resulting in issues like these

- Why does pow(5,2) become 24?
- Why pow(10,5) = 9,999 in C++

But some better `pow`

implementations check if the exponent is integer to use a better algorithm. They also use exponentiating by squaring so they'll be magnitudes faster than your linear multiplication like your for loop. For example for b^{100000} they need only 17 loops instead of 100000

If a compiler is smart enough to recognize your loop as to calculate power, it may convert to `std::pow`

completely. But probably it's not your case so `std::pow`

is still faster. If you write your `pow(int, int)`

function that uses exponentiating by squaring then it may likely be faster than `std::pow`

### Related Topics

What Is This Weird Colon-Member (": ") Syntax in the Constructor

How to Remove "Noise" from Gcc/Clang Assembly Output

Definitive List of Common Reasons For Segmentation Faults

C++ Delete - It Deletes My Objects But I Can Still Access the Data

When to Use Virtual Destructors

Read File Line by Line Using Ifstream in C++

Templated Check For the Existence of a Class Member Function

How to Initialize Private Static Members in C++

Function Pointer to Member Function

Is It a Good Idea to Typedef Pointers

What Are Rvalues, Lvalues, Xvalues, Glvalues, and Prvalues

How to Tokenize a String in C++

Can Num++ Be Atomic For 'Int Num'

When Does Invoking a Member Function on a Null Instance Result in Undefined Behavior

What Is a "Span" and When Should I Use One

What Is a Reference Variable in C++

Using Arrays or Std::Vectors in C++, What's the Performance Gap