### how to add two numbers in my own datatype

hello

i have made a new data type which named LongDouble

it takes 16 byte to save the number
the first bit for the sign
the next 15 bit for the exponent
the other 112 bit for the mantissa

, i have to apply four operation on these numbers , but i have a problem in addition , when the sign for any of the numbers is negative ,how i can perform the operation , also if there is a Mantissa how i can also perform the operation ????????

if i try to save mantissa in a variable there would a loss of precision ...

here's my code

 ``12345678910111213141516171819202122232425262728`` `````` LongDouble LongDouble::operator+(const LongDouble& olong) { LongDouble temp; if (this->sign == 0 && olong.sign == 0) { temp.exponent = this->exponent + olong.exponent; for (int i = 0; i < len; i++) { temp.mantissa[i] = this->mantissa[i] + olong.mantissa[i]; } temp.sign = 0; } if (this->sign == 1 && olong.sign == 1) { temp.exponent = this->exponent + olong.exponent; for (int i = 0; i < len; i++) { temp.mantissa[i] = this->mantissa[i] + olong.mantissa[i]; } temp.sign = 1; } return temp; }``````
Last edited on
First, I presume you are normalizing your data. It seems like a weird concept, but it is actually very important for floating point math.

Remember also that without the sign, a number is just magnitude. When signs equal (both are positive or both are negative), then you just add magnitudes.

Only when signs are unequal (one is positive and one is negative) do you you have to care about finding the difference between them, and assigning the correct sign to the result.

Hope this helps.

BTW, how do you plan to print your numbers? The code to do that is deep black magic.
Last edited on
I know that , and i asked for :)
how to add if the is different....
?????any help pls
 I know that

The code you posted does not show that you know that.

 how to add if the is different...

If the signs are the same: add the magnitudes (your code does that... almost)
If the signs are different: subtract the magnitudes

You are going to have to use basic algebra to do it. (Be careful with your exponents.)

You are also going to have to add code that handles overflows.

Remember to add with carry when summing the pieces of the mantissa.
Topic archived. No new replies allowed.