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

Méthode Java Float classe floatToRawIntBits() avec exemple

Méthode floatToRawIntBits() de la classe flottante

  • méthode floatToRawIntBits() est disponible dans le package java.lang.
  • méthode floatToRawIntBits() suit les normes à virgule flottante simple précision IEEE 754 et selon les normes, il renvoie les bits qui dénotent la valeur à virgule flottante tout en préservant la valeur NaN.
  • méthode floatToRawIntBits() 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 floatToRawIntBits() ne lève pas d'exception au moment de représenter les bits tout en préservant NaN (Pas un nombre).

Syntaxe :

    public static int floatToRawIntBits(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 les valeurs NaN réelles (c'est-à-dire qu'il ne réduit pas tous les bits codant un NaN en un "basique" valeur NaN).

Exemple :

// Java program to demonstrate the example 
// of floatToRawIntBits (float f)
// method of Float class

public class FloatToRawIntBits {
    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.floatToRawIntBits(value1)
        int result1 = Float.floatToRawIntBits(value1);

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

        // Display result1,result2 values
        System.out.println("Float.floatToRawIntBits(value1): " + result1);
        System.out.println("Float.floatToRawIntBits(value2): " + result2);
        System.out.print("Float.floatToRawIntBits(NaN): ");
        System.out.println(Float.floatToRawIntBits(5.0f % 0f));
    }
}

Sortie

value1: 18.2
value2: 19.2
Float.floatToRawIntBits(value1): 1100061082
Float.floatToRawIntBits(value2): 1100585370
Float.floatToRawIntBits(NaN): 2143289344

Balise Java