259

The float data type is a single-precision 32-bit IEEE 754 floating point and the double data type is a double-precision 64-bit IEEE 754 floating point.

What does it mean? And when should I use float instead of double or vice-versa?

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
asked Dec 22, 2014 at 7:11
8
  • 10
    You should use floats instead of double when memory usage is critical. If you need more precise computations, use doubles. Commented Dec 22, 2014 at 7:16
  • 17
    @Everv0id: I'm not sure of any situation in which memory was so tight that one had to sacrifice accuracy for space. (You're using Java, for goodness' sake...) There may be some situations when it's called for, but in my practice I've seen it very seldomly. If you wanted to elaborate on why you believe this is a good idea, providing an answer with a for-instance would be a worthy addition. Commented Dec 22, 2014 at 7:21
  • 7
    @Makoto actually, I've never used floats, only doubles. But there could be applications (in theory) which should keep large amounts of floating point numbers, so 2x memory usage could be critical. In theory, ofc; in practice you always can buy yet another server. Commented Dec 22, 2014 at 7:37
  • 3
    I have used 4 byte and even 2 byte fixed precision numbers to save memory, but unless you have billions of these it is unlikely to be worth it. The time it takes you to write "double" instead of "float" (it has one more letter) is worth 1000x more than the extra memory you use, but if using double rather than float saves you from a precision related bug, it is worth it. Commented Dec 22, 2014 at 10:53
  • 1
    An example of when to use floats: When using ADC data acquisition, you know the precision of your data and if you have lots of data, you will save a lot of space in your database. It could make sense using floats in such situations. Commented Nov 27, 2017 at 9:01

9 Answers 9

301

The Wikipedia page on it is a good place to start.

To sum up:

  • float is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the significand (or what follows from a scientific-notation number: 2.33728*1012; 33728 is the significand).

  • double is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of significand.

By default, Java uses double to represent its floating-point numerals (so a literal 3.14 is typed double). It's also the data type that will give you a much larger number range, so I would strongly encourage its use over float.

There may be certain libraries that actually force your usage of float, but in general - unless you can guarantee that your result will be small enough to fit in float's prescribed range, then it's best to opt with double.

If you require accuracy - for instance, you can't have a decimal value that is inaccurate (like 1/10 + 2/10), or you're doing anything with currency (for example, representing 10ドル.33 in the system), then use a BigDecimal, which can support an arbitrary amount of precision and handle situations like that elegantly.

answered Dec 22, 2014 at 7:17
Sign up to request clarification or add additional context in comments.

14 Comments

Is not 233728 == mantissa in given example? I mean, where else is the integer part stored?
I'd also like to know. It seems like 233728 should be the mantissa.
I was searching info on floats and doubles and found this and needed to comment: if you're doing anything with currency that doesn't involve fractional cents, using BigDecimal is ridiculous. Common currency is discrete data, so you should be using an integer data type. (This is one of the more common errors made by young programmers--since we use a . to separate dollars from cents, they think it's a floating point value. It isn't.)
@TrixieWolf, could you be more specific, did you propose to use two integers (integer and decimal parts) ? And you are talking about Common currency, what about the rest ? Some amount are evaluated with 6 decimal so you can't simply *100. Please, you have a point here but could you be more precise :)
@AxelH Except for the middles of financial calculations where fractional cents can exist, money is always discrete. You would use one integer type to store the data. So 5ドル.34 would be stored as 534. The dollar portion is val/100 in integer math, and the cents are val%100 in integer math, where % refers to the remainder operation. For money with more places past the decimal it should still be stored as integral because it's discrete. Even if it isn't discrete, often you'll want to back off to a discrete storage most of the time because it's precise so you won't lose money to rounding errors.
|
83

A float gives you approx. 6-7 decimal digits precision while a double gives you approx. 15-16. Also the range of numbers is larger for double.

A double needs 8 bytes of storage space while a float needs just 4 bytes.

answered Dec 22, 2014 at 7:16

5 Comments

float give you 8 digits precision. The result of System.out.println(Float.parseFloat("0.123456789")); -> 0.12345679
@gshock try also System.out.println(Float.parseFloat("0.987654321")); which gives you 0.9876543
Ok. I found an article that explained it very well arshadsuraj.medium.com/…. Thanks ✌️
This precision is strongly depends on how many mathematical operations (like addition) you execute over numbers (float or double). With each operation there is some error (in mathematical sense). These errors accumulate. After like 100.000 additions for float the actual precision is like 3 digits and for long it's like 8 digits.
@KrzysztofTomaszewski don't mix up precision and accuracy. Rounding errors influence the accuracy, You can loose almost all accuracy even with a single operation if you subtract two nearly equal quantities.
16

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

The type float specifies a single-precision value that uses 32 bits of storage. Single precision is faster on some processors and takes half as much space as double precision, but will become imprecise when the values are either very large or very small. Variables of type float are useful when you need a fractional component, but don't require a large degree of precision.

Here are some example float variable declarations:

float hightemp, lowtemp;


double

Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values. When you need to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers, double is the best choice.

reducing activity
2,3974 gold badges45 silver badges75 bronze badges
answered Dec 22, 2014 at 7:18

3 Comments

This answer for clearly clarified when we should use float and double.why not?
Neither float nor double types are best used for currency in Java, because they open the opportunity for rounding errors. This article goes into more detail: javapractices.com/topic/TopicAction.do?Id=13
" float can be useful when representing dollars and cents." - no, no, no, nononono. Never, ever store currency as floats/doubles.
6

This will give error:

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

/MyClass.java:3: error: incompatible types: possible lossy conversion from double to float float a = 0.5;

This will work perfectly fine

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

This will also work perfectly fine

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

Reason : Java by default stores real numbers as double to ensure higher precision.

Double takes more space but more precise during computation and float takes less space but less precise.

answered Jul 29, 2018 at 13:25

2 Comments

float a = 0.5f; You can also add only (f) this will also work fine.
"by default stores real numbers as double" - is not 100% correct - the reason is that, by specification , 0.5 is a double literal (0.5f is a float) - and there is no assignment conversion from double to float allowed in 5.2. Assignment Contexts (like for example in byte b = 123)
3

Java seems to have a bias towards using double for computations nonetheless:

Case in point the program I wrote earlier today, the methods didn't work when I used float, but now work great when I substituted float with double (in the 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;
 }
}
Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Feb 21, 2016 at 15:45

3 Comments

I had the same problem today. What can be the reason behind this bias?
@Sachi - The "bias" is there because double gives more accurate answers than float. The Java language designers knew this ... and designed the language accordingly with a bias towards double; e.g. in floating-point literal syntax. But they also recognized that there were situations where ~13 decimal digits of precision is not needed, so they included float as well as double.
And for why double gives more accurate results ... read other answers, and study the mathematics of computer floating point arithmetic, aka "numerical analysis".
1

According to the IEEE standards, float is a 32 bit representation of a real number while double is a 64 bit representation.

In Java programs we normally mostly see the use of double data type. It's just to avoid overflows as the range of numbers that can be accommodated using the double data type is more that the range when float is used.

Also when high precision is required, the use of double is encouraged. Few library methods that were implemented a long time ago still requires the use of float data type as a must (that is only because it was implemented using float, nothing else!).

But if you are certain that your program requires small numbers and an overflow won't occur with your use of float, then the use of float will largely improve your space complexity as floats require half the memory as required by double.

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Jun 14, 2015 at 23:25

Comments

0

In regular programming calculations, we don’t use float. If we ensure that the result range is within the range of float data type then we can choose a float data type for saving memory. Generally, we use double because of two reasons:-

  • 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.

Both float and double data types were designed especially for scientific calculations, where approximation errors are acceptable. If accuracy is the most prior concern then, it is recommended to use BigDecimal class instead of float or double data types. Source:- Float and double datatypes in Java

answered Jun 27, 2020 at 0:55

Comments

0

Please note that the actual precision in sense of decimal places for float and long depends on how many mathematical operations were executed. With each mathematical operation (like addition) a small error (discrepancy between a value of float/double and a real result of a computation) appears. They accumulate over time. Consider this simple Java code:

final int count = 1000000;
double x = 0.0f;
final double delta = 1.0 / count;
for (int i = 0; i < count; ++i) {
 x += delta;
}
System.out.println(x + ", delta: " + delta);

On my JVM the output was 1.000000000007918, delta: 1.0E-6. This means double calculations were correct up to 10 decimal places.

Now let's replace double by float. I've got output of 1.0090389, delta: 1.0E-6. This means float calculations were correct up to 1 decimal place!

This happens after 1 mln of additions. When you change "count" to 1000 then you get:

  • actual precision on double: 14 decimal places
  • actual precision on float: 4 decimal places
answered Jan 21, 2025 at 16:01

Comments

-1

You should use double instead of float for precise calculations, and float instead of double when using less accurate calculations. Float contains only decimal numbers, but double contains an IEEE754 double-precision floating point number, making it easier to contain and computate numbers more accurately. Hope this helps.

answered Mar 10, 2020 at 2:00

1 Comment

"Float contains only decimal numbers" - you're wrong. This is not the case.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.