简体   繁体   English

如何向下舍入到最接近10的幂?

[英]How to round down to the nearest power of 10?

I can't even find search keywords for this. 我甚至找不到搜索关键字。 Please consider this code: 请考虑以下代码:

float inputValue = getInputValue();
float resultValue;

if (inputValue < 0.1f) {
    resultValue = 0.01f;
}
else if (inputValue < 1.0f) {
    resultValue = 0.1f;
}
else if (inputValue < 10.0f) {
    resultValue = 1.0f;
}
else {
    resultValue = 10.0f;
}

and so on. 等等。 There must be a more elegant way to do this. 必须有一种更优雅的方式来做到这一点。 I guess the solution is easy, but I try to find a way now for 2 hours and read about round, ceil, floor...can't find it. 我想这个解决方案很容易,但是我试着找到一种方法,现在已经有2个小时,并且阅读了关于圆形,细胞,地板......无法找到它。

Has anyone an idea? 有人有想法吗?

powf(10.0f, floorf(log10f(value)))

I'm sure there's a much simpler way that doesn't try to work forward from first principles, but how about: 我确信有一种更简单的方法,不会尝试从第一原则向前推进,但如何:

float inputValue = getInputValue();

float log10 = floorf(log10f(inputValue));
float modulo = powf(10.0f, log10);
inputValue -= fmodf(inputValue, modulo);

EDIT: actually, I think I've assumed you'd want 230 to round to 200, 0.73 to round to 0.7, etc, so this probably isn't the answer. 编辑:实际上,我想我已经假设你想要230轮到200,0.73轮到0.7等,所以这可能不是答案。 I'm leaving it up on the basis that it may be helpful regardless. 我把它留下来,因为无论如何它可能会有所帮助。

Your code isn't doing what you think it does. 您的代码没有按照您的想法执行。 For any value less than 100 (including 0.001), resultValue will be set to 10. You'd need to check in the opposite order. 对于小于100(包括0.001)的任何值,resultValue将设置为10.您需要以相反的顺序检查。

I'd start by writing down the spec for this function: Exactly what output values do you expect for what input values? 我首先写下这个函数的规范:对于什么输入值,你期望什么输出值? Do you expect 1.0e17 for an input of 1.01e17? 您是否期望1.0e17输入1.01e17? What if the input is 0, or -1? 如果输入为0或-1怎么办?

Here is a BigInteger version. 这是一个BigInteger版本。 I needed the next (rather than nearest) power of 10, but you see the idea: 我需要下一个(而不是最接近的)10的幂,但你看到了这个想法:

import java.math.BigInteger;

class A {
    public static void main(String[]args) {
        System.out.println("hi");
    for (long v : new long[] {0,1,2,9,10,11,99,100,101,12345678,123456789012345678L,1234567890123456789L}) {
            System.out.println(""+v+" ==> "+nextPowerOf10(v));
        }
    }
    static long nextPowerOf10(long v) {
        return BigInteger.TEN.pow((int)Math.max(0,1+Math.floor(Math.log10(v)))).longValue();
    }
}

If you want it to behave differently on powers of 10, play with 1+Math.floor above (0/1, floor/ceil/round). 如果你希望它在10的幂上表现不同,请使用上面的1+Math.floor (0/ 1+Math.floor ,floor / ceil / round)。 By the way, what OP meant under "nearest?" 顺便说一句,OP在“最近”的意思下是什么意思? Nearest on the linear or on the logarithmic scale? 最接近线性还是对数刻度? Nearest greater or nearest smaller? 最近或更近?

>java A
hi
0 ==> 1
1 ==> 10
2 ==> 10
9 ==> 10
10 ==> 100
11 ==> 100
99 ==> 100
100 ==> 1000
101 ==> 1000
12345678 ==> 100000000
123456789012345678 ==> 1000000000000000000
1234567890123456789 ==> -8446744073709551616

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM