views:

239

answers:

3

I'm writing a small webpage that will enable students to answer questions and get feedback on their answers.

Part of this detection checks for common errors to give them guidance. Specifically I want to check if their answer is a power of ten out from the actual answer.

If the answer was 3.93E-6, this condition should activate if they type 3.93E2, 3.93E-9, 3.93 etc.

The obvious way to me to test this is to do something like this:

var correct = 3.93E-6;
var entry = 3.93E-2; //really comes from an input box.

if (!(entry / correct)%10) {
    alert ("power of ten error");
}

However, this doesn't work as error/correct doesn't work for large/small numbers.

How can I fix this?

Live code at: http://bradshawenterprises.com/test.html

+1  A: 

If they're required to enter the answer with the "E" notation, why not just check if all the stuff before the "E" is the same in both the student's answer, and the correct answer.

Of course you might also want to give them an idea of how many decimal places they should keep, otherwise 1.2E5 and 1.21E7 wouldn't trigger the "power of ten error"

MatrixFrog
Part of the test is to get the decimal places correct - there is another condition that triggers that.
Rich Bradshaw
+2  A: 
var num = 12.4123;
var numString = num.toExponential()
// numString = "1.24123e+1"

This normalizes the number, but you have to parse it manually. (Like on how accurate the result has to be…)

Georg
+2  A: 

Here's one way to see if two numbers are off by approximately a power of ten:

var correct = 3.93E-6;
var entry = 3.93E-2;
var epsilon = .01;

var log10_ratio = Math.log(correct/entry)/Math.log(10);
if (Math.abs(Math.round(log10_ratio) - log10_ratio) < epsilon) {
    alert ("power of ten error");
}
Miles
didn't you mean `>=` epsilon?
Christoph
@Christoph: No, the goal is to to inform the student if their entry is about a power of 10 off from the correct answer. If it's not, it's not a "power of ten error"
Miles