WeBWorK Problems

Integers with more than 6 digits.

Integers with more than 6 digits.

by Cindy Loten -
Number of replies: 2

I am programming some combinatorics questions.  Students can enter their answers using P(m,n) and C(m,n) notation, but sometimes students like to enter numbers instead.  The integer answers can get quite large, and I want to avoid scientific notation when displaying numbers that arise. 

1) Is there a quick and easy way to display integers that are large (ie more than 6 digits) without using scientific notation? I used sprintf. While I have C(m,n) and P(m,n)  notation appearing in the ``Correct Answer'' column,  I thought it would be nice to show the actual computed number in the solution areas. 

2) If the student enters a computed number (rather than an expression using C(m,n) or P(m,n) ), is there a way to ensure the actual correct integer is entered other than setting the tolerance to 0.000001?

Code and embedded questions below:

########################################################################

DOCUMENT();      

loadMacros(
   "PGstandard.pl",     # Standard macros for PG language
   "MathObjects.pl",
    "PGML.pl",
   "contextIntegerFunctions.pl",   #  need for P(n,k) and C(n,k);
   #"PGcourse.pl",      # Customization file for the course
);

# Print problem number and point value (weight) for the problem
TEXT(beginproblem());

# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1;

##############################################################
#
#  Setup
#
#
Context("IntegerFunctions")->flags->set(
#    reduceConstants=>0,
    reduceConstantFunctions=>0
);
Context()->flags->set(tolerance => 0.000001);

$total =32 ;                 #random(26,32);
$selection = 25;               #random(9,12);


##############################################################
#
#  Text
#
#


BEGIN_PGML

How many unordered selections of size  [:[$selection] :] can be made from a set of [: [$total]:] objects?

    [_]{Formula("C($total, $selection)")}{30}
    

*Solution*  I'm putting the solution here rather than in pgml_solution area to avoid clicking and display my issues when presenting solutions.

[: [@ Formula("C($total, $selection)" ) @]  = [@ Formula("C($total, $selection)") -> reduce @]  :]

I want to avoid scientific notation when computing the actual number in the solution section.  Note: I have the C(m,n) notation appearing in the ``Correct Answer'' column, which is exactly what I wanted.  That being said, I needed to crank the tolerance up to  0.000001 to make sure 3365860 was not accepted as a correct answer.  Is there a better way to do this?

I tried to use sprintf to get the entire number for [: C([$total], [$selection]) :] with varying results.

Version 1: sprintf with with algebra notation [| [: [@ sprintf("%d",Real(" C($total, $selection) ")-> value ) @] :] |]
    [: [@ sprintf("%d",Real(" C($total, $selection) ")-> value ) @] :]

Version 2: sprintf with with latex notation
[|  [@ sprintf("%d",Real(" C($total, $selection) ")) @]  |]  
    [` [@ sprintf("%d",Real(" C($total, $selection) ")) @] `]  

Version 3: sprintf with with latex  and -> value  applied to the real number [|  [@ sprintf("%d",Real(" C($total, $selection) ") -> value ) @]  |]  
    [` [@ sprintf("%d",Real(" C($total, $selection) ") -> value ) @] `]

Version 3 is what I want.   Is there a shorter way to get Version 3 (ie the entire correct large number)?  



END_PGML

COMMENT('MathObject version. Uses PGML.');
ENDDOCUMENT();


In reply to Cindy Loten

Re: Integers with more than 6 digits.

by Danny Glin -
You can change the default format for how MathObjects displays numbers using
Context()->{format}{number} = "%d";
(see https://webwork.maa.org/wiki/Modifying_Contexts_(advanced))

The catch is that (as you discovered) algebra notation doesn't respect this.  It looks like any values in algebra mode are processed again, and it appears to be hard-coded to the "%g" printf format.  I've opened an issue about this (https://github.com/openwebwork/pg/issues/810).

If you set the format for the context, then [`[@ Formula("C($total, $selection)") -> reduce @]`] and [`[@ Real("C($total, $selection)") @]`] should both display all of the digits, though the latter is probably more computationally efficient as it is simply evaluating a real number, whereas the reduce method runs through the entire process of reducing formulas.  In fact, if you are using that value in more than one place you should probably assign it to a variable, and just call that variable rather than have to calculate the value multiple times.

Regarding the tolerance, in this type of question it would make sense to switch to absolute tolerance (https://webwork.maa.org/wiki/NumericalTolerance):
Context()->flags->set(tolerance => 0.1, tolType => "absolute"); 

In reply to Danny Glin

Re: Integers with more than 6 digits.

by Cindy Loten -
Thanks for the tips and links to further information! That solved all of my problems.