Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How computer does floating point arithmetic?

I have seen long articles explaining how floating point numbers can be stored and how the arithmetic of those numbers is being done, but please briefly explain why when I write

cout << 1.0 / 3.0 <<endl; 

I see 0.333333, but when I write

cout << 1.0 / 3.0 + 1.0 / 3.0 + 1.0 / 3.0 << endl; 

I see 1.

How does the computer do this? Please explain just this simple example. It is enough for me.

like image 563
Narek Avatar asked May 17 '11 15:05

Narek


People also ask

How floating point arithmetic operations are performed?

Arithmetic operations on floating point numbers consist of addition, subtraction, multiplication and division. The operations are done with algorithms similar to those used on sign magnitude integers (because of the similarity of representation) — example, only add numbers of the same sign.

How do computers store floating-point numbers?

All floating point numbers are stored by a computer system using a mantissa and an exponent. The following example is used to illustrate the role of the mantissa and the exponent. It does not fully reflect the computer's method for storing real numbers but gives the general idea.


1 Answers

Check out the article on "What every computer scientist should know about floating point arithmetic"

like image 118
Prasoon Saurav Avatar answered Oct 12 '22 07:10

Prasoon Saurav