moodle-qtype_formulas icon indicating copy to clipboard operation
moodle-qtype_formulas copied to clipboard

Unit conversion sometimes fails

Open FloMiLe opened this issue 6 months ago • 7 comments

I have noticed a strange behavior with unit conversions that I don't understand. In the MWE attached, the duration of an hour should be given in s (seconds), and the user gives his answer in ks (kilo seconds).

With the following lines in conversion_rules.php, the input answer 3.6 ks is correctly interpreted:

s: k;

With the following lines in conversion_rules.php, the input answer 3.6 ks is not correctly interpreted:

s: k;
1 min = 60 s;

With the following lines in conversion_rules.php, the input answer 3.6 ks is correctly interpreted:

s: k;
60 s = 1 min;
1 min = 60 s;

questions-Unit Conversion-20250623-2343.xml.zip

FloMiLe avatar Jun 23 '25 21:06 FloMiLe

Thanks for reporting this. That odd behaviour is explained here:

https://github.com/FormulasQuestion/moodle-qtype_formulas/pull/164#issuecomment-2698593955

Unfortunately, rewriting the entire unit system takes quite some time...

PhilippImhof avatar Jun 24 '25 03:06 PhilippImhof

Thanks for your reply and the link to an explanation.

So the behavior is explainable, albeit not necessarily intuitive for the user. But is there perhaps nonetheless a way to define time conversions for hours, minutes, seconds that would allow for using any of these units together with prefixes?

FloMiLe avatar Jun 24 '25 09:06 FloMiLe

You can use the following definition:

h = 60 min; min = 60 s; s = 0.001 ks

Doing like this makes sure that hours are tied to minutes which are tied to seconds which are tied to kilo-seconds. Think of it like a chain.

If the correct answer is 3600 s, the student can use 1 h, 60 min, 3600 s or 3.6 ks as they please.

PhilippImhof avatar Jun 24 '25 10:06 PhilippImhof

I see. Since you need to redefine ks in your example, I guess it is not possible to find some permutation of

h = 60 min; min = 60 s;

that doesn't affect the general allowance of prefixes as in

s:   P T G M k h da d c m u n p f;

(I wasn't really clear about that...)

FloMiLe avatar Jun 25 '25 07:06 FloMiLe

Bringing minutes into relation with seconds will move the seconds away from its "dimension" place and thus remove the link to the original SI prefixes. So there is no "real" way of doing this.

There is, however, a workaround that would probably give acceptable results in most situations. You can define

s = 0.01666666666666666 min; min = 0.01666666666666666 h; s:   P T G M k h da d c m u n p f;

In this case, 1 h, 60 min, 3600 s, 3.6 ks and 0.0036 Ms would be accepted. This comes with some limitations due to the decimal expansion of 1/60 being infinitely long.

Honestly, I doubt that any students would try to give that result in Megaseconds, though…

PhilippImhof avatar Jun 25 '25 08:06 PhilippImhof

Thanks. Well, I have some specialist students who fully exhaust the possibility of using any prefix with any SI unit. 😉

FloMiLe avatar Jun 25 '25 09:06 FloMiLe

Just for completeness' sake: I believe the following also works:

31557600 s = 525960 min = 8766 h = 365.25 d = 1 a = 1 y;
s: P T G M k h da d c m u n p f;

But I do look forward to your rewriting of the parser, which would make this more robust...

FloMiLe avatar Jun 26 '25 19:06 FloMiLe