WeBWorK Problems

min function not working on Reals

by Alex Jordan -
Number of replies: 2
I have an exercise where a quadratic function is plotted. The code for the problem identifies where the vertex and y-intercept will be, and uses these locations to help set the viewing window. In one random version of this problem, something is going very wrong. I whittled down the code to the MWE pasted below.

There is an array, @y. It contains two Real Math Objects, with one clearly smaller than the other. But min(@y) is returning the larger number. I guess that the min function is using a comparison operator for MathObject Reals, and my values are so close together, that they are equal by the fuzzy comparisons used by Math Objects.

So I can't trust min() and max() to work on Math Objects if the entries of the array are close. My question is, do we accept this? Or do we replace min() and max() so that when a Math Object number is an input, it automatically uses the ->value method to get a perl real?

DOCUMENT();

"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
);

@y = (
Real(2001),
Real(2000)
);
TEXT('The array @y is:',$BR,join($BR,@y),$PAR); TEXT('But its min is:',$BR,min(@y));

ENDDOCUMENT();

Re: min function not working on Reals

by Glenn Rice -
If you change the tolerance to one more decimal place of accuracy then min returns the correct thing. I.e., use Context()->flags->set(tolerance => 1E-4).

Of course add another digit to your numbers and you are back to the same problem, unless you add another decimal place to the tolerance.

I have always thought the default tolerances were not very good. In most of my problems I have to use different values. I often need set the tolType to absolute as well.

I am not sure about your question of accepting this, or use the value method to compare perl values. We are saying with the current tolerance that the two are equal with the fuzzy comparison, and so the one you get from the min function is just the one that occurs first in the list. Perhaps that is correct with the tolerance settings? If you need better then perhaps you need to change your tolerance?