## WeBWorK Main Forum

### more.... appropriate precision

by Zak Zarychta -
Number of replies: 5
A little thinking about the original problem

"I understand that a minimum for numerical precision may be set a minimal required precision . Is there anyway to also set a maximum numerical precision?

I require to write some questions regarding appropriate precision in arithmetical computations where, an answer is to be expressed to the same precision (d.p) as the term with the least amount of d.p in sums and differences. Similarly an answer is to be expressed to the same precision (s.f) as the the factor with the least amount of s.f in products and ratios."

Has lead me to a couple of ideas and further questions. I'd like to float them for opinions if i could?

Say that i want to round a floating point number to an arbitrary number of d.p. or s.f i'd simply have to pass the value to sprintf right?

Now if i was to pass the output of sprintf to a string variable and then use

ANS( $strAns->cmp() ); in comparing the answer, might this work ? Any opinions welcome, Zak In reply to Zak Zarychta ### Re: more.... appropriate precision by Gavin LaRose - Hi Zak, Would doing something like working the correct answer out to the correct precision and then setting the numerical tolerance to an absolute value of 0.49x10^(-a) (for an appropriate choice of a) also be a good solution? Gavin In reply to Gavin LaRose ### Re: more.... appropriate precision by Zak Zarychta - mmmmm, I'm not sure. consider this question express the following to the appropriate precision 0.324 + 0.5 ans = 0.8 to appropriate precision ie 1 d.p would not your solution allow 0.82, 0.824, 0.8240 etc? all of which in this question are incorrect. In reply to Zak Zarychta ### Re: more.... appropriate precision by Gavin LaRose - Hi Zak, The advantage of using a numerical answer may be that it preserves the error messages for that context. You're right that the suggestion above does not address your need. Something closer might be a tolerance of some small absolute value (e.g., 0.0001). Gavin In reply to Gavin LaRose ### Re: more.... appropriate precision by Zak Zarychta - your absolutely right! i'm a little reticent about going for strings in the answer, but in the requirements I have for the answer even 0.80, 0.800, etc is technically incorrect even though in a numerical context$WW would flag this as correct.

Interestingly enough the perl module Math::Bigfloat that deals with accuracy and precision does all its perlish magic by taking a number as an input string.

As i am no great exponent of perl perhaps i should take this as an omen from the perl gods that this is indeed the way to go? I reckon I could hack together a question using Math::BIgfloat that would satisfy my needs. Although I suppose ideally all of the perl would perhaps be better wrapped up in a $WW macro..... Who knows if I find the time.....? Incidentally what/where might be the conventions for writing$WW macros ?

Zak

### Re: more.... appropriate precision

by Michael Gage -
Zak,

Do you want an algorithm like this?

Do a numerical match of the students answer. Fail unless this matches.

Then take the students string e.g. 2.834 or 2.800 or 2.8 and
truncate the decimals 1 decimal digit (to 2.8 in all cases) and compare this with the original answer -- you get a fail unless they match.

I think you can do this with withPostFilter()