I'm designing a LongDouble datatype which take a 16 byte space.
my question is how to add or substract a two double numbers correctly
ex :
a= 3.025466
b=4.132655``
the result will be 7.158121
but in my code it's different
here is my code
LongDouble LongDouble::operator+( LongDouble olong)
{
LongDouble temp;
if (this->sign == 0 && olong.sign == 0)
{
temp.exponent = this->exponent + olong.exponent;
for (int i = 0; i < 7; i++)
temp.mantissa[i] = this->mantissa[i] + olong.mantissa[i];
if (temp.mantissa[0] > 9)
{
temp.exponent += 1;
temp.mantissa[0]-=10;
}
temp.sign = 0;
}
if (this->sign == 1 && olong.sign == 1)
{
temp.exponent = this->exponent + olong.exponent;
for (int i = 0; i < 7; i++)
temp.mantissa[i] = this->mantissa[i] + olong.mantissa[i];
if (temp.mantissa[1] > 9)
{
temp.exponent += -1;
temp.mantissa[1] += 1;
}
temp.sign = 1;
}
if (this->sign == 0 && olong.sign == 1)
{
temp.exponent = this->exponent + (-1)*olong.exponent;
for (int i = 0; i < 7; i++)
temp.mantissa[i]= this->mantissa[i] + (-1)*olong.mantissa[i];
if (temp.exponent < 0)
temp.sign = 1;
else
temp.sign = 0;
}
if (this->sign == 1 && olong.sign == 0)
{
temp.exponent = (-1)*this->exponent + olong.exponent;
for (int i = 0; i < 7; i++)
temp.mantissa[i]= (-1)*this->mantissa[i] + olong.mantissa[i];
if (temp.exponent < 0)
temp.sign = 1;
else
temp.sign = 0;
}
return temp;
}