如何找到自定义函数的最近两个数字
How to find the nearest two numbers of a custom function
给定一个数字,如何找到最接近的两个以 1、2.5 或 5 开头的数字? (10, 25, 50, 100, 250, 500, 1000...无穷无尽)
结果应该是最接近的大数,并且比它低一个。
例如:号码 420 应该 return 250 和 500。
例如:数字 10 应该 return 10 和 25.
如果有帮助,可以使用 Lodash。
谢谢。
好的,根据您的评论,我想我明白了。
// Finds the two numbers on each side for any number.
function nearest(n) {
let m = multiplier(n);
return nearest10(n / m).map(n => n * m);
}
// Finds the two numbers on each side for numbers between 1 and 10.
function nearest10(n) {
return n < 2.5 ? [1, 2.5] : n < 5 ? [2.5, 5] : [5, 10];
}
// Returns the neareast power of 10 less than or equal to n.
function multiplier(n) {
return Math.pow(10, Math.floor(Math.log10(n)));
}
下面是结果示例:
console.log(nearest(2)); // [1, 2.5]
console.log(nearest(420)); // [250, 500]
console.log(nearest(79310)); // [50000, 100000]
function findPrevAndNext(x){
// returns the next highest value and previous
// lower value in the infinite series
// [1, 2.5, 5, 10, 25, 50, 100, ...]
var top = 5;
while(top < x){
top = top * 10;
}
var mid = top / 2; // ex: 5/2 = 2.5
var bot = top / 5; // ex: 5/5 = 1
var prev, next = 0;
if(x >= mid){
prev = mid;
next = top;
}
else if(x >= bot){
prev = bot;
next = mid;
}
else{
prev = bot / 2;
next = bot
}
return Array(prev,next);
}
先用科学计数法写出数字,去掉刻度。
420 = 4.2x10^2
然后在[1, 2.5)
,[2.5, 5)
,[5, 10)
.
中找到尾数
4.2 in [2.5, 5)
并传递指数,
2.5x10^2 = 250, 5x10^2 = 500
最好使用以 10 为底的对数,
L= log(X) / log(10)
E= floor(L)
L= L - E
if L < log(2.5), LB= pow(10, N), UB= 2.5 * pow(10,N)
else if L < log(5) , LB= 2.5 * pow(10, N), UB= 5 * pow(10,N)
else LB= 5 * pow(10, N), UB= 10 * pow(10,N)
给定一个数字,如何找到最接近的两个以 1、2.5 或 5 开头的数字? (10, 25, 50, 100, 250, 500, 1000...无穷无尽)
结果应该是最接近的大数,并且比它低一个。
例如:号码 420 应该 return 250 和 500。 例如:数字 10 应该 return 10 和 25.
如果有帮助,可以使用 Lodash。
谢谢。
好的,根据您的评论,我想我明白了。
// Finds the two numbers on each side for any number.
function nearest(n) {
let m = multiplier(n);
return nearest10(n / m).map(n => n * m);
}
// Finds the two numbers on each side for numbers between 1 and 10.
function nearest10(n) {
return n < 2.5 ? [1, 2.5] : n < 5 ? [2.5, 5] : [5, 10];
}
// Returns the neareast power of 10 less than or equal to n.
function multiplier(n) {
return Math.pow(10, Math.floor(Math.log10(n)));
}
下面是结果示例:
console.log(nearest(2)); // [1, 2.5]
console.log(nearest(420)); // [250, 500]
console.log(nearest(79310)); // [50000, 100000]
function findPrevAndNext(x){
// returns the next highest value and previous
// lower value in the infinite series
// [1, 2.5, 5, 10, 25, 50, 100, ...]
var top = 5;
while(top < x){
top = top * 10;
}
var mid = top / 2; // ex: 5/2 = 2.5
var bot = top / 5; // ex: 5/5 = 1
var prev, next = 0;
if(x >= mid){
prev = mid;
next = top;
}
else if(x >= bot){
prev = bot;
next = mid;
}
else{
prev = bot / 2;
next = bot
}
return Array(prev,next);
}
先用科学计数法写出数字,去掉刻度。
420 = 4.2x10^2
然后在[1, 2.5)
,[2.5, 5)
,[5, 10)
.
4.2 in [2.5, 5)
并传递指数,
2.5x10^2 = 250, 5x10^2 = 500
最好使用以 10 为底的对数,
L= log(X) / log(10)
E= floor(L)
L= L - E
if L < log(2.5), LB= pow(10, N), UB= 2.5 * pow(10,N)
else if L < log(5) , LB= 2.5 * pow(10, N), UB= 5 * pow(10,N)
else LB= 5 * pow(10, N), UB= 10 * pow(10,N)