WeBWorK Problems

Answer with units

Answer with units

by Zak Zarychta -
Number of replies: 3
Hi there,
I have a question that has the answer to which is in units of Hz.
A correct numerical answer with units of Hz is graded as correct.

However, an equivalent answer with equivalent units of s^-1 is marked incorrect. The code is here

$ans_i = NumberWithUnits("$f_0","Hz");

Is there a way to have both of these units marked correct?

Thanks in advance for any help,
Zak
In reply to Zak Zarychta

Re: Answer with units

by Alex Jordan -
What's happening is that the definition of Hz that the units parser is using is "2pi rad/s" instead of "s^-1. If you've got the problem ready to accept "3 Hz", it should count "6pi rad s^-1" as correct too. Since it may be relevant, note that the units parser also defines a cycle as "2pi rad".

The parser treats "rad" as a fundamental unit on par with "m" or "s". That makes "s^-1" and any number of "rad/s" dimensionally incompatible, which will make the answer checker say something like "you are using the wrong kind of units" instead of just marking it wrong because the scale is off by 2pi. This is a tricky thing, because a "rad" is in some sense actually a unitless rescaling.

It's understandable why this definition of Hz is here, but I would prefer that it were just "s^-1". You can use Hz with things like square waves or periodic pulses, where radians seem to have no place. Maybe if we scan the OPL to see where Hz is actually used, it would inform whether any such change can be made without breaking things.

If you have the ability to edit the problem, maybe OneOf would work. As in, load parserOneOf.pl, and make the answer be OneOf(answer-with-Hz, answer-with-s^-1). See if that works out.
In reply to Alex Jordan

Re: Answer with units

by Zak Zarychta -
Thanks for your reply.

I've always understood frequency having the units of Hz or s^-1 and angular frequency (velocity) as having units of rad/s. Although, I appreciate that rad isn't really a dimensional unit but a rescaling. I don't think the literature is very clear about it either. I think I have seen somewhere angular frequency (velocity) expressed in s^-1,..... although never in Hz.

The workround you suggested works and the answer checker (now successful) looks like

$ans_i = OneOf(
NumberWithUnits("$f_0","Hz"),
NumberWithUnits("$f_0","s^-1")
);
ANS($ans_i->cmp(
tolType => 'relative',
tolerance => .01,
));

Thanks again,
Zak
In reply to Zak Zarychta

Re: Answer with units

by Zak Zarychta -
Most strange......
....having tested the problem with the current answer checker and getting the correct answer returned for both units Hz and s^-1.

The problem now does not report correct for either of the units citing that the variable is not declared in this context, even though when the correct answer checkbox is checked the correct answer is reported as Hz or s^-1. I am absolutely stumped.

Any suggestions/ideas welcome,
Zak