mathjs/examples/basic_usage.js
2013-04-19 21:43:28 +02:00

39 lines
1006 B
JavaScript

// basic usage
// load math.js
var math = require('../math.js');
/**
* Helper function to output a value in the console. Value will be formatted.
* @param {*} value
*/
function print (value) {
console.log(math.format(value));
}
// functions and constants
console.log('functions and constants');
print(math.round(math.e, 3)); // 2.718
print(math.atan2(3, -3) / math.pi); // 0.75
print(math.log(1000, 10)); // 3
print(math.sqrt(-4)); // 2i
print(math.pow([[-1, 2], [3, 1]], 2)); // [[7, 0], [0, 7]]
console.log();
// expressions
console.log('expressions');
print(math.eval('1.2 / (2.3 + 0.7)')); // 0.4
print(math.eval('5.08 cm in inch')); // 2 inch
print(math.eval('sin(45 deg) ^ 2')); // 0.5
print(math.eval('9 / 3 + 2i')); // 3 + 2i
print(math.eval('det([-1, 2; 3, 1])')); // -7
console.log();
// chained operations
console.log('chained operations');
var a = math.select(3)
.add(4)
.multiply(2)
.done();
print(a); // 14