#!/usr/bin/perl -l 

# deriv($func, $x, $delta) approximates the derivative of $func (a
# code reference) at $x. If provided, $delta is used as dx; otherwise,
# it begins at $delta = 1e-31 and increments by an order of magnitude
# until a just noticeable difference is reached.
#
# If the function $func is discontinuous, all bets are off.
#
sub deriv {
    my ($func, $x, $delta) = @_;

    # Choose a delta if one wasn't provided.
    #
    unless ($delta) {
        $delta = 1e-31;
        while ($x == $x + $delta) { $delta *= 10 }
    }

    # Compute and return an approximation to the derivative.
    #
    return ( &{$func}( $x + $delta ) - &{$func}( $x ) ) / $delta;
}

# 40% growth rate starting at 5000
$users = sub { 5000 * ( 1.4 ** $_[0] ) };

# One year away, to an accuracy of .01
#
$jan_2002 = deriv($users, 1,   0.1) / 366;
print $jan_2002;

# Two and a half years away, to an accuracy of .01
#
$jul_2003 = deriv($users, 2.5, 0.1) / 365;
print $jul_2003;
