How to avoid cancelling of factor 1 with units?

How to avoid cancelling of factor 1 with units?

by Martin Kraska -
Number of replies: 6

A given value is defined as 

rr: rand_with_step(1,10,1)*mm;

In the question text I try to display this using

 \({@'r=rr@}\)

In the case of rr=1*mm, this is displayed as r = mm, which irritates the students, they think the number is missing.

Wrapping the assigment to rr in simp:false; simp:true; does not help,

What can I do except separating the output of the number and the unit?

 \({@'r=rr/mm@}\,\mathrm{mm}\)

I am not asking for  excluding 1 as a trivial solution ;)





Average of ratings: -
In reply to Martin Kraska

Re: How to avoid cancelling of factor 1 with units?

by Stephan Bach -
Hi Martin,
setting simp to false in the question variables and then using texput to display the unit in mathroman should do it.
But why are you not using stackunits?
Stephan
In reply to Stephan Bach

Re: How to avoid cancelling of factor 1 with units?

by Martin Kraska -
Stephan,

Thanks for the hints. Actually, setting simp to false in the whole question variables seems a bit invasive, because multiple side effects to subsequent definitions and operations in the question variables and in the question text could result.

Therefore i wrapped the definition of r in the question variables in this way:

stack_unit_si_declare(true);
simp:false; /* useless for display purpose if switched off again at the end of the question variables */
r1:1*mm;
r2:3*mm;
simp:true;
The problem is that display with {@@} is executed with simp:true. 

<p>{@'r=r1@}</p> 
<p>{@'r=r1/mm@} {@mm@}</p>
<p>{@'r=r2@}</p>
<p>{@(make_multsgn("blank"), 'r=r2)@}</p>
<p>{@'r=r2/mm@} {@mm@}</p>



Martin
In reply to Martin Kraska

Re: How to avoid cancelling of factor 1 with units?

by Christopher Sangwin -
Picture of Particularly helpful Moodlers Picture of Plugin developers

You said
> The problem is that display with {@@} is executed with simp:true.
Is the problem because you define simp:true; at the end of your question variables?! Castext re-evaluates the expressions within {@...@}, otherwise you couldn't do calculations there (which I'm sure you do elsewhere).

What about

stack_unit_si_declare(true);
simp:false; /* useless for display purpose if switched off again at the end of the question variables */
r1:1*mm;
r2:3*mm;
r3:stackunits(1,mm);
simp:true;

Then
{@'r=r3@}
This still has your simp:true; at the end in case you really do need this elsewhere (you might...)

In reply to Christopher Sangwin

Re: How to avoid cancelling of factor 1 with units?

by Martin Kraska -

Yes, leaving simp:false would add too much complexity to the problem for me.

Your example demonstrates the benefit of stackunits() for display purposes. The question is: How about the complexity of defining variables with stackunits()? Is that generally recommended over number*unit? Otherwise I would need to define separate sets of variables for display and math.

I'd not prefer this over occasional complications in display. 

In the given case I'd leave the definition as  r1=1*mm (in general that would result from some randomization) and then restrict use of stackunits() to the display itself just like

{@'r1=stackunits(r1/mm, mm)@} This gives the expected result and should be free of side effects. I like this variant.

{@'r1=stackunits(stack_unit_si_to_si_base(r1/cm), cm)@} This allows for local specification of display unit but hardly I'll be able to remember that until tomorrow.



In reply to Martin Kraska

Re: How to avoid cancelling of factor 1 with units?

by Christopher Sangwin -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Teachers seamlessly switch between thinking of 1mm as a single entity (a dimensional numerical quantity) and 1*mm (the product of a number with its units). Sometimes they don't notice a slightly larger switch to the equivalent 0.001m even! The CAS has to have one or the other. In some situations you will be better off with separate variables for numbers and units, combining them later with stackunits. In some situations you will be better off with single variables with stackunits. The functions
stack_units_units(ex);
stack_units_nums(ex);
try to split the expression into units and numbers, and the return the units and numbers found, so you can always get numbers and units separately later if you want to.

There are a variety of functions documented here https://docs.stack-assessment.org/en/Authoring/Units/

I'm always open to suggestions!
Chris
In reply to Christopher Sangwin

Re: How to avoid cancelling of factor 1 with units?

by Martin Kraska -
Thanks for the explanations.

BTW, I missed a hint on stack_unit_si_to_si_base(x) in the doc page you mentioned above.