Search code examples
c++aspect-ratio

Compare aspect ratios given width and height


Given a list of screen sizes, how do I detect which ones are in 4:3 16:9 aspect ratio? I can use width / height to get it but for 16:9 sizes I sometimes get 1.778 and sometimes I get 1.777778 due to rounding errors.


Solution

  • Check if 4 * height == 3 * width or 16 * height == 9 * width.

    Remember the definition of a rational number: It is an equivalence class of pairs of integers (m, n) sub­ject to the equivalence (m, n) ≡ (m', n') if and only if nm = n m'.