Print Friendly and PDF
Floating-point data types are used to define variable that can hold real numbers. Whole numbers are not adequate for many jobs. If you are writing a program that works with dollar amounts or precise measurements, you need a data type that allows fractional values. In programming terms, these are called floating-point numbers.

CONCEPT:

Floating-point data types are used to define variable that can hold real numbers. Whole numbers are not adequate for many jobs. If you are writing a program that works with dollar amounts or precise measurements, you need a data type that allows fractional values. In programming terms, these are called floating-point numbers.
Internally, floating-point numbers are stored in a manner similar to scientific notation. Take the number 47,281.97. In scientific notation this number is 4.728197 10^4(10^4 is equal to 10,000 and 4.728197 10,000 is 47,281.97). The first of part this number, 4.728197, is called the mantissa.The mantissa is multiplied by the power of 10.
The first part of the number 4.728197 is called mantissa .The mantissa is multiplied by a power of 10.Computers typically use E notation represent floating point value .in E notation the number 47,281.97 would be 4.728197E4.The part of number before the E is mantissa and the part after the E is power of 10.when floating point number is stored in memory.it is stored as the mantissa and the power of 10.

Table 2-7 shows other number to represent in scientific and E notation



  In C++ there are three type of data type that can represent floating point number .there are 
  • Float
  • Double 
  • Long Double 
The floating data type is considered single precision. The double data type is usually twice as big as float, so it is considered as double precision as you’ve guessed , long double is intended to be larger than double of course size of these data type depend upon the computer you are using the only guarantees are
  •  A double is at least as big as float.
  • A long double is at least as big as a double. 
Table 2-8 show the size and rang of floating point data type usually found on PC’s.



*some compiler use 10 byte for long double. The allow a range of 3.4E-4932 to 1.1E4832

You will notice there are no assigned floating point data type .on all machine variable of floating, double and double long data type can store positive or negative

Floating Point Literal

Floating point literals may be expressed in variety of ways.as shown in below E notation is one method. When you are writing number that are extremely large or extremely small ,this will probably be the easiest way E notation number may be expressed with an uppercase E or a lowercase e. notice that in the source code literal were written as 1.495979E11 and 1.989E30, but the program printed them as 1.49598e+110 and 1.989e+30. The two sets of numbers are equivalent (The plus sign in front of the exponent is also optional)

Example

// This program uses floating point data types.
#include <iostream>
using namespace std;
int main()
{
float distance;
double mass;
distance = 1.495979E11;
mass = 1.989E30;
cout << "The Sun is " << distance << " meters away.\n";
cout << "The Sun\'s mass is " << mass << " kilograms.\n";
return 0;
}

Program Output

The Sun is 1.49598e+011 meters away.
The Sun's mass is 1.989e+030 kilograms

You can also express floating point literal in decimal notation. The literal 1.495979E11 could have written as

149597900000.00

Obviously the E notation is more convenient for lengthy number, but for the number like 47.39 decimal notation is preferable to 4.739E1

All of the following floating point literal are equivalent:


  •  1.4959E11

  • 1.4959e11

  •  1.4959E+11

  •  1.4959e+11

  • 1495900000.00

Floating point literal normally stored in memory as a double. But remember C++ provide tools for handling just about any situation. Just in case you need to force literal to be stored as float. You can append the letter F or f to the end of it.

For Example


  •  1.2F
  •  45.90f

If you want to force a value to be stored as long double, append an L or l to it, as in the following example:

  • 1030.40L
  • 80.20

    Assigning floating point value to integer

When assigning the floating point value to an integer variable. The fractional part of the value is (the part after decimal point) discarded

For Example

Int number ;

Number =7.5; // 7 assigned to number

When part of after decimal point discarded is said to be truncated.

Assigning a floating point variable to an integer variable has the same effect.

For example:

Int i ;

Float f ;

F=5.8;

F=i; // 5 assign to i

When float variable f assign to int variable i, the value being assigned (5.8) is truncated after this code execute i hold the value 5 and f will hold the value 5.8




zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: