Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 what it does exactly and I was looking for simple explanation about If i should be using it or not and why

like image 601
Ismail Marmoush Avatar asked Mar 03 '11 13:03

Ismail Marmoush


People also ask

What does strictfp mean in Java?

strictfp is a modifier that stands for strict floating-point which was not introduced in the base version of java as it was introduced in Java version 1.2.

What is the use of strictfp?

strictfp is used to ensure that floating points operations give the same result on any platform. As floating points precision may vary from one platform to another. strictfp keyword ensures the consistency across the platforms.

What is strictfp modifier?

strictfp is an obsolete and unused reserved word in the Java programming language. Previously, this keyword was used as a modifier that restricted floating-point calculations to IEEE 754 semantics in order to ensure portability.

Why we are using this keyword in Java?

Definition and Usage The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).


1 Answers

strictfp indicates that floating point calculations should use the exact IEEE754 standard. Without strictfp, the VM is free to use other (but platform dependent) representations of intermediate float and double values, in order to increase precision.

Use strictfp if you need the exact same results on multiple platforms. Avoid it if you want the best precision your current platform can give you.

E.g. in the following simple addition:

  2.0 + 1.1 + 3.0

Do you want the intermediate results (e.g. 2.0 + 1.1) to be represented as an IEEE754 standard double, or with the best possible precision your platform allows. strictfp ensures the first, not using strictfp allows the VM to use the second alternative.

Not using strictfp will not hurt performance, and may on platforms where the native float types don't map to IEEE754 increase performance, since the VM isn't required to convert back and forth in between native and IEEE754 formats. The answer is platform dependent, you'll need to measure.

like image 99
Erik Avatar answered Oct 14 '22 03:10

Erik