[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 4996 - (download) (as text) (annotate)
Sun Jun 10 11:34:27 2007 UTC (12 years, 8 months ago) by dpvc
File size: 1962 byte(s)
Allow promote() to accept a context as the first argument so that the
promoted object can be forced into the given context.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9