📜  说明浮动用法的Java程序

📅  最后修改于: 2022-05-13 01:54:41.812000             🧑  作者: Mango

说明浮动用法的Java程序

浮点数是那些需要小数精度的数字,即可以在分数中的数字。还有更多涉及浮动类型的数学计算。例如,找到一个数字的平方根,直到某些十进制值,找到该数字的立方根,找到二次方程的根,以及涉及三角函数(如 sin cos tan)的计算。

有两种类型的浮点数据类型

  • 漂浮
  • 双倍的
NameWidthRange
Float321.4e–045 to 3.4e+038
Double644.9e–324 to 1.8e+308

Float :它是一个单精度值,存储空间为 32 位。与双精度相比,这种单精度速度更快,占用的空间也更小。

对于Java变量,我们可以在声明或初始化期望值时使用 float 为小数。 Java中的默认值为0.0f,大小为4字节。 Java中的浮点数可以有负值。

定义Java浮点的正确方法和错误方法。

  1. 浮点 a1=10.57f 等于 10.57
  2. float a2 =10f 等于 10.0
  3. float a3=9.58 会报错。

在这里我们得出结论,如果我们用 float 初始化任何值,它以 f 结尾,那么它是正确的,否则会抛出错误。

打印浮点值:

Java
// Java Program to Illustrate the Usage of Floating
import java.io.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // initialized two float variables with
        // the least and max value of float
 
        float a1 = 1.40129846432481707e-45f;
        float a2 = 3.40282346638528860e+38f;
 
        // printed the value of a1 and a2
        System.out.println("Start range: " + a1);
        System.out.println("End range: " + a2);
    }
}


Java
// Java Program to Illustrate the Usage of Floating
import java.io.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // initialized two float variables a1 and a2.
        // declared n3 which will contain
        // the output of a1 * a2.
 
        float a1 = 10.89f;
        float a2 = 7.43f;
        float a3;
 
        // multiplied n1 and n2 and stored it in a3
        a3 = a1 * a2;
 
        // printed the value of a3
        System.out.println("The result of n1 x n2 is: "
                           + a3);
    }
}


Java
// Java Program to Illustrate the Usage of Floating
import java.io.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // initialized two float variables a1 and a2.
        // a1 has simple value of float type and a2
        // has the equivalent scientific notation.
 
        float a1 = 283.75f;
        float a2 = 2.8375e2f;
 
        // printed the value of a1 and a2
        System.out.println("Simple Float: " + a1);
        System.out.print("Scientific Notation: " + a2);
    }
}



输出
Start range: 1.4E-45
End range: 3.4028235E38

浮点值的乘法:

Java

// Java Program to Illustrate the Usage of Floating
import java.io.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // initialized two float variables a1 and a2.
        // declared n3 which will contain
        // the output of a1 * a2.
 
        float a1 = 10.89f;
        float a2 = 7.43f;
        float a3;
 
        // multiplied n1 and n2 and stored it in a3
        a3 = a1 * a2;
 
        // printed the value of a3
        System.out.println("The result of n1 x n2 is: "
                           + a3);
    }
}


输出
The result of n1 x n2 is: 80.912704

以科学记数法提供价值

Java

// Java Program to Illustrate the Usage of Floating
import java.io.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // initialized two float variables a1 and a2.
        // a1 has simple value of float type and a2
        // has the equivalent scientific notation.
 
        float a1 = 283.75f;
        float a2 = 2.8375e2f;
 
        // printed the value of a1 and a2
        System.out.println("Simple Float: " + a1);
        System.out.print("Scientific Notation: " + a2);
    }
}


输出
Simple Float: 283.75
Scientific Notation: 283.75