complex.c
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 434 行 · 第 1/2 页
C
434 行
//
// Copyright (C) 1991 Texas Instruments Incorporated.
// Copyright (C) 1992 General Electric Company.
//
// Permission is granted to any individual or institution to use, copy, modify,
// and distribute this software, provided that this complete copyright and
// permission notice is maintained, intact, in all copies and supporting
// documentation.
//
// Texas Instruments Incorporated and General Electric Company
// provides this software "as is" without express or implied warranty.
//
//
// Created: MBN 11/01/89 -- Initial implementation
// Updated: MBN 03/04/90 -- Added exception for DIVIDE_BY_ZERO
// Updated: MJF 03/12/90 -- Added group names to RAISE
// Updated: MJF 07/31/90 -- Added terse print
// Updated: DLS 03/22/91 -- New lite version
// Updated: VDN 06/29/92 -- roots of real polynomial, degree <= 4.
//
// The Complex class implements Complex numbers and arithmetic. A Complex
// object has the same precision and range of values as the system built-in
// type double. Implicit conversion to the system defined types short, int,
// long, float, and double is supported by overloaded operator member
// functions. Although the Complex class makes judicous use of inline
// functions and deals only with floating point values, the user is warned that
// the Complex double arithmetic class is still slower than the built-in real
// data types.
//
// Find roots of a real polynomial in a single variable, with degree <=4.
// Reference: Winston, P.H, Horn, B.K.P. (1984) "Lisp", Addison-Wesley.
#ifndef COMPLEXH // If no Complex class
#include <cool/Complex.h> // Include class definition
#endif
// minus_infinity -- Raise Error exception for negative infinity
// Input: Character string of derived class and function
// Output: None
void CoolComplex::minus_infinity (const char* name) const {
//RAISE (Error, SYM(CoolComplex), SYM(Minus_Infinity),
printf ("CoolComplex::%s: Operation results in negative infinity value",
name);
abort ();
}
// plus_infinity -- Raise Error exception for positive infinity
// Input: Character string of derived class and function
// Output: None
void CoolComplex::plus_infinity (const char* name) const {
//RAISE (Error, SYM(CoolComplex), SYM(Plus_Infinity),
printf ("CoolComplex::%s: Operation results in positive infinity value",
name);
abort ();
}
// overflow -- Raise Error exception for overflow occuring during conversion
// Input: Character string of derived class and function
// Output: None
void CoolComplex::overflow (const char* name) const {
//RAISE (Error, SYM(CoolComplex), SYM(Overflow),
printf ("CoolComplex::%s: Overflow occured during type conversion", name);
abort ();
}
// underflow -- Raise Error exception for underflow occuring during conversion
// Input: Character string of derived class name and function
// Output: None
void CoolComplex::underflow (const char* name) const {
//RAISE (Error, SYM(CoolComplex), SYM(Underflow),
printf ("CoolComplex::%s: Underflow occured during type conversion",name);
abort ();
}
// no_conversion -- Raise Error exception for no conversion from CoolComplex
// Input: Character string of derived class name and function
// Output: None
void CoolComplex::no_conversion (const char* name) const {
//RAISE (Error, SYM(CoolComplex), SYM(No_Conversion),
printf ("CoolComplex::%s: No conversion from CoolComplex", name);
abort ();
}
// divide_by_zero -- Raise Error exception for divide by zero
// Input: Character string of derived class and function
// Output: None
void CoolComplex::divide_by_zero (const char* name) const {
//RAISE (Error, SYM(CoolComplex), SYM(Divide_By_Zero),
printf ("CoolComplex::%s: Divide by zero", name);
abort ();
}
// operator= -- Overload the assignment operator for the CoolComplex class
// Input: Reference to CoolComplex object
// Output: Reference to updated CoolComplex object
CoolComplex& CoolComplex::operator= (const CoolComplex& c) {
this->r = c.r; // Copy real part
this->i = c.i; // Copy imaginary part
this->state = c.state; // Copy state
return *this; // Return reference
}
// operator short -- Provide implicit conversion from CoolComplex to short
// Input: None
// Output: Converted number
CoolComplex::operator short () {
if (this->i != 0.0) { // If there is an i-part
this->state = N_NO_CONVERSION; // Indicate exception case
this->no_conversion ("operator short"); // And raise exception
}
else if (this->r > MAXSHORT) { // If there is an overflow
this->state = N_OVERFLOW; // Indicate exception case
this->overflow ("operator short"); // And raise exception
}
else if (this->r < MINSHORT) { // If there is an underflow
this->state = N_UNDERFLOW; // Indicate exception case
this->underflow ("operator short"); // And raise exception
}
return short(this->r); // Return converted number
}
// operator int -- Provide implicit conversion from CoolComplex to int
// Input: None
// Output: Converted number
CoolComplex::operator int () {
if (this->i != 0.0) { // If there is an i-part
this->state = N_NO_CONVERSION; // Indicate exception case
this->no_conversion ("operator int"); // And raise exception
}
else if (this->r > MAXINT) { // If there is an overflow
this->state = N_OVERFLOW; // Indicate exception case
this->overflow ("operator int"); // And raise exception
}
else if (this->r < MININT) { // If there is an underflow
this->state = N_UNDERFLOW; // Indicate exception case
this->underflow ("operator int"); // And raise exception
}
return int (this->r); // Return converted number
}
// operator long -- Provide implicit conversion from CoolComplex to long
// Input: None
// Output: Converted number
CoolComplex::operator long () {
if (this->i != 0.0) { // If there is an i-part
this->state = N_NO_CONVERSION; // Indicate exception case
this->no_conversion ("operator long"); // And raise exception
}
else if (this->r > MAXLONG) { // If there is an overflow
this->state = N_OVERFLOW; // Indicate exception case
this->overflow ("operator long"); // And raise exception
}
else if (this->r < MINLONG) { // If there is an underflow
this->state = N_UNDERFLOW; // Indicate exception case
this->underflow ("operator long"); // And raise exception
}
return long (this->r); // Return converted number
}
// operator float -- Provide implicit conversion from CoolComplex to float
// Input: None
// Output: Converted number
CoolComplex::operator float () {
if (this->i != 0.0) { // If there is an i-part
this->state = N_NO_CONVERSION; // Indicate exception case
this->no_conversion ("operator float"); // And raise exception
}
else if (this->r > MAXFLOAT) { // If there is an overflow
this->state = N_OVERFLOW; // Indicate exception case
this->overflow ("operator float"); // And raise exception
}
else if (this->r < 0.0 && (-this->r) < MINFLOAT) { // Is there an underflow?
this->state = N_UNDERFLOW; // Indicate exception case
this->underflow ("operator float"); // And raise exception
}
return float (this->r); // Return converted number
}
// operator double -- Provide implicit conversion from CoolComplex to double
// Input: None
// Output: Converted number
CoolComplex::operator double () {
if (this->i != 0.0) { // If there is an i-part
this->state = N_NO_CONVERSION; // Indicate exception case
this->no_conversion ("operator double"); // And raise exception
}
return double (this->r); // Return converted number
}
// operator/= -- Overload the division/assign operator for the CoolComplex class
// Input: Reference to complex object
// Output: Reference to new complex object
CoolComplex& CoolComplex::operator/= (const CoolComplex& c2) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?