[system] / trunk / pg / lib / Value / Infinity.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Value/Infinity.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2625 - (download) (as text) (annotate)
Mon Aug 16 18:35:12 2004 UTC (15 years, 3 months ago) by dpvc
File size: 1927 byte(s)
Added string comparison to all Value object classes (to compare the
string value of an object to another string).

Overloaded perl '.' operator to do dot product when the operands are
formulas returning vectors.  (Part of the auto-generation of
formulas).

A few improvements to real and complex class output results.

Made Union class slightly more robust and removed need for makeUnion
method other than in the Union itself.

    1 ###########################################################################
    2 
    3 package Value::Infinity;
    4 my $pkg = 'Value::Infinity';
    5 
    6 use strict;
    7 use vars qw(@ISA);
    8 @ISA = qw(Value);
    9 
   10 use overload
   11        '.'   => \&Value::_dot,
   12        '<=>' => \&compare,
   13        'cmp' => \&Value::cmp,
   14        'neg' => \&neg,
   15   'nomethod' => \&Value::nomethod,
   16         '""' => \&Value::stringify;
   17 
   18 #
   19 #  Create an infinity object
   20 #
   21 sub new {
   22   my $self = shift; my $class = ref($self) || $self;
   23   Value::Error("Infinity should have no parameters") if scalar(@_);
   24   bless {
   25     data => [$$Value::context->flag('infiniteWord')],
   26     isInfinite => 1, isNegative => 0,
   27   }, $class;
   28 }
   29 
   30 #
   31 #  Return the appropriate data.
   32 #
   33 sub length {0}
   34 sub typeRef {my $self = shift; Value::Type($self->class,$self->length)}
   35 sub value {shift->{data}[0]}
   36 
   37 ##################################################
   38 
   39 #
   40 #  Return an infinity or real
   41 #
   42 sub promote {
   43   my $x = shift; $x = [$x,@_] if scalar(@_) > 0;
   44   $x = Value::makeValue($x);
   45   return $x if ref($x) eq $pkg || Value::isReal($x);
   46   Value::Error("Can't convert '$x' to Infinity");
   47 }
   48 
   49 ############################################
   50 #
   51 #  Operations on Infinities
   52 #
   53 
   54 sub neg {
   55   my $self = shift;
   56   my $neg = Value::Infinity->new();
   57   $neg->{isNegative} = !$self->{isNegative};
   58   $neg->{data}[0] = '-'.$neg->{data}[0] if $neg->{isNegative};
   59   return $neg;
   60 }
   61 
   62 sub compare {
   63   my ($l,$r,$flag) = @_;
   64   if ($l->promotePrecedence($r)) {return $r->compare($l,!$flag)}
   65   $r = promote($r); my $sgn = ($flag ? -1: 1);
   66   return 0 if $r->class ne 'Real' && $l->{isNegative} == $r->{isNegative};
   67   return ($l->{isNegative}? -$sgn: $sgn);
   68 }
   69 
   70 ############################################
   71 #
   72 #  Generate the various output formats
   73 #
   74 
   75 sub TeX {(shift->{isNegative} ? '-\infty': '\infty ')}
   76 sub perl {(shift->{isNegative} ? '-(Infinity)': '(Infinity)')}
   77 
   78 ###########################################################################
   79 
   80 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9