V tomto kurzu se pomocí příkladů dozvíme o floatových a dvojitých datových typech. Podíváme se také na některé klíčové rozdíly mezi nimi a na to, kdy je použít.
V C ++ se pro hodnoty s plovoucí desetinnou čárkou používají oba float
i double
datové typy. Čísla s plovoucí desetinnou čárkou se používají pro desítkové a exponenciální hodnoty. Například,
// creating float type variables float num1 = 3.0f; float num2 = 3.5f; float num3 = 3E-5f; // 3x10^-5 // creating double type variables double num4 = 3.0; double num5 = 3.5; double num6 = 3E-5; // 3x10^-5
Musíme přidat příponu f
nebo F
na konec float
hodnoty. Důvodem je, že kompilátor interpretuje desítkové hodnoty bez přípony jako double
.
Zvažte tento kód.
float a = 5.6;
Zde jsme proměnné přiřadili double
hodnotu float
.
V tomto případě je kompilátor automaticky převeden na 5,6,float
než je přiřazen k proměnné a. To může vést ke ztrátě dat. Další informace najdete v převodu typu C ++.
Rozdíl mezi floatem a double
plovák | dvojnásobek |
---|---|
Velikost: 4 bajty | Velikost: 8 bytů |
Přesnost: Obecně přesnost 7 desetinných míst | Přesnost: Obecně přesnost 15 desetinných míst |
Příklad: 3.56f , 3e5f atd. | Příklad: 3.56 , 3e5 atd. |
Poznámka: Pokud nemáte konkrétní požadavek, používejte vždy double
místo float
, protože float
proměnné mohou být náchylné k chybám při práci s velkým počtem.
Příklad 1: C ++ float a double
#include #include using namespace std; int main() ( // Creating a double type variable double a = 3.912348239293; // Creating a float type variable float b = 3.912348239293f; // Printing the two variables cout << "Double Type Number = " << a << endl; cout << "Float Type Number = " << b << endl; return 0; )
Výstup
Double Type Number = 3.91235 Float Type Number = 3.91235
Poznámka: Kompilátor použitý v tomto příkladu (kompilátor MinGW) povolil 6 číslic. Takže naše hodnoty proměnných byly kompilátorem zaokrouhleny a zkráceny na 6 číslic.
setprecision () na Zadat desetinná místa
Můžeme zadat počet desetinných míst tisknout cout
pomocí setprecision()
funkce.
Tato funkce je definována v iomanip
hlavičkovém souboru, který znamená manipulaci se vstupem / výstupem .
Příklad 2: Použití setprecision () pro čísla s plovoucí desetinnou čárkou
#include #include using namespace std; int main() ( // Creating a double type variable double a = 3.912348239293; // Creating a float type variable float b = 3.912348239293f; // Setting the precision to 12 decimal places cout << setprecision(13); // Printing the two variables cout << "Double Type Number = " << a << endl; cout << "Float Type Number = " << b << endl; return 0; )
Výstup
Dvojité číslo typu = 3,912348239293 Číslo typu Float = 3,912348270416
Jak vidíme z výše uvedeného příkladu, zadali jsme přesnost až na 13 číslic.
cout << setprecision(13);
Hodnota s plovoucí desetinnou čárkou, kterou jsme přiřadili našim proměnným, se také skládá z 13 číslic.
Jelikož však float
má přesnost až pouze 7 číslic, po překročení jeho přesnosti zobrazuje hodnoty smetí .
Naše double
proměnná zobrazuje správné číslo, protože má přesnost 15 číslic, zatímco samotné číslo se skládá z 13 číslic.
Alternativně můžeme při tisku zadat různé přesnosti pro různé proměnné.
Příklad 3: Různé přesnosti pro různé proměnné
#include #include using namespace std; int main() ( // Creating a double type variable double a = 3.912348239293; // Creating a float type variable float b = 3.912348239293f; // Setting precision to 11 for double cout << "Double Type Number = " << setprecision(11) << a << endl; // Setting precision to 7 for float cout << "Float Type Number = " << setprecision(7) << b << endl; return 0; )
Výstup
Double Type Number = 3.9123482393 Float Type Number = 3.912348
Z výše uvedeného programu vidíme, že jsme nastavili dvě různé hodnoty přesnosti pro float
a double
.
V obou případech je přesnost menší než skutečné číslice čísla. Poslední číslice je tedy zaokrouhlena a zbytek je zkrácen.
Note: If we specify the precision greater than the precision of the data type itself (7 for float
and 15 for double
), then the compiler will give us garbage values after the precision limit has been exceeded, as can be seen with the float
output in example 2.
Work with Exponential Numbers
As mentioned above, float
and double
can also be used to represent exponential numbers. For example,
// ex = 325 X (10 25) double ex = 325E25;
C++ outputs exponential numbers and very large numbers in a format called the scientific format. The variable ex will be outputted in this format by default since it is a very large number.
In order to force C++ to display our floating-point numbers in the scientific
format regardless of the size of the number, we use the format specifier scientific
inside of cout
.
double num = 3.25; // ex = 325 X (10 25) double ex = 325E25; // using scientific format cout << scientific << num; cout << scientific << ex;
In addition to this, there is another format specifier known as fixed
, which displays floating-point numbers in the decimal format.
It is similar to displaying floating-point numbers by only using cout
without setprecision()
, except for the fact that fixed
displays numbers up to 6 decimal points.
On the other hand, only using cout
displays digits according to the specific compiler (6 total digits in the case of MinGW compiler, including the digits before the decimal point).
Example 4: Fixed and Scientific Formats
#include #include using namespace std; int main() ( // Creating a decimal double type variable double a = 3.912348239293; // Creating an exponential double type variable double ex1 = 325e+2; // Creating a float type variable float b = 3.912348239293f; // Creating an exponential float type variable float ex2 = 325e+2f; // Displaying output with fixed cout << "Displaying Output With fixed:" << endl; cout << "Double Type Number 1 = " << fixed << a << endl; cout << "Double Type Number 2 = " << fixed << ex1 << endl; cout << "Float Type Number 1 = " << fixed << b << endl; cout << "Float Type Number 2 = " << fixed << ex2 << endl; // Displaying output with scientific cout << "Displaying Output With scientific:" << endl; cout << "Double Type Number 1 = " << scientific << a << endl; cout << "Double Type Number 2 = " << scientific << ex1 << endl; cout << "Float Type Number 1 = " << scientific << b << endl; cout << "Float Type Number 2 = " << scientific << ex2 << endl; return 0; )
Output
Displaying Output With fixed: Double Type Number 1 = 3.912348 Double Type Number 2 = 32500.000000 Float Type Number 1 = 3.912348 Float Type Number 2 = 32500.000000 Displaying Output With scientific: Double Type Number 1 = 3.912348e+000 Double Type Number 2 = 3.250000e+004 Float Type Number 1 = 3.912348e+000 Float Type Number 2 = 3.250000e+004
long double
Apart from float
and double
, there is another data type that can store floating-point numbers. This is known as long double
.
It usually occupies a space of 12 bytes (depends on the computer system in use), and its precision is at least the same as double
, though most of the time, it is greater than that of double
.
long double
values should end with L
. For example,
// declaring a long double variable long double num_ldb = 2.569L;
Poznámka: Datové typy s plovoucí desetinnou čárkou podporované C ++ jsou float
, double
a long double
. Neexistuje žádný long float
.