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

Commit

Permalink
Add log_10() function to calculator
Browse files Browse the repository at this point in the history
Test Plan:
Loaded http://exercises.ka.local/test and watched tests pass
Loaded http://exercises.ka.local/exercises/law_of_cosines.html
Calculator looked reasonable.
Clicking "log" button inserted "log("
Entered "log(10)" and got 1
Entered "log(1000)" and got 3
Entered "log(0)" and got an error
Entered "log(0.001)" and got -3
Entered "log(e)" and got something irrational looking (0.434294482)
Entered "ln(e)" and got 1

Reviewers: charlie, joel

Reviewed By: joel

Subscribers: joel, justin

Maniphest Tasks: T2681

Differential Revision: http://phabricator.khanacademy.org/D10486
  • Loading branch information
Ben Eater committed Jun 26, 2014
1 parent b626f73 commit ade9870
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
7 changes: 7 additions & 0 deletions build/calculator/calculator-tail.js
Expand Up @@ -92,6 +92,13 @@ window.Calculator = (function(parser) {
throw new CalculatorError($._("undefined"));
}
return ans;
},
log: function(a) {
var ans = Math.log(a) / Math.LN10;
if (isNaN(ans) || !isFinite(ans)) {
throw new CalculatorError($._("undefined"));
}
return ans;
}
};

Expand Down
4 changes: 2 additions & 2 deletions exercises/khan-exercise.html
Expand Up @@ -81,10 +81,10 @@
<a href="#" data-text="sin(">sin</a><a href="#" data-text="cos(">cos</a><a href="#" data-text="tan(">tan</a><a href="#" data-text="sqrt("></a><a href="#" data-text="^">x<sup>y</sup></a>
</div>
<div class="calc-row">
<a href="#" data-text="e^">e<sup>x</sup></a><a href="#" data-text="ln(">ln</a><a href="#" data-text="pi">&pi;</a><a href="#">(</a><a href="#">)</a>
<a href="#" data-text="e^">e<sup>x</sup></a><a href="#" data-text="ln(">ln</a><a href="#" data-text="log(">log</a><a href="#" data-text="pi">&pi;</a>
</div>
<div class="calc-row">
<a href="#" class="dark">7</a><a href="#" class="dark">8</a><a href="#" class="dark">9</a>
<a href="#" class="dark">7</a><a href="#" class="dark">8</a><a href="#" class="dark">9</a><a href="#">(</a><a href="#">)</a>
</div>
<div class="calc-row">
<a href="#" class="dark">4</a><a href="#" class="dark">5</a><a href="#" class="dark">6</a><a href="#" data-text="*">×</a><a href="#" data-text="/">÷</a>
Expand Down
7 changes: 7 additions & 0 deletions genfiles/calculator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion test/calculator.js
Expand Up @@ -54,13 +54,16 @@
});


asyncTest("pi, euler and natural logarithm", 6, function() {
asyncTest("pi, euler and logarithms", 9, function() {
calculateStartsWith('pi', 3.1415);
calculateStartsWith('2*pi', 6.2831);
calculateStartsWith('e', 2.7182);
calculateStrictEqual('ln(e^2)', 2);
checkError('ln(-1)', /undefined/, 'ln for x < 0 is undefined (for x=-1)');
checkError('ln(0)', /undefined/, 'ln for 0 is -infinity (undefined?)');
calculateStrictEqual('log(10000)', 4);
checkError('log(-1)', /undefined/, 'log for x < 0 is undefined (for x=-1)');
checkError('log(0)', /undefined/, 'log for 0 is -infinity (undefined?)');
start();
});

Expand Down

0 comments on commit ade9870

Please sign in to comment.