float数据类型是单精度32位IEEE 754浮点数,double数据类型是双精度64位IEEE 754浮点数。

这是什么意思?什么时候应该使用float而不是double,反之亦然?


当前回答

Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. For example, calculations such as square root, or transcendentals such as sine and cosine, result in a value whose precision requires a floating-point type. Java implements the standard (IEEE–754) set of floatingpoint types and operators. There are two kinds of floating-point types, float and double, which represent single- and double-precision numbers, respectively. Their width and ranges are shown here:

   Name     Width in Bits   Range 
    double  64              1 .7e–308 to 1.7e+308
    float   32              3 .4e–038 to 3.4e+038

浮动

float类型指定使用32位存储的单精度值。单精度在某些处理器上更快,占用的空间是双精度的一半,但是当值非常大或非常小时就会变得不精确。float类型的变量在需要小数分量时非常有用,但不需要很高的精度。

下面是一些浮动变量声明的例子:

浮动高温度,低温度;

Double precision(由Double关键字表示)使用64位来存储值。在一些为高速数学计算而优化的现代处理器上,双精度实际上比单精度更快。所有先验数学函数,如sin()、cos()和sqrt(),都返回双值。当您需要在多次迭代计算中保持精度时,或者需要处理大数值时,double是最好的选择。

其他回答

你应该使用double而不是float进行精确的计算,使用float而不是double进行不精确的计算。Float只包含十进制数,但double包含一个IEEE754双精度浮点数,使其更容易包含和更准确地计算数字。希望这能有所帮助。

这个例子演示了如何从Java中的浮点数中提取符号(最左边的位)、指数(后面的8位)和尾数(最右边的23位)。

int bits = Float.floatToIntBits(-0.005f);
int sign = bits >>> 31;
int exp = (bits >>> 23 & ((1 << 8) - 1)) - ((1 << 7) - 1);
int mantissa = bits & ((1 << 23) - 1);
System.out.println(sign + " " + exp + " " + mantissa + " " +
  Float.intBitsToFloat((sign << 31) | (exp + ((1 << 7) - 1)) << 23 | mantissa));

同样的方法可以用于double(11位指数和52位尾数)。

long bits = Double.doubleToLongBits(-0.005);
long sign = bits >>> 63;
long exp = (bits >>> 52 & ((1 << 11) - 1)) - ((1 << 10) - 1);
long mantissa = bits & ((1L << 52) - 1);
System.out.println(sign + " " + exp + " " + mantissa + " " +
  Double.longBitsToDouble((sign << 63) | (exp + ((1 << 10) - 1)) << 52 | mantissa));

来源:http://s-j.github.io/java-float/

这将给出错误:

public class MyClass {
    public static void main(String args[]) {
        float a = 0.5;
    }
}

/MyClass.java:3:错误:不兼容的类型:从double类型到float类型的可能有损转换 浮动a = 0.5;

这将工作得非常好

public class MyClass {
    public static void main(String args[]) {
        double a = 0.5;
    }
}

这也会工作得很好

public class MyClass {
    public static void main(String args[]) {
        float a = (float)0.5;
    }
}

原因:Java默认将实数存储为double,以确保更高的精度。

在计算过程中,Double占用更多空间但更精确,float占用更少空间但更精确。

在常规编程计算中,我们不使用浮点数。如果我们确保结果范围在浮点数据类型的范围内,那么我们可以选择一个浮点数据类型来节省内存。一般来说,我们使用double的原因有两个:-

If we want to use the floating-point number as float data type then method caller must explicitly suffix F or f, because by default every floating-point number is treated as double. It increases the burden to the programmer. If we use a floating-point number as double data type then we don’t need to add any suffix. Float is a single-precision data type means it occupies 4 bytes. Hence in large computations, we will not get a complete result. If we choose double data type, it occupies 8 bytes and we will get complete results.

float和double数据类型都是专门为科学计算设计的,在科学计算中,近似误差是可以接受的。如果精度是最优先考虑的问题,那么建议使用BigDecimal类而不是float或double数据类型。Java中的浮点和双数据类型

尽管如此,Java似乎倾向于使用double进行计算:

以我今天早些时候写的程序为例,当我使用float时,这些方法不起作用,但现在当我用double替换float时工作得很好(在NetBeans IDE中):

package palettedos;
import java.util.*;

class Palettedos{
    private static Scanner Z = new Scanner(System.in);
    public static final double pi = 3.142;

    public static void main(String[]args){
        Palettedos A = new Palettedos();
        System.out.println("Enter the base and height of the triangle respectively");
        int base = Z.nextInt();
        int height = Z.nextInt();
        System.out.println("Enter the radius of the circle");
        int radius = Z.nextInt();
        System.out.println("Enter the length of the square");
        long length = Z.nextInt();
        double tArea = A.calculateArea(base, height);
        double cArea = A.calculateArea(radius);
        long sqArea = A.calculateArea(length);
        System.out.println("The area of the triangle is\t" + tArea);
        System.out.println("The area of the circle is\t" + cArea);
        System.out.println("The area of the square is\t" + sqArea);
    }

    double calculateArea(int base, int height){
        double triArea = 0.5*base*height;
        return triArea;
    }

    double calculateArea(int radius){
        double circArea = pi*radius*radius;
        return circArea;
    }

    long calculateArea(long length){
        long squaArea = length*length;
        return squaArea;
    }
}