I am currently trying to create a function that increment numbers. The thing is that I would like the function to be able to change the incrementing value each time a "power of ten" is reached. I have a first function that handle the "incrementing value".
Here is what I have so far:
function getInc(num) {
let abs = Math.abs(num);
let inc = Math.pow(10, Math.floor(Math.log10(abs)));
if (abs === num) return inc;
if (num === -10) { return -1; }
if (num === -1) { return -0.1; }
return -inc
}
This works well, except from values likes -10, -0.1.
For : getInc(-0.1)
the result should be -0.01, but my current function returns -0.1
I would like to avoid lines like if (num === -10) { return -1; }
because I can not handle all the cases this way, but I am a little stuck here.
Thank you in advance.
Edit: this is the rest of the code, the function that actually increments, if this can help understand how the getInc result is used:
if ((num < -1) && (num >= -10) ) {
return Math.floor(Math.round(num) - getInc(num))
}
if ((num <= 0) && (num >= -1) ) {
return num - getInc(num)
}
if (num >= 1 && num < 10) {
return Math.round(Math.floor(num getInc(num))) ;
}
if ((num >= 10) || (num > 0 && num < 1)) {
const result = Math.ceil(num / getInc(num)) * getInc(num);
if (result === num) { return num getInc(num) }
else { return result }
}
if (num < -10) {
const result = Math.ceil(num / getInc(num)) * getInc(num) - getInc(num);
if (result === num) { return num getInc(num) }
else { return result }
}
}
Desired results example:
getInc(-10) : -1
getInc(-4) : -1
getInc(-1) : -0.1
getInc(-0.4) : -0.1
getInc(-0.1) : -0.01
getInc(-0.04) : -0.01
getInc(-0.01) : -0.001
For now I have:
getInc(-10) : -10
getInc(-4) : -1
getInc(-1) : -1
getInc(-0.4) : -0.1
getInc(-0.1) : -0.1
getInc(-0.04) : -0.01
getInc(-0.01) : -0.01
CodePudding user response:
You could check the sign if the log value is an integer.
function getInc(num) {
const
sign = Math.sign(num),
log = Math.log10(Math.abs(num));
return sign * Math.pow(10, Math.floor(log) - (sign === -1 && Number.isInteger(log)));
}
console.log(getInc(9)); // 1
console.log(getInc(10)); // 10
console.log(getInc(-10)); // -1 adjust - 1
console.log(getInc(-4)); // -1
console.log(getInc(-1)); // -0.1 adjust - 1
console.log(getInc(-0.4)); // -0.1
console.log(getInc(-0.1)); // -0.01 adjust - 1
console.log(getInc(-0.04)); // -0.01
console.log(getInc(-0.01)); // -0.001 adjust - 1
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
your problem are, that numbers, that are already a power of ten are not changed by the expression Math.pow(10, Math.floor(Math.log10(abs)))
(because of Math ... if num === power of 10 --> Math.log10(num) === integer (not float) ---> Math.flat() has no effect on integers --> the expression remaining is Math.pow(10, Math.log10(num))
which is the same as just num)
But you want numbers, that already are a power of ten to return a power of ten (but the exponent being one lower as the exponent of num - in easy words, you want the number to get divided by ten)
so at the start of your function you check for numbers, that already are a power of ten ... if that is the case, return the number divided by ten if it is not already a power of ten proceed as it did before.
So your code basically stays the same (you just exit the function early in case of numbers already being a power of ten (and return the number divided by ten)
here's your code with the if clause implemented:
function getInc(num) {
let abs = Math.abs(num);
if (Math.log10(abs) % 1 === 0) return num / 10;
let inc = Math.pow(10, Math.floor(Math.log10(abs)));
if (abs === num) return inc;
return -inc;
}
console.log(getInc(100));
console.log(getInc(4));
console.log(getInc(0.1));
console.log(getInc(-4));
console.log(getInc(-1));
console.log(getInc(-0.4));
console.log(getInc(-0.1));