Two strange issues with variables - random() and currency()

22 views
Skip to first unread message

Don Shearman

unread,
May 29, 2023, 12:01:15 AM5/29/23
to numbas...@googlegroups.com
I'm currently experiencing strange behaviour with the above functions in a development question:
  • The currency() function appears to work correctly in the Variables section of the question, but when attempting to display the value with currency symbol on the question, the variable is not being interpreted correctly. This happens whether it's in a MathJax environment or not.. Using the currency() function directly in the question also doesn't display.
  • If the upper limit for a random() variable has a 4 in the first decimal place, the upper limit of the variable is never chosen. For example random(8.15..8.45 #0.1) only returns the values 8.15, 8.25 or 8.35. Changing the upper limit to 8.5 overcomes this. This issue doesn;t seem to occur if 4 is replaced with a different value.
I've built a simple question which illustrates both of these issues at https://numbas.mathcentre.ac.uk/question/142857/variable-testing/

Any insight into either issue would be appreciated.

Regards

Don Shearman
Western Sydney University

Ben Brawn

unread,
May 29, 2023, 2:17:41 AM5/29/23
to Numbas Users
Hi Don, 

currency returns a string, so either use  {money} or {latex(money)}. Does that seem correct to you?

However, I can't get the suffix to do anything, so that might be broken.

As for random and the interval, the end point is never included 
"x[a..b]
x[a..b#c]
Slice the collection x - return elements with indices in the given range. Note that list indices start at 0, and the final index is not included."

Don Shearman

unread,
May 29, 2023, 4:20:51 AM5/29/23
to numbas...@googlegroups.com
Hi Ben

Thanks for the thoughts, I haven’t tried latex(money) yet but the other versions were working last year. 

With regards the random issue, the end value does occur if other end points are chosen. You can see this by changing 8.45 to 8.55 in the example question I posted earlier. I’m guessing that this is some strange behaviour in the random algorithm that’s being used. 

Don

Sent from my iPad

On 29 May 2023, at 4:17 pm, Ben Brawn <ben....@gmail.com> wrote:

Hi Don, 
--
You received this message because you are subscribed to the Google Groups "Numbas Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numbas-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/numbas-users/2a36f9bf-d2e6-4e85-b119-79ae1caa1d9cn%40googlegroups.com.

Christian Lawson-Perfect

unread,
May 30, 2023, 4:33:09 AM5/30/23
to numbas...@googlegroups.com
These were both bugs. Thanks for reporting them.

The problem with the currency function was that it didn't mark its output as valid LaTeX code, so when it's included in a LaTeX math expression, it's rendered as e.g. \textrm{$4}.  The dollar sign is interpreted as opening a passage of math mode, and there's no matching dollar sign to close it. I've changed the function so that it marks its output as LaTeX, so it's inserted pretty much verbatim, with the dollar sign escaped.

The problem with the `random` function was floating-point precision, again. 8.45 - 8.15 can't be represented exactly, so it ends up as 0.2999999999999989 instead of 0.3. The code to pick randomly from a range uses this difference to work out how many possible values there are by rounding down to an integer. I've changed it to allow a tiny bit of error, so it now correctly generates 8.15, 8.25, 8.35 and 8.45.

Don Shearman

unread,
May 30, 2023, 5:00:15 AM5/30/23
to numbas...@googlegroups.com
Thanks Christian, appreciate your quick response as always. 

Don

Sent from my iPad

On 30 May 2023, at 6:33 pm, Christian Lawson-Perfect <christia...@gmail.com> wrote:


Reply all
Reply to author
Forward
0 new messages