tags:

views:

374

answers:

4

Hi,

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;
+1  A: 

I can't tell from reading your code, but I wonder if you have issues because of an erroneous application of integer division somewhere.

If you're certain that 1/12 = 0.0833333333..... is the answer you should be getting, all the results you've reported are incorrect:

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

All three are wrong.

I would have expected to see something more along the lines of greatest common divisor once you expressed your value as a ratio of two integers.

duffymo
I see your point. I am just wondering how others are getting the result then, maybe some rounding somewhere ... I only want to be able to display the same value as the other software does so somehow I need to come up with a solution. I have given what I have of data, do you have any idea of how they come to the result "1/12"?
Roy M Klever
"Others"? Is this a homework assignment? They must not be coding it the same way you are. I haven't dug into your code to figure out what you're doing, but it's not straightforward.
duffymo
Nope, with others I mean applications able to display EXIF data and giving the value "1/12" as the result. Well I am trying to figure out how they do it... If I have to do some other calculation its fine for me but I need a little bit help to what I should look for.
Roy M Klever
Problem have been solved.
Roy M Klever
If it has been solved, please share the solution as your own answer, unless one of these is what solved it then accept that answer.
Robert Love
+3  A: 

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

Harriv
+4  A: 

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?

Tim Jarvis
There is truncation involved. There's no information about how many decimal places he chose to retain.
duffymo
A: 

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

Roy M Klever