Java >> Tutoriel Java >  >> Tag >> class

Méthode Java Float classe floatToIntBits() avec exemple

Méthode floatToIntBits() de la classe flottante

  • méthode floatToIntBits() est disponible dans le package java.lang.
  • méthode floatToIntBits() suit les normes à virgule flottante IEEE 754 et selon les normes, il renvoie la représentation des bits qui indique la valeur à virgule flottante.
  • méthode floatToIntBits() est une méthode statique, elle est également accessible avec le nom de la classe et si nous essayons d'accéder à la méthode avec l'objet de la classe, nous n'obtiendrons pas non plus d'erreur.
  • méthode floatToIntBits() ne lève pas d'exception au moment de représenter les bits.

Syntaxe :

    public static int floatToIntBits(float f);

Paramètre(s) :

  • float f - représente la valeur à virgule flottante simple précision.

Valeur renvoyée :

Le type de retour de cette méthode est float, elle retourne les bits qui représentent la valeur à virgule flottante simple précision.

  • Si nous passons "l'infini positif" , il renvoie la valeur "0x7f800000" .
  • Si nous passons "l'infini négatif" , il renvoie la valeur "0xff800000" .
  • Si nous passons "NaN" , il renvoie la valeur "0x7fc00000" .

Exemple :

// Java program to demonstrate the example 
// of floatToIntBits (float value)
// method of Float class

public class FloatToIntBitsOfFloatClass {
    public static void main(String[] args) {
        // Variables initialization
        float value1 = 18.20f;
        float value2 = 19.20f;

        // Display value1,value2 values
        System.out.println("value1: " + value1);
        System.out.println("value2: " + value2);

        // It returns the bits denoted by the single 
        // precision floating-point argument by calling 
        // Float.floatToIntBits(value1)
        int result1 = Float.floatToIntBits(value1);

        // It returns the bits denoted by the single 
        // precision floating-point argument by calling 
        // Float.floatToIntBits(value2)
        int result2 = Float.floatToIntBits(value2);

        // Display result1,result2 values
        System.out.println("Float.floatToIntBits(value1): " + result1);
        System.out.println("Float.floatToIntBits(value2): " + result2);
    }
}

Sortie

value1: 18.2
value2: 19.2
Float.floatToIntBits(value1): 1100061082
Float.floatToIntBits(value2): 1100585370

Balise Java