Duration between two days

S

Sky Yin

------=_Part_37943_21369839.1137253947560
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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>, 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 =3D Date.new 2004, 1, 1
# said:
b =3D Date.new 2004, 1, 3
# said:
Rational(2, 1)

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

Thanks.

------=_Part_37943_21369839.1137253947560--
 
J

Joe Van Dyk

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>, return t= he
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:



Rational(2, 1)

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

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

Will

I'm relatively new to Ruby, but I would guess that this is so the
Datetime class (which extends the Date class) -() method can be used
interchangably w/ Date and Datetime objects; if you are subtracting
Date and Datetime objects, you could end up with a fraction of a day,
in which case Fixnum would not be appropriate for representing the
return value.

Maybe someone with more Ruby experience can give you a better answer or
correct me if I'm way off base.
 
D

David Vallner

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

Because the code calculates in rationals?

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

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

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

David Vallner
 
S

Sky Yin

------=_Part_41776_31497137.1137292190211
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

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)
=3D> Rational(2, 1)
irb(main):002:0> require "mathn"
=3D> true
irb(main):003:0> Rational(2, 1)
=3D> 2

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

David Vallner

------=_Part_41776_31497137.1137292190211--
 
B

Brian Buckley

------=_Part_5355_90054.1137305533576
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
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

------=_Part_5355_90054.1137305533576--
 
L

Logan Capaldo

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

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

Brian Buckley

------=_Part_6687_15724498.1137335959834
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
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 # =3D> Rational
require 'mathn'
(date2 - date1).class # =3D> Fixnum

------=_Part_6687_15724498.1137335959834--
 
D

David Vallner

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

Interesting that 'mathn' caused a change in return type. I suppose tha= t
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 =20
change
in return type. My guess if that Date - Date uses the ** operator =20
because
** is redefined in mathn.


Brian Buckley


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

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

David Vallner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top