Java中确定十进制值最快最有效的方法是整数

发布于 2024-08-08 03:08:27 字数 187 浏览 6 评论 0原文

给定一个名为 sizeValue 的双精度变量,且 sizeValue 包含 0 以外的值,确定 sizeValue 包含以下值的最有效方法是什么一个整数?

目前我正在使用

sizeValue % 1 == 0

还有其他更快的方法吗?

Given a double variable named sizeValue and sizeValue contains something other than 0, what is the most efficient way to determine that sizeValue contains a value that is an integer?

Currently i'm using

sizeValue % 1 == 0

any other faster ways?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反目相谮 2024-08-15 03:08:27

尝试一下 Math.ceil

private static boolean isInt(double x) {
     return x == Math.ceil(x);
}

编辑

我用以下方法做了一些基准测试:

private static boolean isInt1(double x) {
    return x == (int) x;
}

private static boolean isInt2(double x) {
    return x == Math.ceil(x);
}

private static boolean isInt3(double x) {
    return x % 1 == 0;
} 

isInt1是其中更快的一个(在sunjre 1.6上)

give a try to Math.ceil:

private static boolean isInt(double x) {
     return x == Math.ceil(x);
}

EDIT

I've done some benchmarks with the following methods:

private static boolean isInt1(double x) {
    return x == (int) x;
}

private static boolean isInt2(double x) {
    return x == Math.ceil(x);
}

private static boolean isInt3(double x) {
    return x % 1 == 0;
} 

isInt1 is the faster of them (on a sunjre 1.6)

七堇年 2024-08-15 03:08:27

我不确定它是否更快,但您可以将 double 转换为 int 并测试是否相等:

double d = 123.456;
boolean dIsAnInteger = (d == (int)d);

I am not sure if it is any faster, but you could cast your double to an int and test for equality:

double d = 123.456;
boolean dIsAnInteger = (d == (int)d);
仙女 2024-08-15 03:08:27

警告:您会认为 0.9999999999999999 是一个整数吗?可能不会。但请注意:

double val = 0;
for(int i=0;i<10;i++)
    System.out.println(val+=0.1);

这会打印出:

0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999

这是有问题的,因为在程序末尾 val 包含一些您认为应该是整数但实际上不是的东西。

因此,我会通过集成像 JUnit 确实如此:

private static boolean isInt(double x, double delta) {
    double ceil = Math.ceil(x);
    return x-delta<ceil && x+delta>ceil;
}

当然还提供了具有合理默认值的相同方法:

private static boolean isInt(double x) {
    return isInt(x, 0.000000001);
}

现在 isInt(val) 终于返回 true。

Warning: would you consider 0.9999999999999999 an integer? Probably not. But watch this:

double val = 0;
for(int i=0;i<10;i++)
    System.out.println(val+=0.1);

This prints out:

0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999

Which is problematic because at the end of the program val contains something that you think should be integer but is not.

So I'd make my method a bit slower (but more correct) by integrating a delta like JUnit does:

private static boolean isInt(double x, double delta) {
    double ceil = Math.ceil(x);
    return x-delta<ceil && x+delta>ceil;
}

and of course provide the same method with a sensible default:

private static boolean isInt(double x) {
    return isInt(x, 0.000000001);
}

Now isInt(val) finally returns true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文