WeBWorK Problems

min function not working on Reals

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();

loadMacros(
"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();


In reply to Alex Jordan

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?
In reply to Glenn Rice

Re: min function not working on Reals

by Alex Jordan -
Tightening the tolerance can get the desired behavior during the setup phase of the code, but then maybe that is too restrictive of a tolerance to impose upon students for their answers. So it inconveniently needs some tolerance setting and re-setting. (In the problem file that led me here, this tolerance is irrelevant to student answers, so I've just tightened it to something like 0.0000001.)

This is maybe just me, but I can be OK with having the comparison operators be fuzzy on MathObjects. There is something about the min() and max() functions where I'm not so comfortable. They rely on the fuzzy comparisons in a "sneaky" gotcha kind of way. Maybe the distinction is only my perception.