☯ All C++ Articles☯ All Articles

A new product.

Compasiron of floating point number and integer

Last updated on: .

◕ Disadvantages of floating point numbers.

If we use floating point numbers in our program then we should take care of the following things.

1st ▨
Floating point operations are usually slightly slower than integer operations.

2nd ▨
We can lose precision in floating point applications.

◕ Let have an example:

#include <iostream>
using namespace std;
float a = 1.234E12f;
float b = a + 1.0f;
int main(){
cout << " The value of a is: " << a << endl;
cout << " We substract a from b. " << endl;
cout << " The result is: " << b - a << endl;
return 0;
}

The output result will be like this:
The value of a is: 1.234E12f
We substract a from b.
The result is: 0

Explanation:
In the above example the we add 1 with the value a. The new value is b.
Then we substract a from the value b.
Now the result should be 1.
But the result is 0.

Because type float can represent only the first 6 or 7 digits in a number.
So trying to change the 12th digit of a has no effect on the value.

◕ Advantages of floating point number over integer.

- Only floating point numbers can represent values between two integers.
Integer can not do this.

- Floating point numbers can represent much greater range of values than integers.
Because of the scaling factor of the floating point numbers.

Related articles:
Why floating point numbers are called floating point numbers?
Floating points numbers
Different way to write Floating-Point Numbers in C++
climits in C++
sizeof Operator in C++
Bits & Bytes
Integer Type in C++
Signed char and Unsigned char in C++
Rules for Variable Names
Variable in C++
Fundamental Data Types & Compound Data Types