Search code examples
mathexifwic

Rounding overly precise fractions to human-friendly precision


How can I round an excessively precise fraction to a less precise format that is more humanly readable?

I'm working with JPEG EXIF exposure time data extracted by MS' Windows Imaging Component. WIC returns exposure times in fractional form with separate ints for numerator and denominator.

WIC usually works as expected, but with some JPEGs, WIC returns exposure times in millionths of a second, meaning that instead of reporting e.g. a 1/135 second exposure time, it reports an exposure time of 7391/1000000 seconds. The difference between 1/135 and 7391/1000000 is quite small but the latter is not intuitive to most users. As such, I'd like to round overly precise exposure times to the nearest standard exposure times used in photography.

Is there a better way to do this other than using a lookup table of known-reasonable exposure times and finding the nearest match?


Solution

  • You can compute the continued fraction expansion of the large fraction. Then take one of the first convergents as your approximate fraction.

    In your case, you get

    7391/1000000 = [ 0; 135, 3, 2, ...]

    so the first convergent is 1/135=0.0074074..., the next

    1/(135+1/3) = 3/406 = 0.00738916256...
    

    and the third

    1/(135+1/(3+1/2)) = 1/(135+2/7) = 7/947 = 0.00739176346...
    

    To compute the (first) coefficients of a continuous fraction development, you start with xk=x0. Then iteratively apply the procedure

    • Separate xk=n+r into integer n and fractional part r.
    • The integer is the next coefficient ak, with the inverse of the fractional part you start this procedure anew, xk = 1/r

    Applied to the given number, this produces exactly the start of the sequence as above Then reconstruct the rational expressions, continue until the inverse of the square of the denominator is smaller than a given tolerance.