utilities.cpp

来自「amygdata的神经网络算法源代码」· C++ 代码 · 共 98 行

CPP
98
字号
/***************************************************************************                          utilities.cpp  -  description                             -------------------    begin                : Wed May 22 2002    copyright            : (C) 2002 by Rdiger Koch    email                : rkoch@rkoch.org ***************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * ***************************************************************************/using namespace std;#include "utilities.h"#include <cmath>#include <stdlib.h>#include <sstream>#include <string>#include <stdexcept>#include <sys/types.h>#include <sys/stat.h>float Utilities::GaussRand(float mean, float sigma){    // Based on code from the GNU Scientific Library    // and _Numerical Recipes in C_	float randVal, xuniRand, yuniRand;    float x, y, r2;  	do {      	// choose x,y in uniform square (-1,-1) to (+1,+1)		xuniRand = float(rand()) / float(RAND_MAX);		yuniRand = float(rand()) / float(RAND_MAX);		x = -1 + 2 * xuniRand;      	y = -1 + 2 * yuniRand;      	// see if it is in the unit circle       	r2 = x * x + y * y;    } while (r2 > 1.0 || r2 == 0);  	// Box-Muller transform  	randVal = sigma * y * sqrt(-2.0 * log(r2) / r2);	return (randVal + mean);}float Utilities::RandPercent(){    return ( float(rand()) / float(RAND_MAX) ) * 100.0;}string Utilities::itostr(int val){    ostringstream o;    if (o << val)        return o.str();    else        throw runtime_error("cannot convert integer to string");    return "";}string Utilities::ftostr(float val){    ostringstream o;    if (o << val)        return o.str();    else        throw runtime_error("cannot convert float to string");    return "";}void Utilities::RoundTime(AmTimeInt& time, const AmTimeInt& stepSize){    double tmpTime;    double roundTime;    tmpTime = (double)time / stepSize;    tmpTime = modf(tmpTime, &roundTime);    if (tmpTime > 0.5) {        time = ((AmTimeInt)roundTime * stepSize) + stepSize;    }    else {        time = (AmTimeInt)roundTime * stepSize;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?