# Duration between two days

**URL:** <https://rubytalk.org/t/duration-between-two-days/24097>\
**Category:** ruby-talk\
**Created:** [14 January 2006 15:52 UTC](https://rubytalk.org/t/duration-between-two-days/24097 "2006-01-14T15:52:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sky\_Yin](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Sky\_Yin](https://rubytalk.org/u/Sky_Yin)\
**Post date:** [14 January 2006 15:52 UTC](https://rubytalk.org/t/duration-between-two-days/24097/1 "2006-01-14T15:52:41Z")

</div>

According to the Rdoc of Date class, operator '-(x)' is described as:

If x is a Date \<[http://www.ruby-doc.org/core/classes/Date.html&gt;](http://www.ruby-doc.org/core/classes/Date.html&gt;), return the  
number of days between the two dates; or, more precisely, how many days  
later the current date is than x.

However, a quick test in irb gives me a weird result:

> require 'date'

true

> a = Date.new 2004, 1, 1

#\<Date: 4906011/2,0,2299161\>

> b = Date.new 2004, 1, 3

#\<Date: 4906015/2,0,2299161\>

> b - a

Rational(2, 1)

Basic math tells me 2/1 = 2, so what's the point of returning Rational  
instead of Fixnum here?

Thanks.

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [14 January 2006 16:55 UTC](https://rubytalk.org/t/duration-between-two-days/24097/2 "2006-01-14T16:55:08Z")

</div>

It's something to do with Astronomical Julian Days and fractional days.

> **···**
>
> On 1/14/06, Sky Yin \<sky.yin@gmail.com\> wrote:
> 
> > According to the Rdoc of Date class, operator '-(x)' is described as:
> > 
> > If x is a Date \<[http://www.ruby-doc.org/core/classes/Date.html&gt;](http://www.ruby-doc.org/core/classes/Date.html&gt;), return the  
> > number of days between the two dates; or, more precisely, how many days  
> > later the current date is than x.
> > 
> > However, a quick test in irb gives me a weird result:
> > 
> > \>require 'date'  
> > true  
> > \>a = Date.new 2004, 1, 1  
> > #\<Date: 4906011/2,0,2299161\>  
> > \>b = Date.new 2004, 1, 3  
> > #\<Date: 4906015/2,0,2299161\>  
> > \>b - a  
> > Rational(2, 1)
> > 
> > Basic math tells me 2/1 = 2, so what's the point of returning Rational  
> > instead of Fixnum here?

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [14 January 2006 23:29 UTC](https://rubytalk.org/t/duration-between-two-days/24097/3 "2006-01-14T23:29:36Z")

</div>

Because the code calculates in rationals?

You might want to 'require "mathn"' on the beginning of the script if it bugs you, the module causes Ruby math to work more... err... mathemathically. E.g.:

irb(main):001:0\> Rational(2, 1)  
=\> Rational(2, 1)  
irb(main):002:0\> require "mathn"  
=\> true  
irb(main):003:0\> Rational(2, 1)  
=\> 2

As always after doing deeper magic, be on the lookout for bugs the module could introduce.

David Vallner

> **···**
>
> On Sat, 14 Jan 2006 16:52:41 +0100, Sky Yin \<sky.yin@gmail.com\> wrote:
> 
> > Basic math tells me 2/1 = 2, so what's the point of returning Rational  
> > instead of Fixnum here?

---

<div class="post-metadata">

**Author:** ![Sky\_Yin](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Sky\_Yin](https://rubytalk.org/u/Sky_Yin)\
**Post date:** [15 January 2006 02:30 UTC](https://rubytalk.org/t/duration-between-two-days/24097/4 "2006-01-15T02:30:03Z")

</div>

Thanks, Joe and David. Although I don't quite understand the science behind  
that, add a bit more ".to\_i" is enough for me.

> **···**
>
> On 1/14/06, David Vallner \<david@vallner.net\> wrote:
> 
> > On Sat, 14 Jan 2006 16:52:41 +0100, Sky Yin \<sky.yin@gmail.com\> wrote:
> > 
> > \>  
> > \> Basic math tells me 2/1 = 2, so what's the point of returning Rational  
> > \> instead of Fixnum here?  
> > \>
> > 
> > Because the code calculates in rationals?
> > 
> > You might want to 'require "mathn"' on the beginning of the script if it  
> > bugs you, the module causes Ruby math to work more... err...  
> > mathemathically. E.g.:
> > 
> > irb(main):001:0\> Rational(2, 1)  
> > =\> Rational(2, 1)  
> > irb(main):002:0\> require "mathn"  
> > =\> true  
> > irb(main):003:0\> Rational(2, 1)  
> > =\> 2
> > 
> > As always after doing deeper magic, be on the lookout for bugs the module  
> > could introduce.
> > 
> > David Vallner

---

<div class="post-metadata">

**Author:** ![Brian\_Buckley](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@Brian\_Buckley](https://rubytalk.org/u/Brian_Buckley)\
**Post date:** [15 January 2006 06:12 UTC](https://rubytalk.org/t/duration-between-two-days/24097/5 "2006-01-15T06:12:26Z")

</div>

> irb(main):002:0\> require "mathn"

Though getting a Rational back is not incorrect, clearly getting a Fixnum  
back would be one's initial expectation.

Interesting that 'mathn' caused a change in return type. I suppose that  
means the return type is not part of the date subtraction contract. I  
quickly eyeballed mathn.rb to try to see the specific reason for the change  
in return type. My guess if that Date - Date uses the \*\* operator because  
\*\* is redefined in mathn.

Brian Buckley

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [15 January 2006 06:23 UTC](https://rubytalk.org/t/duration-between-two-days/24097/6 "2006-01-15T06:23:58Z")

</div>

Requiring mathn did not change the return type. Requiring mathn changed how rationals display themselves (ie it changed Rational#inspect). (It also changed Integer#/ to return rationals instead of Integers, among other things I am sure)

> **···**
>
> On Jan 15, 2006, at 1:12 AM, Brian Buckley wrote:
> 
> > > irb(main):002:0\> require "mathn"
> > 
> > Though getting a Rational back is not incorrect, clearly getting a Fixnum  
> > back would be one's initial expectation.
> > 
> > Interesting that 'mathn' caused a change in return type. I suppose that  
> > means the return type is not part of the date subtraction contract. I  
> > quickly eyeballed mathn.rb to try to see the specific reason for the change  
> > in return type. My guess if that Date - Date uses the \*\* operator because  
> > \*\* is redefined in mathn.
> > 
> > Brian Buckley

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [15 January 2006 16:06 UTC](https://rubytalk.org/t/duration-between-two-days/24097/7 "2006-01-15T16:06:50Z")

</div>

Yes, the change of return type is intentional - from skimming the source code, aside from the newly defined features, it seems mathn tweaks some flags to make Rational and Complex always unify to Integers if possible, and aliases Integer#/ for division of Integers to return Rationals. The latter might NOT be what you might want if you're used to the (IMO illogical) C behaviour of using integral division instead. mathn doesn't redefine Rational#\*\*, it defines it in the first place.

The date substraction contract is not violated, because Integers should have the same contract for every operation they share with Rationals of the same value. If that's not the case, it could be considered a bug in the Ruby core API.

David Vallner

> **···**
>
> On Sun, 15 Jan 2006 07:12:26 +0100, Brian Buckley \<briankbuckley@gmail.com\> wrote:
> 
> > > irb(main):002:0\> require "mathn"
> > 
> > Though getting a Rational back is not incorrect, clearly getting a Fixnum  
> > back would be one's initial expectation.
> > 
> > Interesting that 'mathn' caused a change in return type. I suppose that  
> > means the return type is not part of the date subtraction contract. I  
> > quickly eyeballed mathn.rb to try to see the specific reason for the change  
> > in return type. My guess if that Date - Date uses the \*\* operator because  
> > \*\* is redefined in mathn.
> > 
> > Brian Buckley

---

<div class="post-metadata">

**Author:** ![Brian\_Buckley](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@Brian\_Buckley](https://rubytalk.org/u/Brian_Buckley)\
**Post date:** [15 January 2006 14:39 UTC](https://rubytalk.org/t/duration-between-two-days/24097/8 "2006-01-15T14:39:33Z")

</div>

> Requiring mathn did not change the return type. Requiring mathn  
> changed how rationals display themselves

I think the return type is changed, not just how Rationals are displayed.

(date2 - date1).class # =\> Rational  
require 'mathn'  
(date2 - date1).class # =\> Fixnum
