Parent Directory
|
Revision Log
Revision 5001 - (view) (download) (as text)
| 1 : | sh002i | 2558 | ######################################################################### |
| 2 : | # | ||
| 3 : | # Implements other numeric functions | ||
| 4 : | # | ||
| 5 : | package Parser::Function::numeric; | ||
| 6 : | dpvc | 4975 | use strict; |
| 7 : | our @ISA = qw(Parser::Function); | ||
| 8 : | sh002i | 2558 | |
| 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 : | dpvc | 5001 | my $self = shift; my $name = shift; |
| 29 : | dpvc | 3370 | 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 : | sh002i | 2558 | my $n = $_[0]; |
| 32 : | return $self->$name($n) if Value::matchNumber($n); | ||
| 33 : | dpvc | 5001 | $self->Package("Complex")->promote($self->context,$n)->$name; |
| 34 : | sh002i | 2558 | } |
| 35 : | |||
| 36 : | # | ||
| 37 : | # Should make better errors about division by zero, | ||
| 38 : | dpvc | 2914 | # roots of negatives, logs of negatives. |
| 39 : | sh002i | 2558 | # |
| 40 : | dpvc | 2914 | sub ln {shift; CORE::log($_[0])} |
| 41 : | sh002i | 2558 | 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 : | dpvc | 2914 | sub log { |
| 49 : | dpvc | 4994 | my $self = shift; my $context = $self->context; |
| 50 : | jj | 3384 | return CORE::log($_[0])/CORE::log(10) if $context->flag('useBaseTenLog'); |
| 51 : | dpvc | 2914 | CORE::log($_[0]); |
| 52 : | } | ||
| 53 : | |||
| 54 : | sh002i | 2558 | # |
| 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 : | dpvc | 4994 | # |
| 65 : | sh002i | 2558 | 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 : | dpvc | 3568 | # |
| 71 : | # Handle log (and useBaseTenLog) as a special case | ||
| 72 : | # | ||
| 73 : | sub perl { | ||
| 74 : | dpvc | 4994 | my $self = shift; my $context = $self->context; |
| 75 : | dpvc | 3568 | return $self->SUPER::perl |
| 76 : | unless $self->{name} eq 'log' && $context->flag('useBaseTenLog'); | ||
| 77 : | '(log('.$self->{params}[0]->perl.')/log(10))'; | ||
| 78 : | } | ||
| 79 : | sh002i | 2558 | |
| 80 : | ######################################################################### | ||
| 81 : | |||
| 82 : | 1; |
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |