add complex random
This commit is contained in:
parent
7afca35c73
commit
00794ec956
@ -38,6 +38,21 @@ double gctl::random(double low, double hig)
|
|||||||
return f*(hig-low) + low;
|
return f*(hig-low) + low;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::complex<double> random(std::complex<double> low, std::complex<double> hig)
|
||||||
|
{
|
||||||
|
std::complex<double> c;
|
||||||
|
double r = (double) rand()/RAND_MAX;
|
||||||
|
double i = (double) rand()/RAND_MAX;
|
||||||
|
double rh = std::max(hig.real(), low.real());
|
||||||
|
double rl = std::min(hig.real(), low.real());
|
||||||
|
double ih = std::max(hig.imag(), low.imag());
|
||||||
|
double il = std::min(hig.imag(), low.imag());
|
||||||
|
|
||||||
|
c.real(r*(rh - rl) + rl);
|
||||||
|
c.imag(i*(ih - il) + il);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
bool gctl::isequal(double f, double v, double eps)
|
bool gctl::isequal(double f, double v, double eps)
|
||||||
{
|
{
|
||||||
return fabs(f - v) < eps;
|
return fabs(f - v) < eps;
|
||||||
|
@ -45,10 +45,9 @@ namespace gctl
|
|||||||
*
|
*
|
||||||
* @note Call srand(seed) to initiate the random squence before using this function.
|
* @note Call srand(seed) to initiate the random squence before using this function.
|
||||||
*
|
*
|
||||||
* @tparam T Value type
|
|
||||||
* @param low Lower bound
|
* @param low Lower bound
|
||||||
* @param hig Higher bound
|
* @param hig Higher bound
|
||||||
* @return T Random value
|
* @return Random value
|
||||||
*/
|
*/
|
||||||
int random(int low, int hig);
|
int random(int low, int hig);
|
||||||
|
|
||||||
@ -57,13 +56,23 @@ namespace gctl
|
|||||||
*
|
*
|
||||||
* @note Call srand(seed) to initiate the random squence before using this function.
|
* @note Call srand(seed) to initiate the random squence before using this function.
|
||||||
*
|
*
|
||||||
* @tparam T Value type
|
|
||||||
* @param low Lower bound
|
* @param low Lower bound
|
||||||
* @param hig Higher bound
|
* @param hig Higher bound
|
||||||
* @return T Random value
|
* @return Random value
|
||||||
*/
|
*/
|
||||||
double random(double low, double hig);
|
double random(double low, double hig);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Return a random number in the range [low, hig]
|
||||||
|
*
|
||||||
|
* @note Call srand(seed) to initiate the random squence before using this function.
|
||||||
|
*
|
||||||
|
* @param low Lower bound
|
||||||
|
* @param hig Higher bound
|
||||||
|
* @return Random value
|
||||||
|
*/
|
||||||
|
std::complex<double> random(std::complex<double> low, std::complex<double> hig);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 比较两个浮点数。注意比较的精度为 eps 减一位,比如 eps 为1e-10则比较的精度为小数点后9位。
|
* @brief 比较两个浮点数。注意比较的精度为 eps 减一位,比如 eps 为1e-10则比较的精度为小数点后9位。
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user