Another floating point issue

5 views
Skip to first unread message

billy...@gmail.com

unread,
Nov 21, 2025, 1:29:16 PMNov 21
to Numbas Users
Hi all,

I've just identified a similar problem to Martin Jones which has plagued my questions for weeks!

In the following question https://numbas.mathcentre.ac.uk/question/184984/testing-billy-s-copy-of-a4-q1-probability-table-fi/ , I give a table of probabilities for a random variable. There are 5 values: I generate 4 of them as a list p_aux of random decimals, and create the list p by appending 1 - sum(p_aux). I then ask the student to calculate p[2] + p[3] + p[4]. Unfortunately, since the sum of a list of decimals has type number rather than decimal, p[3] is decimal and p[4] has type number, so there is almost always a floating point error in here.

This is visible in the variables panel when I define a new variable answer and set it equal to p[2] + p[3] + p[4] (screenshot 1). However, if I don't - if I just type in p[2] + p[3] + p[4] directly as the expected answer of the gap - the floating point error is not visible in the display answer (screenshot 2) or in the marking algorithm testing panel (screenshot 3). This is really frustrating, especially when the issue is rare and only affects a tiny handful of students (making it even harder to debug!).

So a few questions / requests:
  • Why is the sum of a list of decimals not decimal? (Same with the sum of a list of rationals.)
  • Could there be a big prominent warning on the variables page when a variable relies on an implicit cast that loses information, so that we know to use precround or similar?
  • Could there be a nice easy button somewhere, like the one in the variables testing panel, that will automatically run a unit test 100 times on 100 randomised variables? This would save me having to do it by hand.
Thanks!
Screenshot_12.png
Screenshot_11.png
Screenshot_10.png

Christian Lawson-Perfect

unread,
Nov 25, 2025, 7:04:06 AMNov 25
to numbas...@googlegroups.com
The short answer is that sum always returns a number because it was written before the other number types existed, and I didn't think to update it.
Thanks for pointing this out. I've updated sum and prod to return integers, decimals or rationals when you give them a list of values of the same type. I've also updated gcd and lcm to return integers.

It'd be very hard to detect implicit type casts, with the way things are set up at the moment. 

For running unit tests, are you talking about part unit tests? Or just a fixed expression in terms of the variables? I thought I'd written that on the to-do list years ago, but I couldn't find it, so I've just added it: https://github.com/numbas/Numbas/issues/1163

--
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, visit https://groups.google.com/d/msgid/numbas-users/90d27051-8810-40d0-a320-f900b1c1723en%40googlegroups.com.

billy...@gmail.com

unread,
Nov 25, 2025, 8:52:21 AMNov 25
to Numbas Users
Thanks! I think I mean gap unit tests - there's a default one called "expected answer is marked correct". I'd like to be able to have that automatically run 100 times on 100 different randomly generated sets of variables, and tell me if the expected answer is ever marked incorrect. (Presumably this should never happen with the default marking algorithm anyway!)

Billy
Screenshot_4.png

Christian Lawson-Perfect

unread,
Nov 25, 2025, 9:16:25 AMNov 25
to numbas...@googlegroups.com
OK. I suppose the right place to put this button would be in the "Testing" tab, which runs unit tests for all parts in the question.

Reply all
Reply to author
Forward
0 new messages