UNPKG

btm-expressions

Version:

BTM (bowtie-math) is a math object model to enable parsing of mathematical expressions into a tree structure that can be manipulated, evaluated, and compared.

140 lines (115 loc) 3.32 kB
/*! * BTM JavaScript Library v@VERSION * https://github.com/dbrianwalton/BTM * * Copyright D. Brian Walton * Released under the MIT license (https://opensource.org/licenses/MIT) * * Date: @DATE */ /* ******************************* ** Define a generic class to work numbers ******************************* */ export class real_number { constructor(a) { if (typeof a === 'number' || a instanceof Number) { this.number = a; } else if (a instanceof real_number) { this.number = a.number; } } // Return a numerical value of the rational expression. value() { return this.number; } // Real numbers have no natural simplification, but declaring the method. simplify() { } equal(other) { if (typeof other === 'number') { other = new real_number(other); } return (this.value()==other.value()); } // Add numbers. add(other) { if (typeof other === 'number') { other = new real_number(other); } var sum = new real_number(this.number + other.value()); return(sum); } // Subtract this - other subtract(other) { if (typeof other === 'number') { other = new real_number(other); } var sum = new real_number(this.number - other.value()); return(sum); } // Multiply this rational by another rational number and create new object. multiply(other) { if (typeof other === 'number') { other = new real_number(other); } var product = new real_number(this.number * other.value()); return(product); } // Divide this rational by another rational number and create new object. divide(other) { if (typeof other === 'number') { other = new real_number(other); } var product; if (other.value != 0) { product = new real_number(this.number / other.value()); } else { product = new real_number(NaN); } return(product); } // Additive Inverse addInverse() { var inverse = new real_number(-this.number); return(inverse); } // Multiplicative Inverse multInverse() { var inverse; if (this.number != 0) { inverse = new real_number(this.number); } else { inverse = new real_number(NaN); } return(inverse); } toString(leadSign) { if (typeof leadSign == 'undefined') { leadSign = false; } var str = (leadSign && this.number>0) ? '+' : ''; if (isNaN(this.number)) { str = 'NaN'; } else { str = str + Number(this.number.toFixed(10)); } return(str); } // Format the rational number as TeX string. toTeX(leadSign) { if (typeof leadSign == 'undefined') { leadSign = false; } var str = (leadSign && this.number>0) ? '+' : ''; if (isNaN(this.number)) { str = '\\mathrm{NaN}'; } else { str = str + Number(this.toString(leadSign)); } return(str); } // Format as a root MathML element. toMathML(leadSign) { return("<cn>" + this.toString() + "</cn>"); } }