Using Math.log() with a different base

Felix D.E.'s icon

Hi there,

I want to calculate the logarithm of y with base x (ie. log x (y )) in js object and i have issue.
I can simply do the math in Javascript as follows:

function getBaseLog(x, y) {
return Math.log(y) / Math.log(x);
}

console.log(getBaseLog(7, 625));
// expected output: 3.3083499013876647


As I have very basic knowledge of js in Max, is there anybody who could help ? any idea would be appreciated.

I tried this code in js object without error, but I don't know where is the issue to get the output value after calculation:

inlets = 2;
outlets = 1;

function msg_int(val){
    switch(inlet){
        case 1:
            post("inlet 2: " + val);
            break;
        default:
            post("inlet 1: " + val);
         break;

        function getBaseLog(x, y) {
if (x = "inlet 1", y = "inlet 2")
    var x = (val);
    var y = (val);
return Math.log(y) / Math.log(x);

}

    }
}

outlet = getBaseLog;

11OLSEN's icon

function getBaseLog(x, y) {
outlet(0, Math.log(y) / Math.log(x) );
}

You should refer to Max javascript docs for the Max specific things like sending something to an outlet: https://docs.cycling74.com/max8/vignettes/jsglobal

Felix D.E.'s icon

@11olsen
Thank you so much for your reply and help. I appreciate it.