Re: Re: DT::Duration overloads

2004-06-11 Thread Rick Measham
I said:
On the other hand, maybe these should be DateTime::Set methods:
   my $mean   = $set-mean( $sunrise, $sunset );
   my $median = $set-median( $sunrise, $sunset );

On Thu, 10 Jun 2004 18:04:36 -0700, Bruce Van Allen wrote
 Huh? I'm confused by your usage of 'mean' and 'median'. In the case in
 question, I think 'midpoint' is a much clearer term to use.

I suggest placing mean and median in a module rather than just the FAQ because the 
current solution is just the midpoint of two datetimes. The mean can give this, but 
can 
also give results for more than two datetimes. 

Consider that an FAQ entry for calculating the average time you start work from a list 
of 
DateTimes would be a lot longer so should we put it in the FAQ? I don't think so. I 
think 
it should be a library method placed either in a Util module or in the DateTime::Set 
module. Then, once we do that, we can change the FAQ entry for midpoint to Either 
use the 'mean' function in DateTime::XXX::XXX or use the following algorithm

As for my usage of 'mean' and 'median':

'mean' and 'median' are mathematical constructs. The 'mean' is often called the 
'average' while the 'median' would be the middle value.

mean(1,2,3) == 2
median(1,2,3) == 2

But here's the difference:
mean(1,2,6) == 3
median(1,2,6) == 2

And the 'median' value with an even number of arguments:
median(1,2,3,4) == 2.5
(middle is both 2 and 3, therefore use their mean (2 + 3) / 2 == 2.5)


And the maths involved:
sub mean{
   return sum(@_) / scalar(@_);
}

sub median{
   my $middle = (scalar(@_) + 1) / 2;
   return ($middle == int(middle)) 
  ? $_[  $middle ]
  : mean( $_[  int($middle) ], $_[  int($middle)+1 ]);
}

sub sum {
   my $return = 0;
   $return += $_ foreach @_;
   return $return;
}

Of course, the DateTime code would be more difficult. I'd probably cheat by getting 
rd_secs and using the above mathematical functions to get mean and median rd_secs 
and then turning them back into DateTime objects.

Hope that clears it up.

Cheers!
Rick




Re: DT::Duration overloads

2004-06-11 Thread Matt Sisk
Quoting Dave Rolsky [EMAIL PROTECTED]:
  With DateTime::Span:
 
   $mid_point =
  $span-start-add_duration(
  seconds = $span-duration-seconds / 2
  );
 
  With DateTime:
 
$mid_point =
  $start-add_duration(
  seconds = $end-subtract_datetime_absolute(
  $start
  )-seconds / 2
  );

 Oh, duh!  Why didn't I think of that.  That's nice  simple.

If the operator overloads did that sort of thing behind the scene (and yes, with
spans also), then it would be nicer and simpler. :)

But thanks, Flavio and Dave, for the recipes and dealing with my questions.

  How about a DateTime::Span-midpoint method?

 Let's wait and see if others ask for it.  For now, let's just add those
 recipes to the faq.

Midpoints are compelling, but also arbitrary. I'd like to see the general
problem of portioning a span, or the delta between two arbitrary datetimes
(expressed as a span or otherwise, transparently), solved in a general way.
After all, if midpoint is that common, sure, provide a method, but we don't
need methods for thirds, quarters, eighths, forty-thirds, etc.

Thanks again,
Matt (in the Big Easy for a wedding...man I should get some sleep)

P.S. Sorry for the wonky quoting in my earlier messages -- I plead inadequate
mail client that can't properly convert between markup and plain text.



Re: DT::Duration overloads

2004-06-11 Thread Dave Rolsky
On Fri, 11 Jun 2004, Matt Sisk wrote:

 Quoting Dave Rolsky [EMAIL PROTECTED]:
   With DateTime::Span:
  
$mid_point =
   $span-start-add_duration(
   seconds = $span-duration-seconds / 2
   );
  
   With DateTime:
  
 $mid_point =
   $start-add_duration(
   seconds = $end-subtract_datetime_absolute(
   $start
   )-seconds / 2
   );
 
  Oh, duh!  Why didn't I think of that.  That's nice  simple.

 If the operator overloads did that sort of thing behind the scene (and yes, with
 spans also), then it would be nicer and simpler. :)

Uh, exactly what operator would you overload for DateTime?  I certainly
don't see and obvious candidate.


-dave

/*===
House Absolute Consulting
www.houseabsolute.com
===*/