jiggzson / nerdamer

a symbolic math expression evaluator for javascript
http://www.nerdamer.com
MIT License
517 stars 82 forks source link

Line 7759 => new definition of log, works with every combinations of multiplications and powers #576

Closed michalmicpaw closed 3 years ago

michalmicpaw commented 4 years ago

Hi.

I've spend few hours but i did it! You can change fefinition in 7759 from:

else if (symbol.value === 'e' && symbol.multiplier.equals(1)) {
        var p = symbol.power;
        retval = isSymbol(p) ? p : new Symbol(p);
}

to this:

else if (symbol.value === 'e') {
    var sMul = Number(new Symbol(symbol.multiplier));
    var sPow = Number(new Symbol(symbol.power));

    if (base === undefined)
        return new Symbol(Math.log(sMul) + sPow);                

    var bPow = Number(new Symbol(base.power));
    var bMul = Number(new Symbol(base.multiplier));
    var bVal = Number(base);

    if (base.value === 'e') //log(e^2, e) retval = new Symbol(sPow / bPow);
        retval = new Symbol((Math.log(sMul) + sPow) / (Math.log(bMul) + bPow));
    else // log(e^2, 2)
        retval = new Symbol((Math.log(sMul) + sPow) / Math.log(bVal));
}

phew..

Now you can write

log(1.2*e^4.3, 7.8*e^4.9)
jiggzson commented 3 years ago

@michalmicpaw, thank you for looking into this. I tried your solution but unfortunately it broke 4 cases. See below.

Failures:
1) Nerdamer core should compute logarithms correctly
  Message:
    Expected 'NaN' to equal 'e'.
  Stack:
    Error: Expected 'NaN' to equal 'e'.
        at <Jasmine>
        at UserContext.<anonymous> (~/nerdamer/spec/core.spec.js:1076:39)
        at <Jasmine>
  Message:
    Expected NaN to equal 2.71828182845905.
  Stack:
    Error: Expected NaN to equal 2.71828182845905.
        at <Jasmine>
        at UserContext.<anonymous> (~/nerdamer/spec/core.spec.js:1077:38)
        at <Jasmine>
  Message:
    Expected '-NaN' to equal '-e'.
  Stack:
    Error: Expected '-NaN' to equal '-e'.
        at <Jasmine>
        at UserContext.<anonymous> (~/nerdamer/spec/core.spec.js:1076:39)
        at <Jasmine>
  Message:
    Expected NaN to equal -2.71828182845904.
  Stack:
    Error: Expected NaN to equal -2.71828182845904.
        at <Jasmine>
        at UserContext.<anonymous> (~/nerdamer/spec/core.spec.js:1077:38)
        at <Jasmine>

However, I think I may have achieved what you were aiming at. It's currently fixed on the dev branch. Let me know. Thanks for your effort. I really appreciate it.