What are C++ Floating-Point Constants?

C++Object Oriented ProgrammingProgramming

Floating-point constants specify values that must have a fractional part.

Floating-point constants have a "mantissa," which specifies the value of the number, an "exponent," which specifies the magnitude of the number, and an optional suffix that specifies the constant's type(double or float).

The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example −

24.25
12.00

These values can also contain exponents. For example,

24.25e3 which is equivalent to 24250

In C++ you can use the following code to create a floating point constant −

Example

Live Demo

#include<iostream>
using namespace std;
int main() {
   const float PI = 3.141;  // 3.141 is floating point constant while PI is a constant float.
   cout << PI;
   return 0;
}

Output

This will give the output −

3.141
raja
Published on 16-Feb-2018 07:28:05
Advertisements