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

Display flag to render surds #709

Open christianp opened 4 years ago

christianp commented 4 years ago

It would be nice to automatically spot surds like 1/sqrt(2), sqrt(3)/2 and sqrt(3/2), particularly for trigonometry. A display flag showSurds would render appropriate numbers as surds.

To avoid being over-eager, maybe only numbers less than 10 or 100 in both the integer or square root part should count as surds.

Here's a function that spots surds, returning undefined otherwise:

function spotSurd(a) {
  var r = Numbas.math.rationalApproximation(a*a);
  if(r[0]>100 || r[1]>100) {
    // reject fractions with numerator or denominator bigger than 10.
    return;
  }
  var sn = Math.sqrt(r[0]);
  var sd = Math.sqrt(r[1]);
  if(Numbas.util.isInt(sn)) {
    if(Numbas.util.isInt(sd)) {
      // reject rational numbers
      return;
    }
    // it's a surd of the form a/sqrt(b)
    return `${sn}/sqrt(${r[1]})`;
  } else if(Numbas.util.isInt(sd)) {
    // it's a surd of the form sqrt(a)/b
    return `sqrt(${r[0]})/${sd}`;
  } else if(sn<10 && sd<10) {
    // it's a surd of the form sqrt(a/b), with both a and b < 10
    return `sqrt(${r[0]}/${r[1]})`;
  }
}
christianp commented 1 year ago

Assuming the number isn't rational, you can write it in any of the forms a/sqrt(b), sqrt(a)/b, or sqrt(a/b). So do we need three display flags? The most common convention at school level is to rationalise the denominator, but at uni, most mathematicians prefer 1/sqrt(2) to sqrt(2)/2.