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

Annotation of /trunk/pg/lib/Parser/Number.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2650 - (view) (download) (as text)

1 : sh002i 2558 #########################################################################
2 :     #
3 :     # Implements the Number class
4 :     #
5 :     package Parser::Number;
6 :     use strict; use vars qw(@ISA);
7 :     @ISA = qw(Parser::Item);
8 :    
9 :     sub new {
10 :     my $self = shift; my $class = ref($self) || $self;
11 :     my $equation = shift; my $num;
12 :     my ($value,$ref) = @_;
13 :     return Parser::Complex->new($equation,$value,$ref) if (ref($value) eq 'ARRAY');
14 : dpvc 2615 $value = $value->value while Value::isReal($value);
15 : dpvc 2650 $value = $value + 0; # format the value as a number, just in case
16 : sh002i 2558 $num = bless {
17 :     value => $value, type => $Value::Type{number}, isConstant => 1,
18 :     ref => $ref, equation => $equation,
19 :     }, $class;
20 : dpvc 2625 my $x = Value::Real->make($value);
21 :     $num->{isOne} = 1 if $x eq 1;
22 :     $num->{isZero} = 1 if $x == 0;
23 : sh002i 2558 return $num;
24 :     }
25 :    
26 :     #
27 :     # We know the answers to these, so no need to compute them
28 :     #
29 :     sub isComplex {0}
30 :     sub isNumber {1}
31 :     sub isRealNumber {1}
32 :    
33 :     #
34 :     # Return the value
35 :     #
36 :     sub eval {(shift)->{value}}
37 :    
38 :     #
39 :     # If the number is negative, factor it out and
40 :     # try using that in the reductions of the parent objects.
41 :     #
42 :     sub reduce {
43 :     my $self = shift;
44 :     if ($self->{value} < 0) {
45 :     $self->{value} = -($self->{value});
46 :     $self = Parser::UOP::Neg($self);
47 : dpvc 2625 $self->{op}{isOne} = 1 if Value::Real->make($self->{op}{value}) eq 1;
48 : sh002i 2558 }
49 :     return $self;
50 :     }
51 :    
52 :     #
53 : dpvc 2579 # Call the Value::Real versions to format numbers
54 : sh002i 2558 #
55 : dpvc 2612 sub string {
56 :     my $self = shift;
57 :     Value::Real->make($self->{value})->string($self->{equation},@_);
58 :     }
59 :     sub TeX {
60 :     my $self = shift;
61 :     Value::Real->make($self->{value})->TeX($self->{equation},@_);
62 :     }
63 : dpvc 2615 sub perl {shift->{value}}
64 : sh002i 2558
65 : dpvc 2650 ###########################################
66 :    
67 :     sub NoDecimals {$$Value::context->flags->set(NumberCheck=>\&_NoDecimals)}
68 :    
69 :     sub _NoDecimals {
70 :     my $self = shift;
71 :     $self->Error("You are not allowed to type decimal numbers in this problem")
72 :     unless $self->{value} =~ m/^[-+]?[0-9]+$/;
73 :     }
74 :    
75 :    
76 : sh002i 2558 #########################################################################
77 :    
78 :     1;
79 :    

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9