Java:合取不等式的最少运算次数?

发布于 2024-08-31 01:25:42 字数 906 浏览 6 评论 0 原文

我尝试简化以下条件:

for ( int t=0, size=fo.getPrintViewsPerFile().size();
          t<size && t<countPerFile;
          t++)
{
    // ...
}

,更准确地说:

t<s && t<c

您需要比较两次,然后计算它们的布尔值。有没有更简单的方法来做到这一点?如果不是,你如何证明呢?我可以在某种程度上简化它, 证明树。

[已添加]

我试图直接通过逻辑来解决问题。看看选择最小值的含义会​​很有趣。链接:

http://www.umsu.de/logik/trees/?f=< /a>(\exists%20s%20\exists%20c%20\forall%20t%20%20(Pts%20\land%20Ptc))\leftrightarrow\neg(\foralls\forallc\existst(\neg(Pts) )\lor\neg(Ptc)))

I try to simplify conditionals in:

for ( int t=0, size=fo.getPrintViewsPerFile().size();
          t<size && t<countPerFile;
          t++)
{
    // ...
}

, more precisely:

t<s && t<c

You need to compare two times, then calc the boolean value from them. Is there any simpler way to do it? If no, how can you prove it? I can simplify it to some extent, proof tree.

[Added]

I tried to solve the problem directly through logic. It would interesting to see the implication in choosing the minima. Link:

http://www.umsu.de/logik/trees/?f=(\exists%20s%20\exists%20c%20\forall%20t%20%20(Pts%20\land%20Ptc))\leftrightarrow\neg(\foralls\forallc\existst(\neg(Pts)\lor\neg(Ptc)))

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

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

发布评论

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

评论(3

天荒地未老 2024-09-07 01:25:42

如果 sizecountPerFile 在循环期间保持不变,则可以在循环之前预先计算它们的最小值,然后测试将是 t.

int size=fo.getPrintViewsPerFile().size();
int minLimit = Math.min(size, countPerFile);
for (int t=0; t<minLimit; t++) {
    ....

If size and countPerFile are constant for the duration of the loop, you can precalculate their minimum before the loop, and then the test would be t<minimum.

int size=fo.getPrintViewsPerFile().size();
int minLimit = Math.min(size, countPerFile);
for (int t=0; t<minLimit; t++) {
    ....
花海 2024-09-07 01:25:42

你可以做 t < Math.min(s, c),但这实际上不会减少比较次数。

我确实认为适当使用 Math.minMath.max 使代码更具可读性。不幸的是,它们只有 2 个参数的重载(对于 intlongfloatdouble 参数)。如果它们也有 3 个参数和可变参数重载,那就太好了。

您始终可以为此类事情编写实用程序方法(间隔检查是常见的习惯用法 (minV <= v) && (v <= maxV) 等),但是 从语言上来说,Java 没有任何奇特的运算符可以完成这些事情。它们只有基本的数字比较运算符 (JLS 15.20 .1)和基本布尔运算符(JLS 15.22.215.23 15.24)。


旁注

高级语言,如 Icon 确实允许这些类型的构造:

Java                      Icon
(x < v1) || (x < v2)      x < (v1 | v2)
(a < b) && (b < c)        a < b < c

You can do t < Math.min(s, c), but that wouldn't actually reduce the number of comparison.

I do think that appropriate use of Math.min and Math.max makes for a much more readable code, though. Unfortunately they only have overloads for 2 args (for int, long, float and double arguments). It would've been really nice if they also have 3 args and varargs overloads.

You can always write utilities method for these kinds of things (interval checking is a common idiom (minV <= v) && (v <= maxV) etc), but linguistically, no Java doesn't have any fancy operators that would do these things. They only have basic numerical comparison operators (JLS 15.20.1) and basic boolean operators (JLS 15.22.2, 15.23, 15.24).


Sidenote

Higher-level languages like Icon does allow these kinds of constructs:

Java                      Icon
(x < v1) || (x < v2)      x < (v1 | v2)
(a < b) && (b < c)        a < b < c
初见终念 2024-09-07 01:25:42

忘记它吧。如果您正在处理打印或文件,这种微观优化几乎不会为您节省任何东西。

Forget about it. If you're dealing with printing or files this kind of micro-optimizaton would save you practically nothing.

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