[yunqa.de] Re: Rounding of REAL and DISQLite3Api.DateTimeToJulianDate

  • From: Delphi Inspiration <delphi@xxxxxxxx>
  • To: yunqa@xxxxxxxxxxxxx
  • Date: Wed, 25 Feb 2009 16:44:08 +0100

Edwin Yip wrote:

>But when the program gets the TDateTime values the precision is not only 4 
>digits, but I can only use ROUND(X, 4) to compare the values after saving into 
>the database in the way described in my original email...

The DateTimeToJulianDate function is implemented as a simple addition of two 
floats. I tend to believe that this should contribute very little only to 
precision loss. My testing shows that 12 digits of precision are well 
preserved. The small loss is certainly not due to the code but to the CPU's 
floating point unit. Not much can be done about this.

Various floating math libraries include dedicated functions to check for float 
equality. Here is one which allows to pass the precision as a 3rd parameter, 
i.e. 1e-6):

function FloatsEqual(const x, y, Delta: Extended): Boolean;
begin
  Result := x = y;
  if not Result then
    try
      if y = 0 then
        Result := Abs(1 - y / x) <= Delta
      else
        Result := Abs(1 - x / y) <= Delta;
    except
      Result := False; // catch real rare overflow e.g.  1.0e3000/1.0e-3000
    end
end;

If you need an 100% exact date and time representation with a guaranteed 
accuracy down to the very second, you must use a separate date format like ISO 
date (http://en.wikipedia.org/wiki/ISO_8601) or a Unix time 
(http://en.wikipedia.org/wiki/Unix_time). Both formats are supported by the 
DISQLite3 date and time functions.

Ralf  

_______________________________________________
Delphi Inspiration mailing list
yunqa@xxxxxxxxxxxxx
//www.freelists.org/list/yunqa



Other related posts: