java - Java 的 strictfp 修饰符是否通过函数调用应用?

标签 java floating-point strictfp

更准确地说,如果调用堆栈中存在带有 strictfp 修饰符的函数,那么调用堆栈顶部的函数是否也遵守 strictfp 说明符?

public class Main {

    // case 1: strictfp not present at top of call stack
    private static double bar1(double x) {
        return Math.sin(x);
    }

    strictfp private static double foo1(double x) {
        return bar1(x);
    }

    // case 2: strictfp present at top of call stack
    strictfp private static double bar2(double x) {
        return Math.sin(x);
    }

    strictfp private static double foo2(double x) {
        return bar2(x);
    }

    public static void main(String[] args) {
        double x = 10.0;
        System.out.println(foo1(x)); // -0.5440211108893698
        System.out.println(foo2(x)); // -0.5440211108893698
    }
}

在此示例中,foo1foo2 似乎返回相同的值。换句话说,当调用堆栈顶部的函数也有 strictfp 修饰符时,调用堆栈顶部的函数是否有 strictfp 修饰符看起来并不重要。

这总是成立的吗?如果我为 x 选择不同的值会怎样?如果我选择正弦以外的浮点运算怎么办?

最佳答案

JLS 15.4 :

If an expression is not a constant expression, then consider all the class declarations, interface declarations, and method declarations that contain the expression. If any such declaration bears the strictfp modifier (§8.1.1.3, §8.4.3.5, §9.1.1.2), then the expression is FP-strict.

[...]

It follows that an expression is not FP-strict if and only if it is not a constant expression and it does not appear within any declaration that has the strictfp modifier.

因此,调用外部方法或其他获取浮点表达式的方法不会“继承”调用堆栈中某些东西的 FP 严格性。

关于java - Java 的 strictfp 修饰符是否通过函数调用应用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57341891/

相关文章:

java - 如果不使用 strictfp,代码是否会以某种方式变得不兼容?

java - SSL证书。未找到 EAP

java - 从文本中提取名词 (Java)

java - 重构大型 JSON 多次调用

C++ 浮点算术运算

javascript - jQuery data() 将字符串视为数字的问题

python - 如何根据升序过滤列表?

java - 为什么 Eclipse 要求在枚举中声明 strictfp

java - org.hibernate.MappingException 使用注解

java - 来自 Java 编译器的 Android 中的 strictpf