Double
As you saw in the previous task, float
's precision (7 decimal digits) is not always enough. But C++ has a solution: double
. With a precision of 15 decimal digits, it is much more accurate than float
. The syntax of using double is the same as always:
By default, cout
prints floating-point numbers with a precision of 6 significant digits. That way, you won't see a difference between float
and double
. To set a larger precision, we can use std::cout << std::setprecision(15);
again (this will set the precision to 15 significant digits).
Note
Don't forget to include
<iomanip>
to be able to usestd::setprecision()
main.cpp
How is better precision achieved in <code class="go3679463865">double</code>?
It just takes up more memory. A float
type takes up 4 bytes, and a double
takes up 8 bytes of memory.
There also is a long double
type. Its size ranges from 8 bytes (just like a double) to 16 bytes. You can check the size of the long double
on your system by using the following code:
Additionally, you should add L
to a value you are assigning (otherwise, the value will be assigned with the precision of double
). Look at the example:
Here is a little summary:
Data Type | Range | Precision |
---|---|---|
float |
±1.2e-38 to ±3.4e+38 | 7 decimal digits |
double |
±2.2e-308 to ±1.8e+308 | 15 decimal digits |
long double |
depends on a system but huge | from 15 to 33 depending on a system |
Tarea
You need to calculate (1/3)² using different data types. For that, assign the value of 1/3 to the float
, double
, and long double
typed variables.
Then print those numbers multiplied by themselves, for example:
Steps:
- Initialize a
float
variable with the value1/3.
; - Initialize a
double
variable with the value1/3.
; - Initialize a
long double
variable with the value1/3.L
.
note:long double
takes up 16 bytes of memory on our system - Set the precision of output to 25 decimals with
setprecision(25)
; - Print all those values multiplied by themselves.
Note
If you got zeros in the output, make sure you assigned the values exactly like shown above (
1/3.
,1/3.L
).
We will discuss the reasons for this behavior in the Arithmetic Operations chapter.
¿Todo estuvo claro?
Contenido del Curso
C++ Data Types
2. Numerical Data Types
4. Other Data Types and Concepts
C++ Data Types
Double
As you saw in the previous task, float
's precision (7 decimal digits) is not always enough. But C++ has a solution: double
. With a precision of 15 decimal digits, it is much more accurate than float
. The syntax of using double is the same as always:
By default, cout
prints floating-point numbers with a precision of 6 significant digits. That way, you won't see a difference between float
and double
. To set a larger precision, we can use std::cout << std::setprecision(15);
again (this will set the precision to 15 significant digits).
Note
Don't forget to include
<iomanip>
to be able to usestd::setprecision()
main.cpp
How is better precision achieved in <code class="go3679463865">double</code>?
It just takes up more memory. A float
type takes up 4 bytes, and a double
takes up 8 bytes of memory.
There also is a long double
type. Its size ranges from 8 bytes (just like a double) to 16 bytes. You can check the size of the long double
on your system by using the following code:
Additionally, you should add L
to a value you are assigning (otherwise, the value will be assigned with the precision of double
). Look at the example:
Here is a little summary:
Data Type | Range | Precision |
---|---|---|
float |
±1.2e-38 to ±3.4e+38 | 7 decimal digits |
double |
±2.2e-308 to ±1.8e+308 | 15 decimal digits |
long double |
depends on a system but huge | from 15 to 33 depending on a system |
Tarea
You need to calculate (1/3)² using different data types. For that, assign the value of 1/3 to the float
, double
, and long double
typed variables.
Then print those numbers multiplied by themselves, for example:
Steps:
- Initialize a
float
variable with the value1/3.
; - Initialize a
double
variable with the value1/3.
; - Initialize a
long double
variable with the value1/3.L
.
note:long double
takes up 16 bytes of memory on our system - Set the precision of output to 25 decimals with
setprecision(25)
; - Print all those values multiplied by themselves.
Note
If you got zeros in the output, make sure you assigned the values exactly like shown above (
1/3.
,1/3.L
).
We will discuss the reasons for this behavior in the Arithmetic Operations chapter.
¿Todo estuvo claro?