fixed floating point calculations in Java
In Java I need some hint to declare floating poin开发者_开发知识库t variable that in all virtual machine run uniquely and show number unique float number in all machine ( mobile machine and PC )
You can use strictfp
keyword for class declaration or variables :
public strictfp class MyFPclass {
// ... contents of class here ...
}
The Java package java.lang.Math class contains these strictfp methods:
public static strictfp double abs(double);
public static strictfp int max(int, int);
public static strictfp long max(long, long);
public static strictfp float max(float, float);
public static strictfp double max(double, double);
public static strictfp int min(int, int);
I can suggest 2 classes for this purpose you can think of
- Math.random()
- java.util.Random class
The methods of the Random class often produce random numbers in a more convenient form, but requires creating an object, which sometimes is inconvenient. In constrast, the Math.random() method produces a double value which must sometimes be translated and cast into the form you need it. It's a tradeoff between the globalness of Math.random more directly useful numbers from the Random class.
精彩评论