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;
}
#include<iostream>
#include<cmath>
using namespace std;

class LongDouble
{
public:
    LongDouble();

    LongDouble operator+( LongDouble olong);
    LongDouble operator-( LongDouble olong);
    LongDouble operator*( LongDouble olong);
    LongDouble operator/( LongDouble olong);
    void print();
    void set(unsigned short arr[], short ex, short s);



private:

    unsigned short mantissa[7];
    signed short exponent : 15;
    unsigned short sign : 1;

};
#include <cstdlib>
#include <iostream>
#include"LongDouble.h"


using namespace std;


int main()
{

    LongDouble l1,l2,l3;
    unsigned short x[7] = { 9, 3, 4, 5, 6, 7, 8 };
    unsigned short ss[7] = { 9, 3, 4, 5, 6, 7, 8 };
    l1.set(x, 3, 0);
    l2.set(ss, 5,0);
    l3 = l1 - l2;
    l1.print();
    l2.print();
    cout << endl;
    l3.print();
    cout << endl;
    l3 = l1 + l2;
    l3.print();
    cout << endl;
    l3 = l1 * l2;
    l3.print();
    cout << endl;
    l3 = l1 / l2;
    l3.print();
    cout << endl;

    system("PAUSE");
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.