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

Méthode intBitsToFloat() de la classe Java Float avec exemple

Méthode intBitsToFloat() de la classe flottante

  • méthode intBitsToFloat() est disponible dans le package java.lang.
  • méthode intBitsToFloat() suit les normes à virgule flottante IEEE 754 et selon les normes, il renvoie la valeur flottante correspondant à un argument donné qui dénote la représentation de bits entiers.
  • méthode intBitsToFloat() 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 intBitsToFloat() ne lève pas d'exception au moment de la conversion des représentations de bits en valeur flottante.

Syntaxe :

    public static float intBitsToFloat(int bits_rep);

Paramètre(s) :

  • int bits_rep – représente la valeur entière en bits.

Valeur renvoyée :

Le type de retour de cette méthode est float, elle renvoie la valeur float qui représente l'argument donné en bits entiers.

  • Si nous passons "0x7f800000" , il renvoie la valeur "infini positif" .
  • Si nous passons "0xff800000" , il renvoie la valeur "infini négatif" .
  • Si la valeur se situe entre "0x7f800001" et "0x7ffffff" ou la valeur se situe entre "0xff800001" et "0xffffffff" .

Exemple :

// Java program to demonstrate the example 
// of intBitsToFloat (int bits_rep)
// method of Float class

public class IntBitsToFloatOfFloatClass {
    public static void main(String[] args) {
        // Variables initialization
        int value1 = 20;
        int value2 = 0x7f800000;
        int value3 = 0xff800000;

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


        // It returns the float value denoted by the given 
        // bit representation by calling Float.intBitsToFloat(value1)
        float result1 = Float.intBitsToFloat(value1);

        // It returns the float value denoted by the given 
        // bit representation by calling Float.intBitsToFloat(value2)
        float result2 = Float.intBitsToFloat(value2);

        // It returns the float value denoted by the given 
        // bit representation by calling Float.intBitsToFloat(value3)
        float result3 = Float.intBitsToFloat(value3);

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

Sortie

value1: 20
value2: 2139095040
value3: -8388608
Float.intBitsToFloat(value1): 2.8E-44
Float.intBitsToFloat(value2): Infinity
Float.intBitsToFloat(value3): -Infinity

Balise Java