Pol  Revision:4b29d2b
mathmod.cpp
Go to the documentation of this file.
1 
10 #include "mathmod.h"
11 
12 #include <cmath>
13 #include <stdio.h>
14 
15 #include "../../bscript/berror.h"
16 #include "../../bscript/bobject.h"
17 #include "../../bscript/impstr.h"
18 
19 
20 namespace Pol
21 {
22 namespace Bscript
23 {
24 using namespace Module;
25 
26 template <>
32  {"ASin", &MathExecutorModule::mf_ASin},
33  {"ACos", &MathExecutorModule::mf_ACos},
34  {"ATan", &MathExecutorModule::mf_ATan},
35 
39  {"Sqrt", &MathExecutorModule::mf_Sqrt},
40  {"Root", &MathExecutorModule::mf_Root},
42  {"Log10", &MathExecutorModule::mf_Log10},
43  {"LogE", &MathExecutorModule::mf_LogE},
44 
45  {"DegToRad", &MathExecutorModule::mf_DegToRad},
46  {"RadToDeg", &MathExecutorModule::mf_RadToDeg},
47 
48  {"Ceil", &MathExecutorModule::mf_Ceil},
49  {"Floor", &MathExecutorModule::mf_Floor},
50 
51  {"ConstPi", &MathExecutorModule::mf_ConstPi},
52  {"ConstE", &MathExecutorModule::mf_ConstE},
53 
54  {"FormatRealToString", &MathExecutorModule::mf_FormatRealToString}};
55 }
56 namespace Module
57 {
58 using namespace Bscript;
59 
60 static double const_pi;
61 static double const_e;
62 
63 class initer
64 {
65 public:
66  initer();
67 };
69 {
70  const_pi = acos( double( -1 ) );
71  const_e = exp( double( 1 ) );
72 }
74 
76  : Bscript::TmplExecutorModule<MathExecutorModule>( "math", exec )
77 {
78 }
79 
81 {
82  double x;
83  if ( getRealParam( 0, x ) )
84  {
85  return new Double( sin( x ) );
86  }
87  else
88  {
89  return new BError( "Invalid parameter type" );
90  }
91 }
93 {
94  double x;
95  if ( getRealParam( 0, x ) )
96  {
97  return new Double( cos( x ) );
98  }
99  else
100  {
101  return new BError( "Invalid parameter type" );
102  }
103 }
105 {
106  double x;
107  if ( getRealParam( 0, x ) )
108  {
109  return new Double( tan( x ) );
110  }
111  else
112  {
113  return new BError( "Invalid parameter type" );
114  }
115 }
117 {
118  double x;
119  if ( getRealParam( 0, x ) )
120  {
121  return new Double( asin( x ) );
122  }
123  else
124  {
125  return new BError( "Invalid parameter type" );
126  }
127 }
129 {
130  double x;
131  if ( getRealParam( 0, x ) )
132  {
133  return new Double( acos( x ) );
134  }
135  else
136  {
137  return new BError( "Invalid parameter type" );
138  }
139 }
141 {
142  double x;
143  if ( getRealParam( 0, x ) )
144  {
145  return new Double( atan( x ) );
146  }
147  else
148  {
149  return new BError( "Invalid parameter type" );
150  }
151 }
152 
153 /*
154 math::Pow(x : number, y : number ) : real
155 Function: calculates x raised to the power of y
156 Returns: x^y as a Real
157 Parameters: x and y can be Reals or Integers
158 
159 math::Pow(2,5) = 2^5 = 32
160 */
162 {
163  double x, y;
164  if ( getRealParam( 0, x ) && getRealParam( 1, y ) )
165  {
166  return new Double( pow( x, y ) );
167  }
168  else
169  {
170  return new BError( "Invalid parameter type" );
171  }
172 }
173 
174 /*
175 math::Sqrt( x : number ) : real
176 Returns: Square Root of x as a Real
177 */
179 {
180  double x;
181  if ( getRealParam( 0, x ) )
182  {
183  return new Double( sqrt( x ) );
184  }
185  else
186  {
187  return new BError( "Invalid parameter type" );
188  }
189 }
190 
191 /*
192 math::Root( x : number, y : number ) : real
193 Function: calculates y Root of x as a Real
194 */
196 {
197  double x, y;
198  if ( getRealParam( 0, x ) && getRealParam( 1, y ) )
199  {
200  return new Bscript::Double( pow( x, 1.0 / y ) );
201  }
202  else
203  {
204  return new Bscript::BError( "Invalid parameter type" );
205  }
206 }
207 
209 {
210  Bscript::BObjectImp* impX = getParamImp( 0 );
211  Bscript::BObjectImp* impY = getParamImp( 1 );
212  if ( ( ( impX->isa( Bscript::BObjectImp::OTDouble ) ) ||
213  ( impX->isa( Bscript::BObjectImp::OTLong ) ) ) &&
214  ( ( impY->isa( Bscript::BObjectImp::OTDouble ) ) ||
215  ( impY->isa( Bscript::BObjectImp::OTLong ) ) ) )
216  {
217  if ( *impX < *impY )
218  return impX->copy();
219  else
220  return impY->copy();
221  }
222  else if ( impX->isa( Bscript::BObjectImp::OTArray ) )
223  {
224  Bscript::ObjArray* value = static_cast<Bscript::ObjArray*>( impX );
225  if ( value->ref_arr.empty() )
226  return new Bscript::BError( "Array empty" );
227 
228  Bscript::BObjectImp* compare = nullptr;
229  for ( std::vector<Bscript::BObjectRef>::iterator itr = value->ref_arr.begin();
230  itr != value->ref_arr.end(); ++itr )
231  {
232  if ( itr->get() )
233  {
234  Bscript::BObject* bo = ( itr->get() );
235 
236  if ( bo == nullptr )
237  continue;
238  if ( ( bo->isa( Bscript::BObjectImp::OTDouble ) ) ||
239  ( bo->isa( Bscript::BObjectImp::OTLong ) ) )
240  {
241  if ( compare == nullptr )
242  compare = bo->impptr();
243  else if ( *( bo->impptr() ) < *compare )
244  compare = bo->impptr();
245  }
246  }
247  }
248  if ( compare != nullptr )
249  return ( compare->copy() );
250  else
251  return new Bscript::BError( "No Integer/Double elements" );
252  }
253  else
254  return new Bscript::BError( "Invalid parameter type" );
255 }
256 
258 {
259  Bscript::BObjectImp* impX = getParamImp( 0 );
260  Bscript::BObjectImp* impY = getParamImp( 1 );
261  if ( ( ( impX->isa( Bscript::BObjectImp::OTDouble ) ) ||
262  ( impX->isa( Bscript::BObjectImp::OTLong ) ) ) &&
263  ( ( impY->isa( Bscript::BObjectImp::OTDouble ) ) ||
264  ( impY->isa( Bscript::BObjectImp::OTLong ) ) ) )
265  {
266  if ( *impX < *impY )
267  return impY->copy();
268  else
269  return impX->copy();
270  }
271  else if ( impX->isa( Bscript::BObjectImp::OTArray ) )
272  {
273  Bscript::ObjArray* value = static_cast<Bscript::ObjArray*>( impX );
274  if ( value->ref_arr.empty() )
275  return new Bscript::BError( "Array empty" );
276 
277  Bscript::BObjectImp* compare = nullptr;
278  for ( std::vector<BObjectRef>::iterator itr = value->ref_arr.begin();
279  itr != value->ref_arr.end(); ++itr )
280  {
281  if ( itr->get() )
282  {
283  Bscript::BObject* bo = ( itr->get() );
284 
285  if ( bo == nullptr )
286  continue;
287  if ( ( bo->isa( Bscript::BObjectImp::OTDouble ) ) ||
288  ( bo->isa( Bscript::BObjectImp::OTLong ) ) )
289  {
290  if ( compare == nullptr )
291  compare = bo->impptr();
292  else if ( *( bo->impptr() ) >= *compare )
293  compare = bo->impptr();
294  }
295  }
296  }
297  if ( compare != nullptr )
298  return ( compare->copy() );
299  else
300  return new Bscript::BError( "No Integer/Double elements" );
301  }
302  else
303  return new Bscript::BError( "Invalid parameter type" );
304 }
305 
306 /*
307 math::Abs( x : number) : number
308 Returns: the absolute value of x
309 If a Real is passed, will return a Real
310 If an Integer is passed, will return an Integer
311 
312 math::Abs(-2) = 2
313 math::Abs(-1.5) = 1.5
314 */
316 {
317  Bscript::BObjectImp* imp = getParamImp( 0 );
318  if ( imp->isa( Bscript::BObjectImp::OTDouble ) )
319  {
320  double value = static_cast<Double*>( imp )->value();
321  return new Double( fabs( value ) );
322  }
323  else if ( imp->isa( Bscript::BObjectImp::OTLong ) )
324  {
325  int value = static_cast<BLong*>( imp )->value();
326  return new BLong( labs( value ) );
327  }
328  else
329  {
330  double x;
331  // just for debug.log
332  (void)getRealParam( 0, x );
333  return new BError( "Invalid parameter type" );
334  }
335 }
336 
338 {
339  double x;
340  if ( getRealParam( 0, x ) )
341  {
342  return new Double( log10( x ) );
343  }
344  else
345  {
346  return new BError( "Invalid parameter type" );
347  }
348 }
350 {
351  double x;
352  if ( getRealParam( 0, x ) )
353  {
354  return new Double( log( x ) );
355  }
356  else
357  {
358  return new BError( "Invalid parameter type" );
359  }
360 }
361 
363 {
364  return new Double( const_pi );
365 }
367 {
368  return new Double( const_e );
369 }
370 
372 {
373  double x;
374  int digits;
375  if ( getRealParam( 0, x ) && getParam( 1, digits ) )
376  {
377  char buffer[200];
380  sprintf( buffer, "%.*g", static_cast<int>( digits ), x );
381  return new String( buffer );
382  }
383  else
384  {
385  return new BError( "Invalid parameter type" );
386  }
387 }
388 
390 {
391  double rad;
392  if ( !getRealParam( 0, rad ) )
393  return new BError( "Invalid parameter type" );
394 
395  /*
396  360 degrees <~> 2*pi radians
397  <=> deg/360 = rad / 2*pi
398  <=> deg = 360*rad / 2*pi
399  <=> deg = 180*rad / pi
400  */
401 
402  return new Double( ( rad * 180.0 ) / const_pi );
403 }
404 
406 {
407  double deg;
408  if ( !getRealParam( 0, deg ) )
409  return new BError( "Invalid parameter type" );
410 
411  /*
412  2*pi radians <~> 360 degrees
413  <=> rad / 2*pi = deg / 360
414  <=> rad = 2*pi*deg / 360
415  <=> rad = deg * pi / 180
416  */
417  return new Double( ( deg * const_pi ) / 180.0 );
418 }
419 
421 {
422  double x;
423  if ( getRealParam( 0, x ) )
424  {
425  return new Double( ceil( x ) );
426  }
427  else
428  {
429  return new BError( "Invalid parameter type" );
430  }
431 }
433 {
434  double x;
435  if ( getRealParam( 0, x ) )
436  {
437  return new Double( floor( x ) );
438  }
439  else
440  {
441  return new BError( "Invalid parameter type" );
442  }
443 }
444 }
445 }
Bscript::BObjectImp * mf_Floor()
Definition: mathmod.cpp:432
Bscript::BObjectImp * mf_ATan()
Definition: mathmod.cpp:140
Bscript::BObjectImp * mf_Sqrt()
Definition: mathmod.cpp:178
Bscript::BObjectImp * mf_Sin()
Definition: mathmod.cpp:80
Bscript::BObjectImp * mf_Min()
Definition: mathmod.cpp:208
bool getRealParam(unsigned param, double &value)
Definition: execmodl.cpp:53
bool isa(BObjectType type) const
Definition: bobject.h:353
Bscript::BObjectImp * mf_DegToRad()
Definition: mathmod.cpp:405
Bscript::BObjectImp * mf_ConstPi()
Definition: mathmod.cpp:362
Bscript::BObjectImp * mf_ACos()
Definition: mathmod.cpp:128
bool getParam(unsigned param, int &value)
Definition: execmodl.cpp:62
Bscript::BObjectImp * mf_Abs()
Definition: mathmod.cpp:315
virtual BObjectImp * copy() const =0
Bscript::BObjectImp * mf_Cos()
Definition: mathmod.cpp:92
Bscript::BObjectImp * mf_ConstE()
Definition: mathmod.cpp:366
BObjectImp * getParamImp(unsigned param)
Definition: execmodl.cpp:22
Bscript::BObjectImp * mf_Max()
Definition: mathmod.cpp:257
Bscript::BObjectImp * mf_RadToDeg()
Definition: mathmod.cpp:389
unsigned char buffer[10000]
Definition: UoToolMain.cpp:109
Bscript::BObjectImp * mf_Pow()
Definition: mathmod.cpp:161
Bscript::BObjectImp * mf_Ceil()
Definition: mathmod.cpp:420
Bscript::BObjectImp * mf_ASin()
Definition: mathmod.cpp:116
Bscript::BObjectImp * mf_Tan()
Definition: mathmod.cpp:104
Bscript::BObjectImp * mf_FormatRealToString()
Definition: mathmod.cpp:371
initer _initer
Definition: mathmod.cpp:73
static double const_pi
Definition: mathmod.cpp:60
Bscript::BObjectImp * mf_Root()
Definition: mathmod.cpp:195
static double const_e
Definition: mathmod.cpp:61
Bscript::BObjectImp * mf_LogE()
Definition: mathmod.cpp:349
Bscript::BObjectImp * mf_Log10()
Definition: mathmod.cpp:337
MathExecutorModule(Bscript::Executor &exec)
Definition: mathmod.cpp:75
Definition: berror.cpp:12