English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have to write a code for addition of two numbers that will be entered as decimal, written on the screen as binary and then have them added up together. I know how to do the conversion and the display of the numbers but how do i add up two floating point numbers. I don't understand that float point part

2007-05-12 03:44:31 · 2 answers · asked by Pinky 1 in Computers & Internet Programming & Design

the result should be a 32-bit binary number, written in the form Manisa*2^(Eksponent)
I will be using C++

2007-05-12 05:11:22 · update #1

2 answers

Floats are hardly a new idea, their so commonly needed that you really can just add them, and the language does all the work for you(they are a native type)*, eg if you want to add floats a & b and store the result in c:

c=a+b;

NB: actually only one of the pairs of operands needs to be a float, that is a float plus an integer always gives a float as the result unless a cast is applied.

This is important in division eg:
float a=4 / 3;
float b=4 / (float)3;
float c=4.0f / 3;
While both c & b will be 1.333... while variable a's value will be computed in all integer arthmetic and hence will be 1.0f;

Were talking about IEEE standard floating point representation though, so they aren't quite what you describe a float to look like, but you're close, that's the concept, just they pulled a few tricks for efficency/accuracy. The link below gives more details explaining how IEEE floats differ from say a naive attempt to express scientific notation in binary form.

* not only native variables can be added this way but others too: "operator overloading" is one of C++ great assests, you can literally write a function for the plus sign to use with a data type you've made up. But thats an advanced topic.

2007-05-12 06:31:56 · answer #1 · answered by Anonymous · 0 0

I assume you are using a programming language that has a variable type of float.

You just add two number.
total=num1+num2

The programming language will do the rest.
Float and Double are just ways the programming language uses for defining numbers with decimal places.

I want to add a link that will give you more information but I am unable to find one that explains it in a simple manner.

Basically a floating point number is in 2 parts as you probably know.
The Mantissa is the number, well the digits that represent the number. sometimes this is shown with a 0. in front of the digits.
The Exponent tells you how to move the decimal point.

Try to think of it as a set of scales. The final number on one side and the exponent on the other.
If you increase the exponent then the final number will be increased to keep the scales balanced.

So
Mantissa, Exponent, Value
71, 0, 71
71, 1, 710
71, 2, 7100
71, -1, 7.1

I hope this helps.

2007-05-12 12:04:55 · answer #2 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers