Questions tagged [strictfp]

`strictfp` is a keyword in the Java programming language that restricts floating-point calculations to ensure portability. It was introduced into Java with JVM version 1.2

strictfp is a keyword in the programming language that restricts floating-point calculations to ensure portability. It was introduced into Java with JVM version 1.2. Prior to JVM 1.2, floating-point calculations were strict; that is, all intermediate floating-point results were represented as IEEE single or double precisions only. As a consequence, errors of calculation (round-off errors), overflows and underflows, would occur with greater frequency than in architectures which did intermediate calculations in greater precision. Since JVM 1.2, intermediate computations are not limited to the standard 32- and 64-bit precisions. On platforms that can handle other representations, e.g. 80-bit double extended on x86 or x86-64 platforms, those representations can be used, helping to prevent round-off errors and overflows, thereby increasing precision. For some applications, a programmer might need every platform to have precisely the same floating-point behavior, even on platforms that could handle greater precision. The strictfp modifier accomplishes this by truncating all intermediate values to IEEE single- and double-precision, as occurred in earlier versions of the JVM.

29 questions
280
votes
11 answers

When should I use the "strictfp" keyword in java?

I've looked up what this does, but does anyone actually have an example of when you would use the strictfp keyword in Java? Has anyone actually found a use for this? Would there be any side-effects of just putting it on all my floating point…
GBa
  • 17,509
  • 15
  • 49
  • 67
23
votes
3 answers

Why is Eclipse asking to declare strictfp inside enum

I was trying out enum type in Java. When I write the below class, public class EnumExample { public enum Day { private String mood; MONDAY, TUESDAY, WEDNESDAY; Day(String mood) { } Day() { } } } Compiler says: Syntax…
Anoop Dixith
  • 633
  • 2
  • 8
  • 20
18
votes
2 answers

Behavior of strictfp keyword with implementing/extending an interface/class

JLS strictfp Interfaces specifies that : The effect of the strictfp modifier is to make all float or double expressions within the interface declaration be explicitly FP-strict (§15.4). This implies that all nested types declared in the interface…
Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
15
votes
1 answer

Does Java strictfp modifier have any effect on modern CPUs?

I know the meaning of the strictfp modifier on methods (and on classes), according to the JLS: JLS 8.4.3.5, strictfp methods: The effect of the strictfp modifier is to make all float or double expressions within the method body be explicitly…
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
9
votes
4 answers

strictfp in Java

I'm implementing some neural network library in java , and there are intensive double (not Double) matrix operations, Matrices are large and performance is required of course. So I came to read about strictfp keyword I honestly didn't understand…
Ismail Marmoush
  • 13,140
  • 25
  • 80
  • 114
9
votes
2 answers

No strictfp in Scala - workarounds?

I searched the web for how to enforce srictfp in Scala but could not find any hint of it. There are some people complaining about it, but real solutions cannot be found. There is a bugtracker entry about it which is almost two years old. As it seems…
Malax
  • 9,436
  • 9
  • 48
  • 64
7
votes
2 answers

Reasons behind why abstract and strictfp keywords cannot be used together in a method declaration?

I'm reading SCJP by katherine sierra. I understand that abstract and final keywords cannot be used together because they contradict each other as explained in the book. However, I don't understand why strictfp and abstract keywords cannot be used…
Ascendant
  • 827
  • 2
  • 16
  • 34
6
votes
1 answer

Does Java's strictfp modifier apply through function calls?

More precisely, if there exists a function in the call stack with the strictfp modifier, will the function at the top of the call stack also adhere to the strictfp specifier? public class Main { // case 1: strictfp not present at top of call…
jvn91173
  • 266
  • 1
  • 8
6
votes
1 answer

Force all Java arithmetic to strictfp at runtime, using javassist?

Given a Java application which was written with performance in mind (i.e. methods are deliberately not declared 'strictfp' in the source code), is it possible to allow users to run the entire application in strictfp mode? It looks like a crude…
6
votes
3 answers

Does declaring a Java class strictfp mean methods it calls in other classes are also strictfp?

As title really... if class X is declared strictfp and calls methods in class Y, will strictness be enforced, or does this only apply to X's own code? Additionally, if I calculate a value in a strictfp class method and pass it into a non-strictfp…
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
6
votes
1 answer

How to make a (static) initializer block strictfp?

While refactoring some code I stumbled over this oddity. It seems to be impossible to control the strictfp property for an initializer without affecting the entire class. Example: public class MyClass { public final static float[] TABLE; …
Durandal
  • 19,919
  • 4
  • 36
  • 70
5
votes
1 answer

Doubt about strictfp and StrictMath

I have a little technical question, is it the same to call: public static strictfp double myMethod(double phi){ return Math.exp(phi); } or: public static double myMethod(double phi){ return StrictMath.exp(phi); } Or does…
Federico Vera
  • 1,357
  • 1
  • 12
  • 18
3
votes
0 answers

Is there still a difference between Math and StrictMath after Java 17 and JEP 306?

JEP 306, implemented in Java 17, provides always-strict floating point semantics, deprecating the strictfp flag. Does this mean that java.lang.Math can be counted on to behave exactly the same as the analogous methods in StrictMath (i.e., that…
Joshua Goldberg
  • 5,059
  • 2
  • 34
  • 39
3
votes
1 answer

Does code becomes non - compliant in someway if strictfp not used?

We have a first code audit coming up and I was told by someone that not using strictfp for floating point arithmetic might get us flagged. Software is coded on Windows machine and deployed to Solaris machines for production use. Any suggestions…
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
3
votes
1 answer

Does Java's % operator ever overflow?

In C and C++, the behavior of INT_MIN % -1 seems to be undefined / platform-dependent as per Shafik's post. In Java, does the % operator ever overflow? Consider this piece of code: public class Test { public static void main(String[] args) { …
Pacerier
  • 86,231
  • 106
  • 366
  • 634
1
2