[ Pobierz całość w formacie PDF ]
.When a method is marked strictfp, all floating-point operations within it are done exactly as the original JVMspecification required.If strictfp is not present, the JVM may use its discretion for implementing floating-pointarithmetic.You may also mark the class strictfp, which is equivalent to marking each method.Strictnessapplies to both float and double computations; int and long operations are not affected.You may not use strictfp to get extra floating-point precision.At various points, the JVM implementation is stillrequired to round numbers off to the standard precision.The rules for when this happens are somewhat arcaneand of interest mostly to JVM implementers.As a JVM programmer, these rules mean that you must not assumeany more bits of precision than are guaranteed by the JVM specification.Programs produced with compilers prior to the Java 2 platform do not have the strictfp bit set.All pre-Java 2implementations always do strict arithmetic.However, a program compiled with a Java 1.0 or Java 1.1 compilermay not yield precisely the same results on a Java 2 platform JVM.Some applications require that you get the same results every time you run the program.For example, scientificexperiments are often sensitive to small differences between strict and nonstrict floating-point arithmetic.If yourequire the same results no matter what JVM you run on, you should use the strictfp keyword and you shouldnot use any methods that are not marked strictfp.In particular, the java.lang.Math libraries are not writtenwith strictfp, so you should be very careful.Using strictfp may slow a program down somewhat, since the JVM implementation may have to emulate strictfloating-point arithmetic, which is slower than using the native floating-point instructions.By defaulting tononstrict arithmetic, the JVM favors performance over exactness, but the strictfp keyword allows you to regainexactness.Ru-Brdfile:///D|/Convert/tmp/0201309726_ch03lev1sec8.html (6 of 6) [10/20/2003 12:24:56 AM] file:///D|/Convert/tmp/0201309726_ch03lev1sec9.htmlRu-Brd3.9 Type ConversionsSince most of the arithmetic operations require two arguments of the same type, you will sometimes have toconvert numbers of one kind into numbers of another.In Java, a conversion is written with syntax identical to acast.For example,String o = (String) x; // This is a castint i = (int) 7.9; // This is a conversionCasts and conversions are actually very different operations.Casting alters the way you are allowed to treat anobject without altering the object itself.Conversions have a real effect on the value on the top of the stack.Someconversions may lose information (for example, there's no int that is exactly equivalent to the float 2.71828),but the JVM does its best.See section 4.6 for more about casting.The internal representation of the float 2 is different from that of the int 2.Although floats have a largerrange and can store numbers with a nonintegral component, with large integer values they have to approximate.Take this Java program:int i = 2147483647;float f = i;int i2 = (int) f;System.out.println("i = " + i);System.out.println("f = " + f);System.out.println("i2 = " + i2);The result isi = 2147483647f = 2.14748365E9i2 =  2147483648By taking i and converting it to a float, a little information was lost; the difference is about one part in a billion.When you convert back, that difference is enough to cause the value to tick over into negative territory, with theleftmost bit set.This is an extreme case, but it gives you the idea that converting between ints and floats ismore than just a matter of what you call the number.Conversions can affect the height of the stack.The f2d instruction converts a float to a double, whichincreases the stack height by 1.Converting it back reduces the stack size by 1.You can convert any numerical type to any other numerical type with a single instruction that converts the top ofthe stack into some other element.The naming convention for these instructions is x2y, where x is the mnemonicfor the type you're converting from and y is the type you're converting to.Table 3.8 summarizes theseconversions.3.9.1 Conversion Examplefile:///D|/Convert/tmp/0201309726_ch03lev1sec9.html (1 of 3) [10/20/2003 12:24:58 AM] file:///D|/Convert/tmp/0201309726_ch03lev1sec9.htmlFollowing is an example of conversions:ldc 1234567890123456789L ; Push a very long numberl2d ; As a double: 1.23456789012345677e+18d2l ; Back to long: 1234567890123456768; Some bits were lost on the rightl2i ; As an int: 2112454912; Lots of bits were lost on the lefti2f ; As a float: 2.11245e+9f2l ; Back to long: 2112454912Table 3.8 [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • blondiii.htw.pl
  •