Isolate variable when rounding

39 Views Asked by At

My goal is to isolate frame from this equation:

$$ ms = round(frame * {1\over fps} * 1000) $$ $$ ms \in \mathbb{N} $$ $$ frame \in \mathbb{N} $$ $$ fps \in \mathbb{R+} $$ Note: $ round(0.5) = 1 $

Example where $ fps = 24000/1001 $

$$ Frame_0 : [0, 42[ ms \text{ because } round(0 * {1001\over 24000} * 1000) = 0 $$ $$ Frame_1 : [42, 83[ ms \text{ because } round(1 * {1001\over 24000} * 1000) = 42 $$ $$ Frame_2 : [83, 125[ ms \text{ because } round(2 * {1001\over 24000} * 1000) = 83 $$ $$ Frame_3 : [125, 167[ ms \text{ because } round(3 * {1001\over 24000} * 1000) = 125 $$

So, here is how I isolate frame

$$ (ms - 0.5) * fps * {1\over 1000} \le frame < (ms + 0.5) * fps * {1\over 1000} $$

Example

$$ ms = 82 $$ $$ fps = 24000/1001 $$ $$ (ms - 0.5) * {24000 \over 1001} * {1\over 1000} \le frame < (ms + 0.5) * {24000 \over 1001} * {1\over 1000} $$ $$ 1.95404 \le frame < 1.97802 $$ But, in reality, when $ ms = 82 $ and $ fps = 24000/1001 $, $ frame = 1 $.

There is no integer between the 2 bounds which surprised me since $ frame \in \mathbb{N} $.

Algorith to isolate frame

So, I tried to make an algorithm. $$ upperBound = (ms + 0.5) * fps * {1\over 1000} $$ $$ truncFrame = floor(upperBound) $$

If the upper_bound equals to the trunc_frame, this means that we don't respect the inequation because it is "greater than", not "greater than or equals". So if it happens, this means we need to return the previous frame

if upperBound == truncFrame
    frame = truncFrame - 1
else
    frame = truncFrame

It is a valid algorithm to isolate frame? Or even, is there a simpler method.