orsa_secure_math.cc

Go to the documentation of this file.
00001 /* 
00002    ORSA - Orbit Reconstruction, Simulation and Analysis
00003    Copyright (C) 2002-2004 Pasquale Tricarico
00004    
00005    This program is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU General Public License
00007    as published by the Free Software Foundation; either version 2
00008    of the License, or (at your option) any later version.
00009    
00010    As a special exception, Pasquale Tricarico gives permission to
00011    link this program with Qt commercial edition, and distribute the
00012    resulting executable, without including the source code for the Qt
00013    commercial edition in the source distribution.
00014    
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019    
00020    You should have received a copy of the GNU General Public License
00021    along with this program; if not, write to the Free Software
00022    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 */
00024 
00025 #include "orsa_secure_math.h"
00026 #include "orsa_error.h"
00027 #include "support.h"
00028 
00029 #include <cmath>
00030 
00031 #include <iostream>
00032 
00033 using namespace std;
00034 
00035 namespace orsa {
00036   
00037   // avoids domain errors when x<0 and non-integer y
00038   double secure_pow(double x, double y) {
00039     
00040     if (x<0.0) {
00041       if (rint(y)!=y) {
00042         ORSA_DOMAIN_ERROR("secure_pow(%g,%g) is undefined!",x,y);
00043         return 1.0; // better value?
00044       } else {
00045         return pow(x,y);
00046       }
00047     } else {
00048       return pow(x,y);
00049     }
00050   }
00051   
00052   // avoids domain errors when x<=0
00053   double secure_log(double x) {
00054     if (x>0) {
00055       return log(x);
00056     } else {
00057       ORSA_DOMAIN_ERROR("secure_log(%g) is undefined!",x);
00058       return 1.0; // better value?
00059     }
00060   }
00061   
00062   // avoids domain errors when x<=0
00063   double secure_log10(double x) {
00064     if (x>0) {
00065       return log10(x);
00066     } else {
00067       ORSA_DOMAIN_ERROR("secure_log10(%g) is undefined!",x);
00068       return 1.0; // better value?
00069     }
00070   }
00071   
00072   // avoids domain errors when x=y=0
00073   double secure_atan2(double x, double y) {
00074     if (x==0.0) {
00075       if (y==0.0) {
00076         // domain error
00077         ORSA_DOMAIN_ERROR("secure_atan2(%g,%g) is undefined!",x,y);
00078         return 1.0; // better value?
00079       } else {
00080         return atan2(x,y);
00081       }
00082     } else {
00083       return atan2(x,y);
00084     }
00085   }
00086   
00087   // avoids domain errors when x is not in [-1,1]
00088   double secure_asin(double x) {
00089     if ((x>1.0) || (x<-1.0)) {
00090       // domain error
00091       ORSA_DOMAIN_ERROR("secure_asin(%g) is undefined!",x);
00092       return 1.0; // better value?
00093     } else {
00094       return asin(x);
00095     }
00096   }
00097   
00098   // avoids domain errors when x is not in [-1,1]
00099   double secure_acos(double x) {
00100     if ((x>1.0) || (x<-1.0)) {
00101       // domain error
00102       ORSA_DOMAIN_ERROR("secure_acos(%g) is undefined!",x);
00103       return 1.0; // better value?
00104     } else {
00105       return acos(x);
00106     }
00107   }
00108   
00109   // avoids domain errors when x<0
00110   double secure_sqrt(double x) {
00111     if (x<0) {
00112       // domain error
00113       ORSA_DOMAIN_ERROR("secure_sqrt(%g) is undefined!",x);
00114       return sqrt(fabs(x)); // better value?
00115     } else {
00116       return sqrt(x);
00117     }
00118   }
00119   
00120 } // namespace orsa

Generated on Wed May 30 13:04:53 2007 for liborsa by  doxygen 1.5.2