Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating EXIF exposure time as a fraction (Delphi)

Tags:

math

delphi

exif

I am trying to display EXIF exposure time as a fraction of seconds but I am not getting expected result, probably a small mistake.

I have found two routines that do the calculation but both bring diffrent and wrong result.

The value I am having problem is: "0.0806451612903226" value is of type Extended.

DecimalToFractStr give me: "16129/200000" as a result, DecToFrac gives me: "5/62" as a result

Expected result should be "1/12" since everyone is getting it (what is the trick). When I right click image in explorer and view details I get this value. How do I get it in my application? Hope someone can help me here.

Kind Regards Roy M Klever

SOLVED! Ok after some testing I figured it out. Using the result from DecToFrac they simply integer divide it with the result from the left side. 5 div 5 = 1 / 62 div 5 = 12 so I am left with wanted result 1 / 12.

Thank you for all your input.

Kind Regards Roy M Klever

procedure DecimalToFract(value: double; AllowedDecimals: integer; var num, den:
  integer);
var
  d, i: integer;
  ex: boolean;
begin
  d := Trunc(power(10, AllowedDecimals));
  num := Trunc(value * d);
  den := d;
  repeat
    ex := true;
    for i := 10 downto 2 do
      if ((num mod i) = 0) and ((den mod i) = 0) then
      begin
        num := num div i;
        den := den div i;
        ex := false;
      end;
  until ex;
end;

function DecimalToFractStr(value: double): string;
var
  num, den: integer;
begin
  DecimalToFract(value, 6, num, den);
  if den = 1 then
    result := inttostr(num)
  else
    result := inttostr(num) + '/' + inttostr(den);
end;

function Dec2Frac(f: Double): String;
var
  df: Double;
  lUpperPart: Integer;
  lLowerPart: Integer;
begin
  lUpperPart := 1;
  lLowerPart := 1;
  df := lUpperPart / lLowerPart;
  While (df <> f) do
  begin
    If (df < f) Then
      lUpperPart := lUpperPart + 1
    Else
    begin
      lLowerPart := lLowerPart + 1;
      lUpperPart := Trunc(f * lLowerPart);
    end;
    df := lUpperPart / lLowerPart;
  end;
  if lLowerPart = 1 then
    result := IntToStr(lUpperPart)
  else
    result := IntToStr(lUpperPart) + '/' + IntToStr(lLowerPart);
end;
like image 375
Roy M Klever Avatar asked Feb 27 '23 20:02

Roy M Klever


2 Answers

1/12 is not actually a correct result for that value.

Maybe they perhaps have some kind of "snap to" lookup table i.e. this value is closest to 1/12 from a list of incremented values?

like image 78
Tim Jarvis Avatar answered Mar 02 '23 10:03

Tim Jarvis


Maybe they have hard coded the common exposure times and they choose the closest one?

like image 34
Harriv Avatar answered Mar 02 '23 08:03

Harriv