From: Len Lawrence Date: 2007-08-04T21:40:00+09:00 Subject: DateTime irrationality I am starting to recode several home applications in Ruby/GTK2 after 16 years of Tcl/Tk and a foray into Ruby/Tk territory. The simplest case would be my sidereal time widget which includes an Stime class, severely pruned below to show the to_s method (which has been altered to investigate the faulty behaviour described later). ---------------------------------------------------------------- require 'date' class Stime def to_s ( siderealtime ) z = siderealtime / 24.0 # h = (24.0 * z).to_i c = Date.day_fraction_to_time( z ) c[3] *= 86400.0 c[2] += c[3] c[3] = nil # c[0] = h sprintf( "%02d:%02d:%06.3f", c[0],c[1],c[2] ) end end puts stime.to_s( 9.334 ) puts stime.to_s( 13.603 ) t = 14.603 puts sprintf( "whatever = %06.3f = %s\n", t, stime.to_s( t ) ) puts stime.to_s( 18.765 ) ---------------------------------------------------------------- Results: 09:20:02.400 13:36:10.800 whatever = 14.603 = 13:36:10.800 ??? 18:45:54.000 I have established by trying a few values within the range 13.999 to 15.000 that the error is confined to the range 14.000 to 14.999. The symptoms are the same on two 64-bit machines, one AMD Opteron (2) and an Intel Core Duo. It ain't rational. Anybody ever seen anything like this? If the two commented lines are uncommented the to_s method always returns the correct hour value in the string. Date#day_fraction_to_time calls clfloor (which library?) and includes an argument 1.to_r (to_r from rational.rb). Len Lawrence