Fixed floating point computation in Java Baeldung

In Java, I need a little hint to declare a floating point variable that works unambiguously on all virtual machines and shows the unique floating point number on all machines (mobile and PC).

+2


a source to share


2 answers


You can use a keyword strictfp

to declare a class 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);

      

0


a source


I can suggest 2 classes for this purpose that 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 require object creation, which is sometimes inconvenient. In constrast, the Math.random () method creates a double value that sometimes needs to be translated and entered in the form you need. This is a trade-off between the globality of Math.random and more directly useful numbers from the Random class.

0


a source







All Articles