X-Spam-Check-By: sourceware.org Date: Fri, 16 Feb 2007 11:27:51 +0000 From: Robin Walker Reply-To: Robin Walker To: cygwin AT cygwin DOT com cc: Andrew Makhorin Subject: Re: strange bug in gettimeofday function Message-ID: In-Reply-To: <106982500.20070216041231@gnu.org> References: <13811889795 DOT 20070215071733 AT gnu DOT org> <106982500 DOT 20070216041231 AT gnu DOT org> X-Mailer: Mulberry/4.0.7 (Win32) MIME-Version: 1.0 Content-Type: multipart/signed; micalg=sha1; protocol="application/pkcs7-signature"; boundary="==========7883D2DD509F37E8653F==========" X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com --==========7883D2DD509F37E8653F========== Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline --On 16 February 2007 04:12 +0300 Andrew Makhorin wrote: >>> double get_time(void) >>> { struct timeval tv; >>> gettimeofday(&tv, NULL); >>> return (double)tv.tv_sec + 1e-6 * (double)tv.tv_usec; >>> } >> >> I would be suspicious of floating-point rounding errors here for the >> original problem you described. Why don't you try a test case that just >> checks if one tv is ever less than a previous tv, without the >> conversions. > > But then I would like to know why comparison of two floating-point > numbers leads to different results: t0 is *exactly* the same as t1, > nevertheless the condition t0 > t1 is true (sometimes). That is the > question. The multiplier 1e-6 cannot be represented exactly in binary floating point.= =20 Therefore the internal representation of your tv_usec will always be=20 subject to rounding errors, maybe upwards or maybe downwards. Therefore,= =20 the result of this function will never be an accurate representation of the= =20 time as returned by struct timeval. The impact of the rounding error will= =20 depend on at what point the internal 80-bit value in the processor is=20 rounded to 64 bits for storage as a double. If you really must do this (and it is not advised) you might do better by= =20 multiplying tv_sec by 1e6 then adding tv_usec unscaled, so that the=20 floating point variable holds an integer number of microseconds. Also, if tv_sec is large, there might be significant loss of precision as= =20 tv_sec and tv_usec are shifted 6 decimal places (about 20 binary places)=20 relative to each other in the mantissa. Floating point representation should never be used for something for which= =20 you need an accurate value, or where you require to test for two things=20 being equal. You have a struct which conveys the time accurately: why not= =20 use that? It is trivial to write functions which compare two timevals for= =20 equality, or yield the difference between two timevals. Another possible factor to bear in mind has already been alluded to:=20 Windows clock slew. Because PC clocks do not keep accurate time, Windows= =20 calibrates the internal clock against an external source (in XP, this is by= =20 default time.microsoft.com). By comparison of the two clocks, Windows=20 calculates a slew by which it occasionally adjusts the PC clock to keep the= =20 time of day in step with the external source. I do not know whether this= =20 factor is relevant to the data returned by gettimeofday(). --=20 Robin Walker (Junior Bursar), Queens' College, Cambridge CB3 9ET, UK rdhw AT cam DOT ac DOT uk http://www.queens.cam.ac.uk/ Tel:+44 1223 335528 --==========7883D2DD509F37E8653F========== Content-Type: application/pkcs7-signature Content-Transfer-Encoding: base64 MIIPuwYJKoZIhvcNAQcCoIIPrDCCD6gCAQExCzAJBgUrDgMCGgUAMAsGCSqG SIb3DQEHAaCCDYAwggPnMIIDUKADAgECAhB/XB5Eipwh6JZtd6i9RCwoMA0G CSqGSIb3DQEBBAUAMIHUMR4wHAYDVQQKExVTdHVkZW50IExvYW5zIENvbXBh bnkxHzAdBgNVBAsTFlZlcmlTaWduIFRydXN0IE5ldHdvcmsxPDA6BgNVBAsT M1Rlcm1zIG9mIHVzZSBhdCBodHRwczovL3d3dy50cnVzdHdpc2UuY29tL3Jw YSAoYykwMzEwMC4GA1UECxMnQ2xhc3MgMiBPblNpdGUgSW5kaXZpZHVhbCBT dWJzY3JpYmVyIENBMSEwHwYDVQQDExhTdHVkZW50IExvYW5zIENvbXBhbnkg Q0EwHhcNMDYwODE3MDAwMDAwWhcNMDcwOTE1MjM1OTU5WjCB/jELMAkGA1UE BhMCR0IxEjAQBgNVBAcUCUNhbWJyaWRnZTEqMCgGA1UEChQhVGhlIFN0dWRl bnQgTG9hbnMgQ29tcG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