Strictfp keywords in Java

Source: Internet
Author: User

Strictfp
Strictfp -- Java keyword.
 
Strictfp, that is, strict float point (precise floating point ).
 
The strictfp keyword can be applied to classes, interfaces, or methods. When a method is declared using the strictfp keyword, all float and double expressions in the method strictly abide by the restrictions of FP-strict and comply with the IEEE-754 specification. When the strictfp keyword is used for a class or interface, all the code in the class, including the initial set value and code in the nested type, will be strictly calculated. Strict constraints mean that the results of all expressions must be the result expected by the IEEE 754 algorithm for the operands, expressed in a single-precision and double-precision format.
 
If you want to make your floating point operations more accurate and do not execute inconsistent results on different hardware platforms, you can use the keyword strictfp.
 
Example 1
The following example demonstrates a class declared using the strictfp modifier.
 
  
// Example of precision control with strictfp
 
Public strictfp class MyClass
 
{
 
Public MyClass (){}
 
Public static void main (String [] args)
 
{
 
Float aFloat = 0.6710339f;
 
Double aDouble = 0.04150553411984792d;
 
Double sum = aFloat + aDouble;
 
Float quotient = (float) (aFloat/aDouble );
 
System. out. println ("float:" + aFloat );
 
System. out. println ("double:" + aDouble );
 
System. out. println ("sum:" + sum );
 
System. out. println ("quotient:" + quotient );
 
}
 
}
 
Sample output
 
Float: 0.6710339
 
Double: 0.04150553411984792
 
Sum: 0.71253945297742238
 
Quotient: 16.1673355
 
 
Example 2
 
The following example demonstrates how to declare using the strictfp modifier.
 
  
 
// Example of precision control with strictfp:
 
Public class MyClass2
 
{
 
Public float aFloat;
 
Public double aDouble;
 
Public MyClass2 (){}
 
Public strictfp double add (float a, double B)
 
{
 
Return (a + B );
 
}
 
Public static void main (String [] args)
 
{
 
MyClass2 myClass2 = new MyClass2 ();
 
MyClass2.aFloat = 0.6710339f;
 
MyClass2.aDouble = 0.04150553411984792d;
 
Double sum = myClass2.add (myClass2.aFloat, myClass2.aDouble );
 
System. out. println ("float:" + myClass2.aFloat );
 
System. out. println ("double:" + myClass2.aDouble );
 
System. out. println ("sum:" + sum );
 
}
 
}
Sample output
 
  
 
Float: 0.6710339
 
Double: 0.04150553411984792
 
Sum: 0.71253945297742238
========================================================== ======================================
Since Java2, the Java language has added a keyword strictfp. Strictfp refers to FP-strict, that is, exact floating point. When the strictfp keyword is not specified for floating-point operations on the Java Virtual Machine, the Java compiler and runtime environment perform these operations on the floating-point expressions in an action similar to the self-processing, as a result, you are often not satisfied with the results. Once strictfp is used to declare a class, interface or method, the declared scope of the Java compiler and runtime environment will be fully executed in accordance with the floating point specification IEEE-754. Therefore, if you want to make your floating point operations more accurate and do not use the strictfp keyword because the results of different hardware platforms are inconsistent.
You can declare a class, interface, and method as strictfp, but you cannot declare the strictfp keyword for methods and constructors in the interface. For example, the following code:
1. Valid keyword strictfp
Strictfp interface {}
Public strictfp class FpDemo1 {
Strictfp void f (){}
}
2. Incorrect usage
Interface {
Strictfp void f ();
}
Public class FpDemo2 {
Strictfp FpDemo2 (){}
}
Once the keyword strictfp is used to declare a class, interface, or method, all floating point operations within the scope declared by this keyword are accurate and comply with the IEEE-754 specification. For example, if a class is declared as strictfp, all the methods in this class are strictfp.
Author: zongquanliu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.