# DateTime irrationality

**URL:** <https://www.ruby-forum.com/t/datetime-irrationality/111258>\
**Category:** Ruby\
**Created:** [August 4, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/datetime-irrationality/111258 "2007-08-04T14:41:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tarazed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed8c4c/32.png) [@tarazed](https://www.ruby-forum.com/u/tarazed)\
**Post date:** [August 4, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/datetime-irrationality/111258/1 "2007-08-04T14:41:22Z")

</div>

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 L.

---

<div class="post-metadata">

**Author:** ![tarazed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed8c4c/32.png) [@tarazed](https://www.ruby-forum.com/u/tarazed)\
**Post date:** [August 4, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/datetime-irrationality/111258/2 "2007-08-04T15:58:27Z")

</div>

Len L. wrote:

> 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).

Just a matter of curiosity as an avid astronomer, what are you working  
on? 😉

I have a few astro related projects I’m working on. I started using  
Ruby but I fear it might be a little slow compared to others in some of  
the operations I’m performing (looking up star data from the TychoII  
catalog). What are your experiences? I considered using inline C but  
it’s a can of worms I haven’t dared open yet (based primarily on the  
fact I’m stuck on a Windows machine for development).

Anyways, I just thought it was cool to see someone else waxing astro  
with ruby 😉

Cliff

---

<div class="post-metadata">

**Author:** ![tarazed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed8c4c/32.png) [@tarazed](https://www.ruby-forum.com/u/tarazed)\
**Post date:** [August 4, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/datetime-irrationality/111258/3 "2007-08-04T17:36:38Z")

</div>

On Sat, 04 Aug 2007 22:57:22 +0900, Cliff R. wrote:

> — quoted text purged -----  
> Anyways, I just thought it was cool to see someone else waxing astro  
> with ruby 😉

Will reply directly Cliff as it is a bit off-topic.

Len

---

<div class="post-metadata">

**Author:** ![tarazed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed8c4c/32.png) [@tarazed](https://www.ruby-forum.com/u/tarazed)\
**Post date:** [August 5, 2007, 4:21am UTC](https://www.ruby-forum.com/t/datetime-irrationality/111258/4 "2007-08-05T04:21:19Z")

</div>

On Aug 4, 8:35 am, “Len L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Stime  
> sprintf( “%02d:%02d:%06.3f”, c[0],c[1],c[2] )
> 
> 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 L.

That’s weird.

Looks like divmod is somehow confused on some machines…

On my Mac (ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-  
darwin8.9.0]):

irb(main):045:0\> (14.9/24.0).divmod(1.0/24)  
=\> [13, 0.0375]

On my Linux box (ruby 1.8.5 (2006-12-04 patchlevel 2) [i386-linux]):

irb(main):002:0\> (14.9/24.0).divmod(1.0/24)  
=\> [14, 0.0375]  
irb(main):003:0\>

---

<div class="post-metadata">

**Author:** ![tarazed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed8c4c/32.png) [@tarazed](https://www.ruby-forum.com/u/tarazed)\
**Post date:** [August 5, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/datetime-irrationality/111258/5 "2007-08-05T23:05:45Z")

</div>

On Sun, 05 Aug 2007 02:18:57 +0000, georgebudd wrote:

That’s weird.

> Looks like divmod is somehow confused on some machines…
> 
> On my Linux box (ruby 1.8.5 (2006-12-04 patchlevel 2) [i386-linux]):
> 
> irb(main):002:0\> (14.9/24.0).divmod(1.0/24)  
> =\> [14, 0.0375]  
> irb(main):003:0\>

I am using ruby 1.8.5 (2006-08-25) [x86\_64-linux-gnu]

For now I shall use a workaround.

Thanks

---

<div class="post-metadata">

**Author:** ![tarazed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed8c4c/32.png) [@tarazed](https://www.ruby-forum.com/u/tarazed)\
**Post date:** [August 5, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/datetime-irrationality/111258/6 "2007-08-05T23:07:46Z")

</div>

On Sat, 04 Aug 2007 22:57:22 +0900, Cliff R. wrote:

> Anyways, I just thought it was cool to see someone else waxing astro  
> with ruby 😉

Sorry Cliff - my message to your gmail account bounced. This happens a  
lot. IP not authorized to access server directly - use SMTP relay of  
ISP.  
Haven’t a clue.
