您的位置:首页 > 其它

【刘庆源码共享】稀疏线性系统求解算法MGMRES(m) 之 基础类(Double类,封装double)

2010-11-13 23:41 393 查看
/*
* Copyright (c) 2009 湖南师范大学数计院 一心飞翔项目组
* All Right Reserved
*
* 文件名:Double.h
* 摘 要:定义一个与double对应的类,有助于捕捉处理 除数为0 的异常
*
* 作 者:刘 庆
* 完成日期:2009年8月24日
*
*/

#include <math.h>
#include <iostream>
#include <fstream>
#include "MyException.h"
#include "define.h"

#ifndef _DOUBLE__
#define _DOUBLE__

class Double
{
double data;
public:
Double()
{
data = 0.0;
}
Double(double d)
{
data = d;
}
~Double(){ }

double GetData()const
{
return data;
}

template <typename T> Double& operator = (const T t)
{
data = t;
return *this;
}
Double& operator = (const Double& d)
{
data = d.data;
return *this;
}

template <typename T> double operator / (const T t)
{
if (t == 0)
{
cout << "Divide By Zero Exception" << endl;
return 0;
throw DivideByZeroException("Divide By Zero Exception");
}
else
{
return data/t;
}
}
double operator / (const Double& t)
{
if ( t.data==0 )
{
cout << "Divide By Zero Exception" << endl;
return 0;
throw DivideByZeroException("Divide By Zero Exception");
}
else
{
return data/t.data;
}
}
template <typename T> double operator * (const T t)
{
return data*t;
}
double operator * (const Double& d)
{
return data*d.data;
}
template <typename T> double operator - (const T t)
{
return data - t;
}
double operator - (const Double& d)
{
return data - d.data;
}
template <typename T> double operator + (const T t)
{
return data + t;
}
double operator + (const Double& t)
{
return data + t.data;
}
template <typename T> Double& operator += (const T t)
{
data = data + t;
return *this;
}
Double& operator += (const Double& t)
{
data = data + t.data;
return *this;
}
template <typename T> Double& operator *= (const T t)
{
data = data*t;
return *this;
}
Double& operator *= (const Double& t)
{
data = data * t.data;
return *this;
}
template <typename T> Double& operator -= (const T t)
{
data = data - t;
return *this;
}
Double& operator -= (const Double& t)
{
data = data - t.data;
return *this;
}
template <typename T> Double& operator /= (const T t)
{
if (t == 0)
throw DivideByZeroException("Divide By Zero Exception");
else
{
data = data/t;
return *this;
}
}
Double& operator /= (const Double& t)
{
if(t.data==0)
throw DivideByZeroException("Divide By Zero Exception");
else
{
data = data/t.data;
return *this;
}
return *this;
}
template <typename T> bool operator > (const T t) const
{
if(data > t)
return true;
else
return false;
}
bool operator > (const Double& d)
{
if (data > d.data)
return true;
else
return false;
}
template <typename T> bool operator < (const T t) const
{
if (data < t)
return true;
else
return false;
}
bool operator < (const Double& d)
{
if (data < d.data)
return true;
else
return false;
}
template <typename T> bool operator >= (const T t) const
{
if (data >= t)
return true;
else
return false;
}
bool operator >= (const Double& d)
{
if (data >= d.data)
return true;
else
return false;
}
template <typename T> bool operator <= (const T t) const
{
if (data <= t)
return true;
else
return false;
}
bool operator <= (const Double& d)
{
if (data<=d.data)
return true;
else
return false;
}
template <typename T> bool operator == (const T t) const
{
if (t == 0)
{
if(fabs(data)<=_PRECIOUS)
return true;
else
return false;
}
if (data == t)
return true;
else
return false;
}

template <typename T> bool operator == (const T t)
{
if (t == 0)
{
if (fabs(data) <= _PRECIOUS)
return true;
else
return false;
}
if (data == t)
return true;
else
return false;
}
bool operator == (const Double& d)
{
if (data == d.data)
return true;
else
return false;
}
template <typename T> bool operator != (const T t)const
{
if (t == 0)
{
if (fabs(data) > _PRECIOUS)
return true;
else
return false;
}
if (data != t)
return true;
else
return false;
}
template <typename T> bool operator != (const T t)
{
if (t == 0)
{
if (fabs(data) > _PRECIOUS)
return true;
else
return false;
}
if (data != t)
return true;
else
return false;
}
bool operator != (const Double& d)
{
if (data != d.data)
return true;
else
return false;
}
friend ostream& operator << (ostream& os, Double& d)
{
os << d.data;
return os;
}
friend istream& operator >> (istream& is, Double& d)
{
is >> d.data;
return is;
}
friend ofstream& operator << (ofstream& ofs, Double& d)
{
ofs << d.data;
return ofs;
}
friend ifstream& operator >> (ifstream& ifs, Double& d)
{
ifs >> d.data;
return ifs;
}
bool EqualZero()
{
if (fabs(data) < _PRECIOUS)
return true;
else
return false;
}
bool NotEqualZero()
{
return !EqualZero();
}
};

#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐