File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's strictfp for?

 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been working on the class and method identifiers. I've doing some work to use them all and understand them very well and how I may combine them without causing a compile time error.

However, still I do not understant what's the strictfp modifier for? Does it make a difference to use or not use it? Can somebody refer me to a code snippet that helps me understand?

Thanks in advance!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
strictfp is used if you want your program to produce identical floating point results on all platforms you run it on. If you do not use strictfp then the compiler/jvm is free to optimise the floating point arithmetic for the particular platform the program is compiled/run on.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about this code:
float x, a, b, c;
....
x = a/b/c;

This is translated by the Java compiler into these expressions:
float temp = a/b;
x = temp/c;

In the general case, floating point division gives answers like 1/3 that cannot be expressed exactly in floating point, so the answers are rounded to the nearest value that can be represented in floating point format.

x, a, b, and c are always stored in IEEE-754 standard float format and look the same on any Java platform. If the hardware on your computer uses a different format, perhaps with more precision than a Java float, the Java Virtual Machine must convert the hardware format to and from the nearest IEEE-754 float value, perhaps with some loss of precision.

But what about temp. It takes extra time to convert from your hardware's format to IEEE-754 format so it makes sense to just use the hardware-format result of a/b in the temp/c operation. In addition, format conversions of intermediate results would likely lose some precision, making the final answer less precise. So Java normally leaves intermediate floating point results in local hardware format.

The problem is that different computers have different hardware floating point formats, so intermediate results are different and the final results are different. This violates the Java basic rule of machine independence.

With strictfp, every intermediate result is converted back into IEEE-754 format, then back into local hardware format for the next operation. This takes more time and costs some precision in the answer, but it results in every computer giving the same result running the same program with the same data.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks four your extraordinaryly well documented answer, Mike. That's exactly the answer I was looking for. I guess now I understand what this of strictfp is all about. I have read a lot in the JLS and JVMS, but I could not find there the simplicity and yet the profoundness of your reply.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this section of the Java Language Specification interesting...

http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#184225
 
navneet shrivastava
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thanks to mike for such a simple & straight description
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic