[system] / trunk / webwork-modperl / doc / parser / extensions / 3-operator.pg Repository:
ViewVC logotype

Diff of /trunk/webwork-modperl/doc/parser/extensions/3-operator.pg

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2572 Revision 2573
32 32
33# 33#
34# Compute the value of n choose r. 34# Compute the value of n choose r.
35# 35#
36sub _eval { 36sub _eval {
37 shift; my ($n,$r) = @_; 37 shift; my ($n,$r) = @_; my $C = 1;
38 $r = $n-$r if ($r > $n - $r); # find the smaller of the two 38 $r = $n-$r if ($r > $n-$r); # find the smaller of the two
39 my $m = $n - $r; my $C = 1; 39 for (1..$r) {$C = $C*($n-$_+1)/$_}
40 #
41 # We compute this so that the value never gets too large
42 #
43 while ($n > $m) {$C = $C*$n/$r; $n--; $r-- if $r > 1}
44 while ($r > 1) {$C /= $r; $r--}
45 return $C 40 return $C
46} 41}
47 42
48# 43#
49# Non-standard TeX output 44# Non-standard TeX output
71$prec = Context()->operators->get('+')->{precedence} - .25; 66$prec = Context()->operators->get('+')->{precedence} - .25;
72 67
73Context()->operators->add( 68Context()->operators->add(
74 '#' => { 69 '#' => {
75 class => 'MyOperator', 70 class => 'MyOperator',
76 precedence => $prec, # just below addition 71 precedence => $prec, # just below addition
77 associativity => 'left', # computed left to right 72 associativity => 'left', # computed left to right
78 type => 'bin', # binary operator 73 type => 'bin', # binary operator
79 string => ' # ', # output string for it 74 string => ' # ', # output string for it
80 TeX => '\mathop{#}', # TeX version (overridden above, but just an example) 75 TeX => '\mathop{#}', # TeX version (overridden above, but just an example)
81 } 76 }
82); 77);
83 78
84 79
85# 80#
91# 86#
92# The problem text 87# The problem text
93# 88#
94BEGIN_TEXT 89BEGIN_TEXT
95 90
96In this problem, we have added a new operator to the Parser: ${BTT} n # r${ETT}, 91In this problem, we have added a new operator to the Parser: ${BTT}n # r${ETT},
97which returns \(n\choose r\). 92which returns \(n\choose r\).
98$PAR 93$PAR
99 94
100\{ParserTable( 95\{ParserTable(
101 'Formula("x # y")', 96 'Formula("x # y")',

Legend:
Removed from v.2572  
changed lines
  Added in v.2573

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9