numbas / Numbas

A completely browser-based e-assessment/e-learning system, with an emphasis on mathematics
http://www.numbas.org.uk
Apache License 2.0
199 stars 118 forks source link

Add a constructive real number data type #893

Open christianp opened 2 years ago

christianp commented 2 years ago

I have ported Hans-J. Boehm's CReal library to JS: christianp/creal.js.

Constructive real numbers are exact: they produce decimal digits of the number through a kind of streaming algorithm. This could be useful for avoiding the kind of floating point rounding errors that regularly trip up question authors.

There are two ways they could be implemented:

Ideally, if we go with the second option, comparison options would use the constructive real form by default, so they are exact. It might be hard to tell which values have creal properties attached and which don't, though - it's already hard to make sure that operations on decimal values don't inadvertently get cast to number.

aarchiba commented 1 year ago

Er, isn't the problem of telling whether two constructive real numbers are equal non-computable?

christianp commented 1 year ago

The problem of deciding whether two algebraic expressions are equal is non-computable, and yet we have a part type for that.

The real problem is that comparing two constructive reals could take arbitrarily long, even if you know that the process will eventually terminate. In practical use, we'd set a limit for how long the computation can go on, and throw an error when it's reached.

For the vast majority of cases (as measured by how often they're seen, not by density in the reals), where a result can be obtained quickly, this will be a convenient way of ensuring a calculation is exact up to the point it needs to be converted to a decimal.

christianp commented 1 year ago

Could we just replace the decimal type with constructive real values?