|
|
| version 1.3, 2001/07/07 16:38:01 | version 1.9, 2001/11/05 11:46:21 |
|---|---|
| Line 2 | Line 2 |
| Parser: @b math parser class. | Parser: @b math parser class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | $Id$ |
| */ | */ |
| static const char *RCSId="$Id$"; | |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| #include "pa_common.h" | #include "pa_common.h" |
| Line 13 static const char *RCSId="$Id$"; | Line 13 static const char *RCSId="$Id$"; |
| #include "pa_vmath.h" | #include "pa_vmath.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| #ifdef WIN32 | |
| # include <windows.h> | |
| #endif | |
| // defines | // defines |
| #define PI 3.1415926535 | #define PI 3.1415926535 |
| Line 23 static const char *RCSId="$Id$"; | Line 27 static const char *RCSId="$Id$"; |
| class MMath : public Methoded { | class MMath : public Methoded { |
| public: | public: |
| MMath(Pool& pool); | MMath(Pool& pool); |
| void configure_admin(Request& r); | |
| public: // Methoded | public: // Methoded |
| bool used_directly() { return true; } | bool used_directly() { return true; } |
| }; | }; |
| // methods | // methods |
| static unsigned int randomizer=0; | |
| static void _random(Request& r, const String& method_name, MethodParams *params) { | static void _random(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& range=params->as_junction(0, "range must be expression"); | Value& range=params->as_junction(0, "range must be expression"); |
| uint max=(uint)r.process(range).as_double(); | uint max=(uint)r.process(range).as_double(); |
| if(max<=1) | if(max<=1) |
| PTHROW(0, 0, | throw Exception(0, 0, |
| &method_name, | &method_name, |
| "bad range [0..%u]", max); | "bad range [0..%u]", max); |
| r.write_no_lang(*new(pool) VInt(pool, rand()%max)); | Value& result=*new(pool) VInt(pool, (int)( |
| ((double)((randomizer=rand())% RAND_MAX)) / RAND_MAX * (max + 1))); | |
| result.set_name(method_name); | |
| r.write_no_lang(result); | |
| } | } |
| typedef double (*math1_func_ptr)(double); | typedef double (*math1_func_ptr)(double); |
| static double round(double param) { return floor(param+0.5); } | |
| static double sign(double param) { return param > 0 ? 1 : ( param < 0 ? -1 : 0 ); } | |
| static double trunc(double param) { return param > 0? floor(param) : ceil(param); } | |
| static double frac(double param) { return param-trunc(param); } | static double frac(double param) { return param-trunc(param); } |
| static double degrees(double param) { return param /PI *180; } | static double degrees(double param) { return param /PI *180; } |
| static double radians(double param) { return param /180 *PI; } | static double radians(double param) { return param /180 *PI; } |
| Line 106 MATH2(pow); | Line 112 MATH2(pow); |
| MMath::MMath(Pool& apool) : Methoded(apool) { | MMath::MMath(Pool& apool) : Methoded(apool) { |
| set_name(*NEW String(pool(), MATH_CLASS_NAME)); | set_name(*NEW String(pool(), MATH_CLASS_NAME)); |
| // setting seed | |
| srand(getpid()+time(NULL)); rand(); | |
| // ^FUNC(expr) | // ^FUNC(expr) |
| #define ADD1(name) \ | #define ADD1(name) \ |
| Line 134 MMath::MMath(Pool& apool) : Methoded(apo | Line 136 MMath::MMath(Pool& apool) : Methoded(apo |
| } | } |
| // in MSVC each thread has it's own pseudo-random sequence | |
| // in win32 apache each thread can handle multiple requests | |
| // so to get proper randoms we remember random generated in one thread | |
| void MMath::configure_admin(Request&) { | |
| // setting seed | |
| srand( | |
| randomizer ^ | |
| #ifdef WIN32 | |
| GetCurrentThreadId() ^ | |
| #else | |
| getpid() ^ | |
| #endif | |
| (unsigned int)time(NULL) | |
| ); | |
| if(!randomizer) | |
| randomizer=rand(); | |
| } | |
| // global variables | // global variables |
| Methoded *math_base_class; | Methoded *math_base_class; |