[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 3716 - (download) (as text) (annotate)
Sun Oct 16 03:37:17 2005 UTC (14 years, 1 month ago) by dpvc
File size: 1999 byte(s)
In the past, when Value objects were inserted into strings, they would
automatically include parentheses so that if you had $f equal to 1+x
and $g equal to 1-x, then Formula("$f/$g") would mean (1+x)/(1-x)
rather than 1+(x/1)-x, which is what would happen as a straing string
substitution.

The problem is that this would also happen for real numbers, vectors,
and everything else, even when it wasn't necessary.  So if $x=Real(3),
then "Let x = $x" would be "Let x = (3)".

I have changed the behavior of the string concatenation for Value
objects so that parentheses are only added in a few cases: for
Formulas, Complex numbers, and Unions.  This makes the other Value
objects work more like regular variables in strings, but might cause
some problems with strings that are used as formulas.  For example, if
$a = Real(-3), then "x + 2 $a" will become "x + 2 -3", or "x-1" rather
than the expected "x - 6".  (The old approach would have made it "x +
2 (-3)" which would have worked properly).  For the most part, it is
easier to use something like "x + 2*$a" or even "x" + 2*$a in this
case, so the extra trouble of having to avoid parentheses when you
really meant to substitute the value into a string didn't seem worth
it.

    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        '.'   => sub {shift->_dot(@_)},
   12        '<=>' => sub {shift->compare(@_)},
   13        'cmp' => sub {shift->compare_string(@_)},
   14        'neg' => sub {shift->neg(@_)},
   15   'nomethod' => sub {shift->nomethod(@_)},
   16         '""' => sub {shift->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 {$Value::Type{infinity}}
   35 sub value {shift->{data}[0]}
   36 
   37 sub isZero {0}
   38 sub isOne {0}
   39 
   40 ##################################################
   41 
   42 #
   43 #  Return an infinity or real
   44 #
   45 sub promote {
   46   my $x = shift; $x = [$x,@_] if scalar(@_) > 0;
   47   $x = Value::makeValue($x);
   48   return $x if ref($x) eq $pkg || Value::isReal($x);
   49   Value::Error("Can't convert '%s' to Infinity",$x);
   50 }
   51 
   52 ############################################
   53 #
   54 #  Operations on Infinities
   55 #
   56 
   57 sub neg {
   58   my $self = shift;
   59   my $neg = Value::Infinity->new();
   60   $neg->{isNegative} = !$self->{isNegative};
   61   $neg->{data}[0] = '-'.$neg->{data}[0] if $neg->{isNegative};
   62   return $neg;
   63 }
   64 
   65 sub compare {
   66   my ($l,$r,$flag) = @_;
   67   if ($l->promotePrecedence($r)) {return $r->compare($l,!$flag)}
   68   $r = promote($r); my $sgn = ($flag ? -1: 1);
   69   return 0 if $r->class ne 'Real' && $l->{isNegative} == $r->{isNegative};
   70   return ($l->{isNegative}? -$sgn: $sgn);
   71 }
   72 
   73 ############################################
   74 #
   75 #  Generate the various output formats
   76 #
   77 
   78 sub TeX {(shift->{isNegative} ? '-\infty': '\infty ')}
   79 sub perl {(shift->{isNegative} ? '-(Infinity)': '(Infinity)')}
   80 
   81 ###########################################################################
   82 
   83 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9