# Why does Uniswap computes amountOut of a token this way?

It seems they do:

`(0.997 * amountIn * reserveOut) / (reserveIn + 0.997 * amountIn)`

From my understanding it should be `amountIn * price`, which with fee becomes `((1 - fee) * amountIn) * price`.

Now, since price is given by `reserveOut / reserveIn`, and since `fee = 0.003`, the above becomes `(0.997 * amountIn * reserveOut) / (reserveIn)`

We're missing the whole second addendum at the denominator!

Where is that coming from?

## References

: Directly from the contract:

``````function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
require(amountIn > 0, 'UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT');
require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
uint amountInWithFee = amountIn.mul(997);
uint numerator = amountInWithFee.mul(reserveOut);
amountOut = numerator / denominator;
}
``````

Well let's start with the price formula which says if `x` (`reserveIn`) and `y` (`reserveOut`) are our token supplies `x' * y' = x * y` must hold. Now let's set `f = (1 - fee) = 0.997`. Furthermore `d` is `amountIn` and `z` will be `amountOut`.

So I'll go ahead and derive the final formula:

`(x + d*f) * (y - z) = x * y` (divide both sides by `(x + d*f)`)

`y - z = (x * y) / (x + d*f)` (bring `y` to the right)

`z = y - (x * y) / (x + d*f)` (factor out `y`)

`z = y * (1 - x / (x + d*f))` (extend `1` to create a single fraction)

`z = y * (x + d*f - x) / (x + d*f)` (`x - x` cancels out)

Which gives us the final formula:

`z = y * d*f / (x + d*f)`

Hope this helps.