• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

What's strictfp for?

 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more thanks to mike for such a simple & straight description
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic