English 中文(简体)
Calculating EXIF exposure time as a fraction (Delphi)
原标题:

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/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?

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

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.

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





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签