¡¡¡¡Strictfp ¡ª¡ª Java ¹Ø¼ü×Ö¡£
¡¡¡¡strictfp, ¼´ strict float point (¾«È·¸¡µã)¡£
¡¡¡¡strictfp ¹Ø¼ü×Ö¿ÉÓ¦ÓÃÓÚÀà¡¢½Ó¿Ú»ò·½·¨¡£Ê¹Óà strictfp ¹Ø¼ü×ÖÉùÃ÷Ò»¸ö·½·¨Ê±£¬¸Ã·½·¨ÖÐËùÓеÄfloatºÍdouble±í´ïʽ¶¼Ñϸñ×ñÊØFP-strictµÄÏÞÖÆ,·ûºÏIEEE-754¹æ·¶¡£µ±¶ÔÒ»¸öÀà»ò½Ó¿ÚʹÓà strictfp ¹Ø¼ü×Öʱ£¬¸ÃÀàÖеÄËùÓдúÂ룬°üÀ¨Ç¶Ì×ÀàÐÍÖеijõʼÉ趨ֵºÍ´úÂ룬¶¼½«ÑϸñµØ½øÐмÆËã¡£ÑϸñÔ¼ÊøÒâζ×ÅËùÓбí´ïʽµÄ½á¹û¶¼±ØÐëÊÇ IEEE 754 Ëã·¨¶Ô²Ù×÷ÊýÔ¤ÆÚµÄ½á¹û£¬ÒÔµ¥¾«¶ÈºÍË«¾«¶È¸ñʽ±íʾ¡£
¡¡¡¡Èç¹ûÄãÏëÈÃÄãµÄ¸¡µãÔËËã¸ü¼Ó¾«È·£¬¶øÇÒ²»»áÒòΪ²»Í¬µÄÓ²¼þƽ̨ËùÖ´ÐеĽá¹û²»Ò»ÖµĻ°£¬¿ÉÒÔÓùؼü×Östrictfp.
¡¡¡¡Ê¾Àý 1
ÏÂÃæµÄʾÀýÑÝʾÁËÒ»¸öʹÓà strictfp ÐÞÊηûÉùÃ÷µÄÀà¡£
¡¡¡¡
// 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);
¡¡¡¡}
¡¡¡¡}
¡¡
¡¡¡¡Ê¾ÀýÊä³ö
float: 0.6710339
¡¡¡¡double: 0.04150553411984792
¡¡¡¡sum: 0.71253945297742238
¡¡¡¡quotient: 16.1673355
¡¡
¡¡¡¡Ê¾Àý 2
¡¡¡¡ÏÂÃæµÄʾÀýÑÝʾÁËÒ»¸öʹÓà strictfp ÐÞÊηûÉùÃ÷µÄ·½·¨¡£
¡¡¡¡
// 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);
¡¡¡¡}
¡¡¡¡}
¡¡¡¡Ê¾ÀýÊä³ö
¡¡¡¡
float: 0.6710339
¡¡¡¡double: 0.04150553411984792
¡¡¡¡sum: 0.71253945297742238
==============================================================
×ÔJava2ÒÔÀ´£¬JavaÓïÑÔÔö¼ÓÁËÒ»¸ö¹Ø¼ü×Östrictfp¡£strictfpµÄÒâ˼ÊÇFP-strict£¬Ò²¾ÍÊÇ˵¾«È·¸¡µãµÄÒâ˼¡£ÔÚJava ÐéÄâ»ú½øÐи¡µãÔËËãʱ£¬Èç¹ûûÓÐÖ¸¶¨strictfp¹Ø¼ü×Öʱ£¬ JavaµÄ±àÒëÆ÷ÒÔ¼°ÔËÐл·¾³ÔÚ¶Ô¸¡µãÔËËãµÄ±í´ïʽÊDzÉȡһÖÖ½üËÆÓÚÎÒÐÐÎÒËØµÄÐÐΪÀ´Íê³ÉÕâЩ²Ù×÷£¬ÒÔÖÂÓڵõ½µÄ½á¹ûÍùÍùÎÞ·¨ÁîÄãÂúÒâ¡£¶øÒ»µ©Ê¹ÓÃÁËstrictfpÀ´ÉùÃ÷Ò»¸öÀà¡¢½Ó¿Ú»òÕß·½·¨Ê±£¬ÄÇôËùÉùÃ÷µÄ·¶Î§ÄÚJavaµÄ±àÒëÆ÷ÒÔ¼°ÔËÐл·¾³»áÍêÈ«ÒÀÕÕ¸¡µã¹æ·¶IEEE-754À´Ö´ÐС£Òò´ËÈç¹ûÄãÏëÈÃÄãµÄ¸¡µãÔËËã¸ü¼Ó¾«È·£¬¶øÇÒ²»»áÒòΪ²»Í¬µÄÓ²¼þƽ̨ËùÖ´ÐеĽá¹û²»Ò»ÖµĻ°£¬ÄǾÍÇëÓùؼü×Östrictfp¡£
Äã¿ÉÒÔ½«Ò»¸öÀà¡¢½Ó¿ÚÒÔ¼°·½·¨ÉùÃ÷Ϊstrictfp£¬µ«ÊDz»ÔÊÐí¶Ô½Ó¿ÚÖеķ½·¨ÒÔ¼°¹¹Ô캯ÊýÉùÃ÷strictfp¹Ø¼ü×Ö£¬ÀýÈçÏÂÃæµÄ´úÂ룺
1. ºÏ·¨µÄʹÓùؼü×Östrictfp
strictfp interface A {}
public strictfp class FpDemo1 {
strictfp void f() {}
}
2. ´íÎóµÄʹÓ÷½·¨
interface A {
strictfp void f();
}
public class FpDemo2 {
strictfp FpDemo2() {}
}
Ò»µ©Ê¹ÓÃÁ˹ؼü×ÖstrictfpÀ´ÉùÃ÷ij¸öÀà¡¢½Ó¿Ú»òÕß·½·¨Ê±£¬ÄÇôÔÚÕâ¸ö¹Ø¼ü×ÖËùÉùÃ÷µÄ·¶Î§ÄÚËùÓи¡µãÔËËã¶¼ÊǾ«È·µÄ£¬·ûºÏIEEE-754¹æ·¶µÄ¡£ÀýÈçÒ»¸öÀà±»ÉùÃ÷Ϊstrictfp£¬ÄÇô¸ÃÀàÖÐËùÓеķ½·¨¶¼ÊÇstrictfpµÄ¡£
×÷Õߣºzongquanliu