Java的strictfp修饰符是否可以通过函数调用来应用?

jvn*_*173 5 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
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,foo1并且foo2似乎返回相同的值。换句话说,当更下方的函数也具有strictfp修饰符时,调用堆栈顶部的函数是否具有strictfp修饰符似乎并不重要。

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

chr*_*ke- 6

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.

Therefore, calls to external methods or other ways of obtaining a floating-point expression do not "inherit" the FP-strictness of something up the call stack.