[system] / trunk / pg / lib / Parser / Function / numeric.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Parser/Function/numeric.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5001 - (download) (as text) (annotate)
Tue Jun 12 04:05:56 2007 UTC (5 years, 11 months ago) by dpvc
File size: 2201 byte(s)
More fixes for creating items in the corret context.  Also added a
method for looking up the package associated with a particular Parser
class (for consistency with the Value->Package call).

    1 #########################################################################
    2 #
    3 #  Implements other numeric functions
    4 #
    5 package Parser::Function::numeric;
    6 use strict;
    7 our @ISA = qw(Parser::Function);
    8 
    9 #
   10 #  Check that the argument is numeric (complex or real)
   11 #
   12 sub _check {(shift)->checkNumeric(@_)}
   13 
   14 #
   15 #  For complex arguments, call the complex version,
   16 #  Otherwise call the version below.
   17 #
   18 sub _eval {
   19   my $self = shift; my $name = $self->{name};
   20   return $_[0]->$name if Value::isComplex($_[0]);
   21   $self->$name(@_);
   22 }
   23 
   24 #
   25 #  Check the arguments and return the (real or complex) result.
   26 #
   27 sub _call {
   28   my $self = shift; my $name = shift;
   29   Value::Error("Function '%s' has too many inputs",$name) if scalar(@_) > 1;
   30   Value::Error("Function '%s' has too few inputs",$name) if scalar(@_) == 0;
   31   my $n = $_[0];
   32   return $self->$name($n) if Value::matchNumber($n);
   33   $self->Package("Complex")->promote($self->context,$n)->$name;
   34 }
   35 
   36 #
   37 #  Should make better errors about division by zero,
   38 #  roots of negatives, logs of negatives.
   39 #
   40 sub ln    {shift; CORE::log($_[0])}
   41 sub log10 {shift; CORE::log($_[0])/CORE::log(10)}
   42 sub exp   {shift; CORE::exp($_[0])}
   43 sub sqrt  {shift; CORE::sqrt($_[0])}
   44 sub abs   {shift; CORE::abs($_[0])}
   45 sub int   {shift; CORE::int($_[0])}
   46 sub sgn   {shift; $_[0] <=> 0}
   47 
   48 sub log   {
   49   my $self = shift; my $context = $self->context;
   50   return CORE::log($_[0])/CORE::log(10) if $context->flag('useBaseTenLog');
   51   CORE::log($_[0]);
   52 }
   53 
   54 #
   55 #  Handle absolute values as a special case
   56 #
   57 sub string {
   58   my $self = shift;
   59   return '|'.$self->{params}[0]->string.'|' if $self->{name} eq 'abs';
   60   return $self->SUPER::string(@_);
   61 }
   62 #
   63 #  Handle absolute values as special case.
   64 #
   65 sub TeX {
   66   my $self = shift; my $def = $self->{def};
   67   return '\left|'.$self->{params}[0]->TeX.'\right|' if $self->{name} eq 'abs';
   68   return $self->SUPER::TeX(@_);
   69 }
   70 #
   71 #  Handle log (and useBaseTenLog) as a special case
   72 #
   73 sub perl {
   74   my $self = shift; my $context = $self->context;
   75   return $self->SUPER::perl
   76     unless $self->{name} eq 'log' && $context->flag('useBaseTenLog');
   77   '(log('.$self->{params}[0]->perl.')/log(10))';
   78 }
   79 
   80 #########################################################################
   81 
   82 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9