Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef NUMBER_H
00029 #define NUMBER_H
00030
00031 #include "QGpCoreToolsDLLExport.h"
00032 #include "Trace.h"
00033 #include "Complex.h"
00034 #include "Translations.h"
00035
00036 namespace QGpCoreTools {
00037
00038 #ifdef BIGNUM
00039 #include <mp/mpreal.h>
00040 #endif
00041
00042 class QGPCORETOOLS_EXPORT Number
00043 {
00044 TRANSLATIONS("Number");
00045 public:
00046 enum Type {Fixed=0, Scientific, Weeks, Days, Hours, Minutes, Seconds};
00047
00048 static unsigned int nextPowerOfTwo(unsigned int val);
00049 static inline double sign(double val);
00050 static inline int sign(int val);
00051 static inline Type type(char t);
00052 static inline char type(Type t);
00053
00054 static inline QString toString(double val, Type t, int precision);
00055 static inline QString toString(double val, int precision);
00056 static inline QString toString(const Complex& val, int precision);
00057
00058 static inline double toDouble(const QString& str, Type t, bool * ok=0);
00059
00060 static double toDouble(float f);
00061 static double toDouble(const Complex & c) {return c.abs();}
00062 static double toDouble(double a) {return a;}
00063 #ifdef BIGNUM
00064 static double toDouble(mp_real a) {return dble(a);}
00065 #endif
00066 static unsigned char fromBinaryCodedDecimal(unsigned char bcd);
00067 static unsigned short fromBinaryCodedDecimal(unsigned short bcd);
00068 static unsigned int fromBinaryCodedDecimal(unsigned int bcd);
00069 static unsigned char toBinaryCodedDecimal(unsigned char val);
00070 static unsigned short toBinaryCodedDecimal(unsigned short val);
00071 static unsigned int toBinaryCodedDecimal(unsigned int val);
00072
00073 static QList<int> divisors(int n);
00074
00075 static QString secondsToTime(double sec, Number::Type type=Number::Weeks, int numberPrecision=-1);
00076 static double timeToSeconds(const QString& str);
00077 private:
00078 inline static bool timeToSecondsSign(const QChar * p, int& i, int n);
00079 static double timeToSecondsError(const QString& str, int pos);
00080 };
00081
00082 inline double abs(double a)
00083 {
00084 return ::fabs(a);
00085 }
00086
00087 inline double abs2(double a)
00088 {
00089 return a*a;
00090 }
00091
00092 inline Number::Type Number::type(char t)
00093 {
00094 switch(t) {
00095 case 'e':
00096 return Scientific;
00097 case 't':
00098 case 'w':
00099 return Weeks;
00100 case 'd':
00101 return Days;
00102 case 'h':
00103 return Hours;
00104 case 'm':
00105 return Minutes;
00106 case 's':
00107 return Seconds;
00108 default:
00109 return Fixed;
00110 }
00111 }
00112
00113 inline char Number::type(Type t)
00114 {
00115 switch(t) {
00116 case Fixed:
00117 break;
00118 case Scientific:
00119 return 'e';
00120 case Weeks:
00121 return 'w';
00122 case Days:
00123 return 'd';
00124 case Hours:
00125 return 'h';
00126 case Minutes:
00127 return 'm';
00128 case Seconds:
00129 return 's';
00130 }
00131 return 'f';
00132 }
00133
00134 inline double Number::toDouble(const QString& str, Type t, bool * ok)
00135 {
00136 switch(t) {
00137 case Fixed:
00138 case Scientific:
00139 return str.toDouble(ok);
00140 case Weeks:
00141 case Days:
00142 case Hours:
00143 case Minutes:
00144 case Seconds:
00145 return timeToSeconds(str);
00146 }
00147 return 0.0;
00148 }
00149
00150 inline QString Number::toString(double val, Type t, int precision)
00151 {
00152 switch(t) {
00153 case Fixed:
00154 return QString::number(val, 'f', precision);
00155 case Scientific:
00156 return QString::number(val, 'e', precision);
00157 case Weeks:
00158 case Days:
00159 case Hours:
00160 case Minutes:
00161 case Seconds:
00162 return secondsToTime(val, t, precision);
00163 }
00164 return QString::null;
00165 }
00166
00167 inline QString Number::toString(double val, int precision)
00168 {
00169 return QString::number(val, 'g', precision);
00170 }
00171
00172 inline QString Number::toString(const Complex& val, int precision)
00173 {
00174 return val.toString(precision);
00175 }
00176
00177 inline double Number::sign(double val)
00178 {
00179 if(val>0.0) {
00180 return 1.0;
00181 } else if(val<0.0) {
00182 return -1.0;
00183 } else {
00184 return 0.0;
00185 }
00186 }
00187
00188 inline int Number::sign(int val)
00189 {
00190 if(val>0) {
00191 return 1;
00192 } else if(val<0) {
00193 return -1;
00194 } else {
00195 return 0;
00196 }
00197 }
00198
00199 }
00200
00201 #endif // NUMBER_H