[system] / trunk / pg / lib / Parser / BOP / divide.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Parser/BOP/divide.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5130 - (download) (as text) (annotate)
Mon Jul 9 12:45:06 2007 UTC (12 years, 7 months ago) by dpvc
File size: 2371 byte(s)
Added new flags to error checking of operands and function arguments.
This is so that a context can be developed that is more forgiving
about what can be put next to what.  Such a context can NOT be used
for evaluation or answer checking, but can be used to generate TeX
output in more sophisticated situations.

    1 #########################################################################
    2 #
    3 #  Implements division
    4 #
    5 package Parser::BOP::divide;
    6 use strict;
    7 our @ISA = qw(Parser::BOP);
    8 
    9 #
   10 #  Check that operand types are OK.
   11 #  Check for division by zero.
   12 #
   13 sub _check {
   14   my $self = shift;
   15   return if $self->checkStrings();
   16   return if $self->checkLists();
   17   $self->Error("Division by zero") if $self->{rop}{isZero};
   18   return if $self->checkNumbers();
   19   my ($ltype,$rtype) = $self->promotePoints();
   20   if ($ltype->{name} =~ m/Vector|Matrix/ && $rtype->{name} eq 'Number') {$self->{type} = {%{$ltype}}}
   21   elsif ($self->context->flag("allowBadOperands")) {$self->{type} = $Value::Type{number}}
   22   else {$self->Error("Division is allowed only for Numbers or a Vector, Point, or Matrix and a Number")}
   23 }
   24 
   25 #
   26 #  Do the division.
   27 #
   28 sub _eval {$_[1] / $_[2]}
   29 
   30 #
   31 #  Remove division by 1.
   32 #  Error for division by zero.
   33 #  Reduce zero divided by anything (non-zero) to zero.
   34 #  Factor out negatives.
   35 #
   36 sub _reduce {
   37   my $self = shift;
   38   my $reduce = $self->{equation}{context}{reduction};
   39   return $self->{lop} if $self->{rop}{isOne} && $reduce->{'x/1'};
   40   $self->Error("Division by zero"), return $self if $self->{rop}{isZero};
   41   return $self->{lop} if $self->{lop}{isZero} && $reduce->{'0/x'};
   42   return $self->makeNeg($self->{lop}{op},$self->{rop})
   43     if $self->{lop}->isNeg && $reduce->{'(-x)/y'};
   44   return $self->makeNeg($self->{lop},$self->{rop}{op})
   45     if $self->{rop}->isNeg && $reduce->{'x/(-y)'};
   46   return $self;
   47 }
   48 
   49 $Parser::reduce->{'x/1'} = 1;
   50 $Parser::reduce->{'0/x'} = 1;
   51 $Parser::reduce->{'(-x)/y'} = 1;
   52 $Parser::reduce->{'x/(-y)'} = 1;
   53 
   54 #
   55 #  Use \frac for TeX version.
   56 #
   57 sub TeX {
   58   my $self = shift;
   59   my ($precedence,$showparens,$position,$outerRight) = @_;
   60   my $TeX; my $bop = $self->{def};
   61   return $self->SUPER::TeX(@_) if $self->{def}{noFrac};
   62   $showparens = '' unless defined($showparens);
   63   my $addparens =
   64       defined($precedence) &&
   65       ($showparens eq 'all' || ($precedence > $bop->{precedence} && $showparens ne 'nofractions') ||
   66       ($precedence == $bop->{precedence} &&
   67         ($bop->{associativity} eq 'right' || $showparens eq 'same')));
   68 
   69   $TeX = '\frac{'.($self->{lop}->TeX).'}{'.($self->{rop}->TeX).'}';
   70 
   71   $TeX = '\left('.$TeX.'\right)' if ($addparens);
   72   return $TeX;
   73 }
   74 
   75 #########################################################################
   76 
   77 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9