[system] / trunk / pg / macros / contextIntegerFunctions.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/contextIntegerFunctions.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5843 - (download) (as text) (annotate)
Tue Jul 8 15:13:18 2008 UTC (11 years, 6 months ago) by gage
File size: 2586 byte(s)
forward port of bug fix in rel-2-4-5

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: pg/macros/contextIntegerFunctions.pl,v 1.11 2007/10/04 16:40:48 sh002i Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 =head1 NAME
   18 
   19 contextIntegerFunctions.pl - adds integer related functions C(n,r) and P(n,r).
   20 
   21 =head1 DESCRIPTION
   22 
   23 This is a Parser context that adds integer related functions C(n,r)
   24 and P(n,r).  They can be used by the problem author and also by
   25 students if the answer checking is done by Parser.  The latter is
   26 the main purpose of this file.
   27 
   28 B<Note:> by default, webwork problems do not permit students to use
   29 C(n,r) and P(n,r) functions.  Problems which do permit this
   30 should alert the student in their text.
   31 
   32 =head1 USAGE
   33 
   34   Context("IntegerFunctions")
   35 
   36   $b = random(2, 5); $a = $b+random(0, 5);
   37   $c = C($a, $b);
   38   ANS(Compute("P($a, $b)")->cmp);
   39 
   40 B<Note:> If the context is set to something else, such as Numeric, it
   41 can be set back with Context("IntegerFunctions").
   42 
   43 =cut
   44 
   45 loadMacros('MathObjects.pl');
   46 
   47 sub _contextIntegerFunctions_init {context::IntegerFunctions2::Init()}; # don't reload this file
   48 
   49 package context::IntegerFunctions2;
   50 our @ISA = qw(Parser::Function::numeric2); # checks for 2 numeric inputs
   51 
   52 sub C {
   53   shift; my ($n,$r) = @_; my $C = 1;
   54   return (0) if($r>$n);
   55   $r = $n-$r if ($r > $n-$r); # find the smaller of the two
   56   for (1..$r) {$C = ($C*($n-$_+1))/$_}
   57   return $C
   58 }
   59 
   60 sub P {
   61   shift; my ($n,$r) = @_; my $P = 1;
   62   return (0) if($r>$n);
   63   for (1..$r) {$P *= ($n-$_+1)}
   64   return $P
   65 }
   66 
   67 sub Init {
   68   my $context = $main::context{IntegerFunctions} = Parser::Context->getCopy("Numeric");
   69   $context->{name} = "IntegerFunctions";
   70 
   71   $context->functions->add(
   72     C => {class => 'context::IntegerFunctions2'},
   73     P => {class => 'context::IntegerFunctions2'},
   74   );
   75 
   76   main::Context("IntegerFunctions");
   77 }
   78 
   79 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9