Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there any specific reason why DateDifference cannot give an exact number in this case?

DateDifference["Jan 2, 2013", "Jan 8, 2013", "Week"]

0.857143

Also, I'd like to know how the various DayCountConvention settings interact with the calculation, in particular for "Week", "Month", etc.

share|improve this question
    
That will return 1, yes. But why my example will not give 6/7? DateDifference will give a machine number in general in this case, when an exact number can be easily derived. –  asterix314 Oct 29 '13 at 7:09

1 Answer 1

up vote 7 down vote accepted

The reason might be gleaned from examining the output from TracePrint.

DateDifference["Jan 2, 2013", "Jan 8, 2013", "Week"] // TracePrint (* Warning Huge Output *)

Somewhere close to the end of this humongous output we see the following:

DataPaclets`CalendarDataDump`n:Except[_Integer] :> N[DataPaclets`CalendarDataDump`n]
6/7 /. DataPaclets`CalendarDataDump`n:Except[_Integer] :> N[DataPaclets`CalendarDataDump`n]

So it appears that Mathematica computes the difference using AbsoluteTime which gives an exact number and afterwards (if the result is not an Integer) takes the numerical value. Hence the result you get. To get around this one can do:

Rationalize[DateDifference["Jan 2, 2013", "Jan 8, 2013", "Week"]]

Which gives:

{6/7, "Week"}

share|improve this answer
1  
This is missing the point. I want to know why, Mathematica being what it is, does not give me 6/7 in the first place. –  asterix314 Oct 29 '13 at 7:24
    
+1 for tracing into the function and locating the N. But why use N when the result is not an integer? –  asterix314 Oct 29 '13 at 13:46
    
@asterix314. That I cannot explain. There's no clue as to why in that huge TracePrint output either. –  RunnyKine Oct 29 '13 at 17:41
3  
@asterix314 I think as users we cannot really answer the "why" question in a satisfactory way. RunnyKine's response shows why the behavior arises, but if you don't like it or want someone to justify it to you, I think you'll have to raise it with support or file it as a bug. –  Oleksandr R. Oct 29 '13 at 19:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.