UNPKG

mathjs

Version:

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.

131 lines (110 loc) 3.31 kB
<!DOCTYPE html> <html> <head> <title>math.js | angle configuration</title> <style> body, input, select { font: 11pt sans-serif; } input, select, th, #result { padding: 5px 10px; } th { text-align: left; } </style> <script src="../../dist/math.js"></script> </head> <body> <p> This code example extends the trigonometric functions of math.js with configurable angles: degrees, radians, or gradians. </p> <table> <tr> <th>Angles</th> <td> <select id="angles"> <option value="deg">deg</option> <option value="grad">grad</option> <option value="rad">rad</option> </select> </td> </tr> <tr> <th>Expression</th> <td> <input id="expression" type="text" value="sin(45)" /> <input id="evaluate" type="button" value="Evaluate"> </td> </tr> <tr> <th>Result</th> <td id="result"></td> </tr> </table> <script> var replacements = {}; // our extended configuration options var config = { angles: 'deg' // 'rad', 'deg', 'grad' }; // create trigonometric functions replacing the input depending on angle config ['sin', 'cos', 'tan', 'sec', 'cot', 'csc'].forEach(function(name) { var fn = math[name]; // the original function replacements[name] = function replacement(x) { if (x instanceof math.type.BigNumber) { x = x.toNumber(); // convert to number } if (typeof x === 'boolean') { x = +x; // convert to number } if (typeof x === 'number') { // convert from configured type of angles to radians switch(config.angles) { case 'deg': return fn(x / 360 * 2 * Math.PI); case 'grad': return fn(x / 400 * 2 * Math.PI); default: return fn(x); } } if (math.collection.isCollection(x)) { return math.collection.deepMap(x, replacement); } return fn(x); }; }); // create trigonometric functions replacing the output depending on angle config ['asin', 'acos', 'atan', 'atan2'].forEach(function(name) { var fn = math[name]; // the original function replacements[name] = function replacement(x) { var result = fn(x); if (typeof result === 'number') { // convert to radians to configured type of angles switch(config.angles) { case 'deg': return result / 2 / Math.PI * 360; case 'grad': return result / 2 / Math.PI * 400; default: return result; } } if (math.collection.isCollection(x)) { return math.collection.deepMap(x, replacement); } return result; }; }); // import all replacements into math.js, override existing trigonometric functions math.import(replacements, {override: true}); // pointers to the input elements var expression = document.getElementById('expression'); var evaluate = document.getElementById('evaluate'); var result = document.getElementById('result'); var angles = document.getElementById('angles'); // attach event handlers for select box and button angles.onchange = function () { config.angles = this.value; }; evaluate.onclick = function () { result.innerHTML = math.eval(expression.value); }; </script> </body> </html>