View Full Version : Float to fraction approximation in AviSynth
thewebchat
29th September 2009, 00:57
My experience in mathematics is quite limited, but I was wondering if there is an "easy" way to perform a float/fraction approximation in AviSynth. From a quick Google search, I found several code examples in C, but they involve using loops which AviSynth does not support.
Gavino
29th September 2009, 01:16
using loops which AviSynth does not support.
You can use recursion, or my GScript plugin.
EDIT: Actually, this might work using Avisynth: use AssumeFPS with your float (or maybe a multiple of the float if it's less than 1) and then look at FrameRateNumerator() and FrameRateDenominator(). :cool:
thewebchat
29th September 2009, 02:09
Haha, I hadn't thought of that. Thanks, Gavino. Also, AssumeFPS seems to accept values less than 1 just fine. Now, here's another thought that I came up with. Since floating point operations introduce rounding error, is there a way to clamp the precision of a float in AVS before I pass it to AssumeFPS?
Sagekilla
29th September 2009, 04:07
@thewebchat: That's the easy part ;)
myFloat = 0.123456789
myFloat = int(myFloat * 100) / 100.0
The number you multiply and divide by is simply 10^num_digits.
thewebchat
29th September 2009, 05:12
Ah, I see. That is indeed quite easy.
IanB
29th September 2009, 11:12
In 2.6.0alpha2 I have exposed the continued fraction routines as used in the FPS code mentioned above through 2 new script functions.
ContinuedNumerator(Float V, Int "", Int "Limit")
ContinuedNumerator(Int N, Int D, Int "Limit")
ContinuedDenominator(Float V, Int "", Int "Limit")
ContinuedDenominator(Int N, Int D, Int "Limit")
The rational pair returned has the smallest possible denominator such that the absolute error is less than 1/Limit.
If Limit is not specified in the Float case the rational pair returned is to the limit of the single precision floating point value.
i.e. (float)((double)Num/(double)Den) == V
In the Int case if Limit is not specified it becomes a nop and just returns the original values.
See Wikipedia for a full explanation continued fractions.
E.g.
ContinuedNumerator(PI(), Limit=5000]) == 355
ContinuedDenominator(PI(), Limit=5000) == 113
ContinuedNumerator(PI(), Limit=50]) == 22
ContinuedDenominator(PI(), Limit=50) == 7
ContinuedNumerator(355, 113, Limit=50]) == 22
ContinuedDenominator(355, 113, Limit=50) == 7
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.