【发布时间】:2019-08-03 20:32:33
【问题描述】:
更准确地说,如果调用堆栈中存在带有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
}
}
在此示例中,foo1 和 foo2 似乎返回相同的值。换句话说,调用堆栈顶部的函数是否具有 strictfp 修饰符似乎并不重要,而更下方的函数也具有该修饰符。
这总是成立吗?如果我为x 选择不同的值怎么办?如果我选择正弦以外的浮点运算怎么办?
【问题讨论】:
-
我认为
strictfp不适用于方法调用。我认为这是一个编译时间标志,以不同方式编译您的类和方法中的代码,它不会影响您调用的单独编译的代码。
标签: java floating-point strictfp