Question:
Why does Uniswap use Q notation versus the uint256 ratio price? A price in uniswap is always in token1/token0. Which token is which depends on the hex value assigned by the contract address when it's created.
(token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
So for example the WETH-USDC pool on ethereum the addresses are
> (WETH) 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 > (USDC) 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
true
So WETH is token 1.
The best documentation I've found on the topic is in this article https://blog.uniswap.org/uniswap-v3-math-primer which states
Q notation specifies the parameters of the binary fixed point number format, which allows variables to remain integers, but function similarly to floating point numbers. Variables that must be as precise as possible in Uniswap v3 are represented with a maximum of 256 bits and account both for overflow and potential rounding issues. By using Q notation, the protocol can ensure that granular decimal precision is not lost.
My thoughts
The only upside I see, assuming that ratios have to be calculated in token1/token0 at some point and don't risk overflows as a result, is that when you store this variable which is a uinit160 (sqrtPriceX96) in a struct it takes less space and thus uses less gas than the full uint256.
And if you're not using the struct you're not gaining much of anything by using a smaller value, in fact you may even be spending more gas as seen from this stack post
"The EVM works with 256bit/32byte words (debatable design decision). Every operation is based on these base units. If your data is smaller, further operations are needed to downscale from 256 bits to 8 bits, hence why you see increased costs."
Why does uint8 cost more gas than uint256?
If someone can confirm that's the case it would be very helpful.