Reputation: 113
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.
Upvotes: 1
Views: 3521
Reputation: 12048
You can force the rounding to be always the same, and then you can compare the values:
float ratio = (int)((width / height) * 100);
You will get always 177 for 16:9 and 133 for 4:3
good luck
Upvotes: 3
Reputation: 1174
you must use an epsilon value for the comparison. You can have a look at: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
Upvotes: 0
Reputation: 980
compare with some epsilon proximity.
should be something like:
double epsilon = 0.01;
if(math.abs(screen1.height/screen1.width - screen2.height/screen2.width) < epsilon)
{
//equal ratios
}
Upvotes: 0
Reputation: 477070
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) subject to the equivalence (m, n) ≡ (m', n') if and only if n' m = n m'.
Upvotes: 11