Cpp - Floating-point types

Introduction

Type

Size

Range of
Values
Lowest Positive
Value
Accuracy
(decimal)
float
4 bytes
-3.4E+38
1.2E-38
6 digits
double
8 bytes
-1.7E+308
2.3E-308
15 digits
long double
10 bytes
-1.1E+4932
3.4E-4932
19 digits

Numbers with a fraction part are indicated by a decimal point in C++ and are referred to as floating-point numbers.

The following three types are available for calculations involving floating-point numbers:

  • float for simple accuracy
  • double for double accuracy
  • long double for high accuracy

Floating-point literals


5.19              
0.519E1           
0.0519e2          
519.OE-2          

1.8E-2 represents a value of 1.8*0.01.

E can also be written with a small letter e.

A decimal point or E (e) must always be used to distinguish floating-point constants from integer constants.

Floating-point constants are of type double by default.

You can add F or f to designate the float type, or add L or l for the long double type.

Exercise