Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Double-Precision Numbers | Numerical Data Types
C++ Data Types
course content

Contenido del Curso

C++ Data Types

C++ Data Types

1. Introduction
2. Numerical Data Types
3. Text Data Type
4. Other Data Types and Concepts

bookDouble-Precision Numbers

The precision of float (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:

double

double

copy
1
double num = 3.14159265359;

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

Don't forget to include <iomanip> to be able to use std::setprecision().

cpp

main

copy
123456789101112131415
#include <iostream> #include <iomanip> int main() { float pi_float = 3.14159265358979; double pi_double = 3.14159265358979; std::cout << "float pi: " << pi_float << std::endl; std::cout << "double pi: " << pi_double << std::endl; std::cout << std::setprecision(15); std::cout << "float pi with setprecision: " << pi_float << std::endl; std::cout << "double pi with setprecision: " << pi_double << std::endl; }

How is better precision achieved in double?

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:

h

sizeof

copy
1
std::cout << sizeof(long double);

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:

h

double

copy
1
long double pi = 3.141592653589793238462643L;

Note

If you got zeros in the output, make sure you assigned the values exactly like shown above (1/3., 1/3.L).

Tarea
test

Swipe to show code editor

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.

  1. Initialize a float variable with the value 1/3.;
  2. Initialize a double variable with the value 1/3.;
  3. Initialize a long double variable with the value 1/3.L.
    note: long double takes up 16 bytes of memory on our system
  4. Set the precision of output to 25 decimals with setprecision(25);
  5. Print all those values multiplied by themselves.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 5
toggle bottom row

bookDouble-Precision Numbers

The precision of float (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:

double

double

copy
1
double num = 3.14159265359;

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

Don't forget to include <iomanip> to be able to use std::setprecision().

cpp

main

copy
123456789101112131415
#include <iostream> #include <iomanip> int main() { float pi_float = 3.14159265358979; double pi_double = 3.14159265358979; std::cout << "float pi: " << pi_float << std::endl; std::cout << "double pi: " << pi_double << std::endl; std::cout << std::setprecision(15); std::cout << "float pi with setprecision: " << pi_float << std::endl; std::cout << "double pi with setprecision: " << pi_double << std::endl; }

How is better precision achieved in double?

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:

h

sizeof

copy
1
std::cout << sizeof(long double);

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:

h

double

copy
1
long double pi = 3.141592653589793238462643L;

Note

If you got zeros in the output, make sure you assigned the values exactly like shown above (1/3., 1/3.L).

Tarea
test

Swipe to show code editor

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.

  1. Initialize a float variable with the value 1/3.;
  2. Initialize a double variable with the value 1/3.;
  3. Initialize a long double variable with the value 1/3.L.
    note: long double takes up 16 bytes of memory on our system
  4. Set the precision of output to 25 decimals with setprecision(25);
  5. Print all those values multiplied by themselves.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 5
toggle bottom row

bookDouble-Precision Numbers

The precision of float (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:

double

double

copy
1
double num = 3.14159265359;

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

Don't forget to include <iomanip> to be able to use std::setprecision().

cpp

main

copy
123456789101112131415
#include <iostream> #include <iomanip> int main() { float pi_float = 3.14159265358979; double pi_double = 3.14159265358979; std::cout << "float pi: " << pi_float << std::endl; std::cout << "double pi: " << pi_double << std::endl; std::cout << std::setprecision(15); std::cout << "float pi with setprecision: " << pi_float << std::endl; std::cout << "double pi with setprecision: " << pi_double << std::endl; }

How is better precision achieved in double?

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:

h

sizeof

copy
1
std::cout << sizeof(long double);

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:

h

double

copy
1
long double pi = 3.141592653589793238462643L;

Note

If you got zeros in the output, make sure you assigned the values exactly like shown above (1/3., 1/3.L).

Tarea
test

Swipe to show code editor

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.

  1. Initialize a float variable with the value 1/3.;
  2. Initialize a double variable with the value 1/3.;
  3. Initialize a long double variable with the value 1/3.L.
    note: long double takes up 16 bytes of memory on our system
  4. Set the precision of output to 25 decimals with setprecision(25);
  5. Print all those values multiplied by themselves.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

The precision of float (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:

double

double

copy
1
double num = 3.14159265359;

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

Don't forget to include <iomanip> to be able to use std::setprecision().

cpp

main

copy
123456789101112131415
#include <iostream> #include <iomanip> int main() { float pi_float = 3.14159265358979; double pi_double = 3.14159265358979; std::cout << "float pi: " << pi_float << std::endl; std::cout << "double pi: " << pi_double << std::endl; std::cout << std::setprecision(15); std::cout << "float pi with setprecision: " << pi_float << std::endl; std::cout << "double pi with setprecision: " << pi_double << std::endl; }

How is better precision achieved in double?

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:

h

sizeof

copy
1
std::cout << sizeof(long double);

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:

h

double

copy
1
long double pi = 3.141592653589793238462643L;

Note

If you got zeros in the output, make sure you assigned the values exactly like shown above (1/3., 1/3.L).

Tarea
test

Swipe to show code editor

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.

  1. Initialize a float variable with the value 1/3.;
  2. Initialize a double variable with the value 1/3.;
  3. Initialize a long double variable with the value 1/3.L.
    note: long double takes up 16 bytes of memory on our system
  4. Set the precision of output to 25 decimals with setprecision(25);
  5. Print all those values multiplied by themselves.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 5
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt