I am learning Java, and am confuse on when to choose double or float for my real numbers or int. It feels like, it doesn’t matter because from my limited experience (with Java) both of them deliver the same results, but I don’t want to go further down the learning curve with Java and have a bad habit of using either messing up my code, and not having a clue as to why. So, when should you use float and double?
Hello, I'm currently making a small matrix library in java, specifically for use with neural networks. So far I've done nearly all the functions and I used floats.
My question is if it would be better to use floats or doubles for this task, neural networks need precise values but are also very resource heavy.
Floats, doubles or write everything twice for overloading. Any ideas?
EDIT: Thanks for the answers, i finished version 1.0 today. If anyone wants to check it out feel free, just go here, hopefully it works well.
I would assume that since floats require less memory it would be the better choice. My java teacher always says use a double though.
From my textbook:
When you write a floating-point literal in your program code, Java assumes it to be of the double data type. A double value is not compatible with a float variable because a double can be much larger or smaller than the allowable range for a float. As a result, code such as the following will cause an error:
Example 1:
float number; number = 23.5; //Error!
You can force a double to be treated as a float, by suffixing it with the letter F or f. The preceding code can be rewritten in the following manner to prevent an error:
Example 2:
float number; number = 23.5F; //This will work. ===================================================
I can't figure out why example 1 produces an error. Why is 23.5 considered a double? I thought it was a float.
AFAIK, float numbers are precise up to 7 decimal digits of accuracy. 23.5 only has 1 decimal digit. What am I not understanding? I thought by typing 'float number;' you are declaring the number variable to be a float. Why would you need the extra step of adding an F?
Thanks
Why would someone even use an int or a float?
I ask this because while int does use less data, you risk overflows and also in general a double is more accurate. How significant is the amount of data saved actually that one would choose that over a double?
How significant is the amount of data saved actually that one would choose that over a double?
While i think that one of the most important points for using an int is really that it is an integer, i.e. pure with no decimals (as others have pointed out), i also wanted to address this question.
A double is twice as big as an int or float. This will not be an issue in a small program you write for a class and it won't even be an issue in many industry-level applications, but there are certainly times where doubling the amount of data that you have to transmit for no gain whatsoever (if you don't happen to need the added precision of a double) can have a huge impact. At work we often deal with the transmission of tens or hundreds of gigabytes of data. Of course, this number wouldn't be exactly twice as large if we used doubles instead of wherever possible, but it would certainly grow a substantial amount, which would certainly influence transmission times noticeably.
It is certainly fine to generally worry more about things like code readability than performance or space requirements, but there are applications where you do need to worry about size. And i don't think getting into the habit of using double "just because" is a good thing.
Most of what is said is only half correct. Remember that int/float and other primitive are represented using x amount of bits. In the days that java started off with embedded systems with limited memory thus you had to constraint how much memory your program could use to do operations and how much memory it needed to use overall. This is where good use of primitive types comes in.
I'm in the throws of trying to learn Java and am very much a beginner. So, I apologise if this is a daft question. tbh it would probably help if you could explain like I'm a five year old who is very slow on the uptake.
The Wikipedia page on it is a good place to start.
To sum up:
floatis 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).doubleis 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.
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.