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

Commit ade9870

Browse files
author
Ben Eater
committed
Add log_10() function to calculator
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
1 parent b626f73 commit ade9870

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

build/calculator/calculator-tail.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ window.Calculator = (function(parser) {
9292
throw new CalculatorError($._("undefined"));
9393
}
9494
return ans;
95+
},
96+
log: function(a) {
97+
var ans = Math.log(a) / Math.LN10;
98+
if (isNaN(ans) || !isFinite(ans)) {
99+
throw new CalculatorError($._("undefined"));
100+
}
101+
return ans;
95102
}
96103
};
97104

exercises/khan-exercise.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@
8181
<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>
8282
</div>
8383
<div class="calc-row">
84-
<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>
84+
<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>
8585
</div>
8686
<div class="calc-row">
87-
<a href="#" class="dark">7</a><a href="#" class="dark">8</a><a href="#" class="dark">9</a>
87+
<a href="#" class="dark">7</a><a href="#" class="dark">8</a><a href="#" class="dark">9</a><a href="#">(</a><a href="#">)</a>
8888
</div>
8989
<div class="calc-row">
9090
<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>

genfiles/calculator.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/calculator.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@
5454
});
5555

5656

57-
asyncTest("pi, euler and natural logarithm", 6, function() {
57+
asyncTest("pi, euler and logarithms", 9, function() {
5858
calculateStartsWith('pi', 3.1415);
5959
calculateStartsWith('2*pi', 6.2831);
6060
calculateStartsWith('e', 2.7182);
6161
calculateStrictEqual('ln(e^2)', 2);
6262
checkError('ln(-1)', /undefined/, 'ln for x < 0 is undefined (for x=-1)');
6363
checkError('ln(0)', /undefined/, 'ln for 0 is -infinity (undefined?)');
64+
calculateStrictEqual('log(10000)', 4);
65+
checkError('log(-1)', /undefined/, 'log for x < 0 is undefined (for x=-1)');
66+
checkError('log(0)', /undefined/, 'log for 0 is -infinity (undefined?)');
6467
start();
6568
});
6669

0 commit comments

Comments
 (0)