Skip to content
This repository was archived by the owner on May 11, 2021. It is now read-only.

Commit 5b40b89

Browse files
committed
Be more conservative about saying something is a multiple of pi.
Summary: I can't figure out how 'pi' is being set as a possible answer-form for questions like https://www.khanacademy.org/math/cc-fourth-grade-math/cc-4th-place-value-rounding/cc-4th-rounding/e/rounding-whole-numbers-2 But it is, and rather than have to fix that up, and then trust editors to properly say "this answer involves pi" at all, let's just be better about guessing whether a number is a multiple of pi. This is probably not perfect, but I think is good enough. If we're worried this will suppress legitimate messaging, we could just suppress it if the answer is an integer, which I expect will catch 95% of the confusing cases now. This resolves https://app.asana.com/0/27216215224639/96623486099021/f This also resolves T2578 Test Plan: Ran `python -m SimpleHTTPServer` and visited localhost:8000/test Also will try the rounding-whole-numbers questions after this is deployed. I expect not to get a "approximated pi" warning if I say xxxx0001 as an answer. Reviewers: eater Reviewed By: eater Subscribers: jared Maniphest Tasks: T2578 Differential Revision: https://phabricator.khanacademy.org/D26043
1 parent 9b32d7c commit 5b40b89

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.FAKE: pack packed lint fix_lint
1+
.FAKE: serve pack packed lint fix_lint
22

33
# This needs 'npm' on your system, to install jison.
44
# The output file has to be named 'Calculator' because that's how jison
@@ -11,6 +11,14 @@ genfiles/calculator.js: build/calculator/calculator.jison build/calculator/calcu
1111
mv genfiles/Calculator.js genfiles/calculator.js
1212

1313

14+
serve:
15+
python -m SimpleHTTPServer
16+
17+
18+
check:
19+
echo "Run 'make serve' then visit http://localhost:8000/test"
20+
21+
1422
deps:
1523
npm install
1624

test/answer-types.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,4 +1363,34 @@
13631363
}, 400);
13641364
});
13651365

1366+
asyncTest("number pi (approximation is not overzealous 1)", 6, function() {
1367+
setupSolutionArea();
1368+
var $problem = jQuery("#qunit-fixture .problem").append(
1369+
"<p class='solution' data-type='number' data-forms='pi'>565.5</p>"
1370+
);
1371+
1372+
var answerData = Khan.answerTypes.number.setup($("#solutionarea"),
1373+
$problem.children(".solution"));
1374+
1375+
testAnswer(answerData, "565.5", "right", "right answer is right");
1376+
testAnswer(answerData, "565", "wrong", "wrong answer is wrong");
1377+
1378+
start();
1379+
});
1380+
1381+
asyncTest("number pi (approximation is not overzealous 2)", 6, function() {
1382+
setupSolutionArea();
1383+
var $problem = jQuery("#qunit-fixture .problem").append(
1384+
"<p class='solution' data-type='number' data-forms='pi'>0.833333333333333333</p>"
1385+
);
1386+
1387+
var answerData = Khan.answerTypes.number.setup($("#solutionarea"),
1388+
$problem.children(".solution"));
1389+
1390+
testAnswer(answerData, "5/6", "right", "right answer is right");
1391+
testAnswer(answerData, ".833", "wrong", "wrong answer is wrong");
1392+
1393+
start();
1394+
});
1395+
13661396
})();

utils/answer-types.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,31 @@ Khan.answerTypes = $.extend(Khan.answerTypes, {
544544
possibilities = _.reduce(Khan.answerTypes.predicate.defaultForms.split(/\s*,\s*/), function(memo, form) {
545545
return memo.concat(forms[form](text));
546546
}, []);
547-
$.each(possibilities, function(ix, possibility) {
548-
possibility.piApprox = true;
549-
});
547+
// If the answer is a floating point number that's
548+
// near a multiple of pi, mark is as being possibly
549+
// an approximation of pi. We actually check if
550+
// it's a plausible approximation of pi/12, since
551+
// sometimes the correct answer is like pi/3 or pi/4.
552+
// We also say it's a pi-approximation if it involves
553+
// x/7 (since 22/7 is an approximation of pi.)
554+
// Never mark an integer as being an approximation
555+
// of pi.
556+
var approximatesPi = false;
557+
var number = parseFloat(text);
558+
if (!isNaN(number) && number !== parseInt(text)) {
559+
var piMult = Math.PI / 12;
560+
var roundedNumber = piMult * Math.round(number / piMult);
561+
if (Math.abs(number - roundedNumber) < 0.01) {
562+
approximatesPi = true;
563+
}
564+
} else if (text.match(/\/\s*7/)) {
565+
approximatesPi = true;
566+
}
567+
if (approximatesPi) {
568+
$.each(possibilities, function(ix, possibility) {
569+
possibility.piApprox = true;
570+
});
571+
}
550572
return possibilities;
551573
}
552574

0 commit comments

Comments
 (0)