+ 1
Can someone please explain what a float, double and long double is? Please also relate this to the number of bytes they take up.
Thank you for your answer.
15 RĂ©ponses
+ 3
Happy to help. đ
+ 2
Courtesy of Google:
Floating point types
float - single precision floating point type. Usually IEEE-754 32 bit floating point type
double - double precision floating point type. Usually IEEE-754 64 bit floating point type
long double - extended precision floating point type. Does not necessarily map to types mandated by IEEE-754. Usually 80-bit x87 floating point type on x86 and x86-64 architectures.
Source: https://en.cppreference.com/w/cpp/language/types
+ 2
float is a single-precision number,which is typically 4 bytes long. It can take values from -3.4 x 10^38 to 3.4 x 10^38.
Double is a double-precision number, which is typically 8 bytes long. It cab take values from -1.7 x 10^308 to 1.7 x 10^308.
It true for most part of up-to-date compilers.
Long double is more compiler dependent type. It can be treated the same as double or it can take 10 or 16 bytes long with larger range.
+ 2
I'm not sure I understand the question. As the programmer, it's generally best practice to tell the software what you want the type to be, not let the program decide. Otherwise you're relying on everyone involved in the project to remember what the default behavior for the language is and other potential issues down the road. My opinion. đ
+ 2
Michael, you can use any floating type for decimal values. But it is common to use double type for it. Don't think that your task requires long double. Using float was common in past when RAM was too small. Now you can meet this type in graphics presentation (for example, video card usually uses it, where great precision is not needed). Double is usually used for math calculations. May be long double is used for complex science calculations. Performance of calculations with this data types depends on your hardware, e.g. what types are implemented in your CPU
+ 2
Yeah, I was trying to look for a happy medium explanation of this stuff, but it's just very computer science heavy. The C++ tutorial has a simple explanation for some of it on a few of its slides ( https://www.sololearn.com/learn/CPlusPlus/1622/?ref=app ), but it does get confusing when it comes to "precision" in general science vs "precision" in computer science. I can't see why adding the word "precision" in computer science to "single" or "double" helps more than it confuses as it appears to only mean the amount of space in memory it takes up (i.e. the 4 bytes for a single or double that for a double).
Maybe someone more computer-sciency will be able to offer a better answer here.
+ 1
The number of decimal places.
Let's take the integer 7.
Single precision = 7.0
Double precision = 7.00
They could all be equal, or they could be rounded.
+ 1
Janningâ Ohhh ok. Thanks for your reply. But say i wanted to store the decimals 4.1721... Will i use a long double as my data type?
+ 1
Janningâ Nice opinion lol. I never saw that perspective and i thank you for showing me!
+ 1
Also you can check memory usage of different data types by yourself if you are interested using the following command:
cout << sizeof(<data type name>);
0
Janningâ Can i ask what a precision is? sorry i am a beginner in c++ and canât understanding some technical language.
0
Yes.
0
Janningâ So are decimals like 2.1 restricted in the data the data type of double or long double?
0
JFYI if you want to store 5.41 as floating number in memory, it can be stored like 5.40999... Rounding mistake always has place. It doesn't matter in custom program, but it does for economical calculations, where each cent is important. To avoid this BCD numbers are used. It is data type with fixed precision. It will store values in memory in different way that floating numbers do. Floating point numbers are stored with sign, exponent and mantissa. BCD numbers are stored with there decimal representation and precision, that is why they won't have rounding mistake.
If your number is going out of data type range, you will get incorrect result using usual data types. For that purposes multiple precision data types are used (e.g. GMP lib and its mpz_class type, but I used it only for integers, can't be sure that it can be used with floating point numbers). It stores one value in several variables if I remember it correct, having overflow flag in memory as well.
0
So there are many data types except usual float, double, etc.
Each type is used for task they were implented for