]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/math/FGFunction.cpp
Merge branch 'vivian/trainz'
[flightgear.git] / src / FDM / JSBSim / math / FGFunction.cpp
index 7a01df058f917d849a53e826fc6f68aee5ea0b46..e45e6d9aa23c1a14181e4a9371fb2f2f9ad9e44b 100755 (executable)
@@ -5,7 +5,7 @@ Author: Jon Berndt
 Date started: 8/25/2004
 Purpose: Stores various parameter types for functions
 
- ------------- Copyright (C) 2004  Jon S. Berndt (jsb@hal-pc.org) -------------
+ ------------- Copyright (C) 2004  Jon S. Berndt (jon@jsbsim.org) -------------
 
  This program is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the Free Software
@@ -28,12 +28,18 @@ Purpose: Stores various parameter types for functions
 INCLUDES
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
-#include <stdio.h>
-
+#include <sstream>
+#include <iomanip>
+#include <cstdlib>
+#include <cmath>
 #include "FGFunction.h"
 #include "FGTable.h"
 #include "FGPropertyValue.h"
 #include "FGRealValue.h"
+#include "input_output/FGXMLElement.h"
+#include "input_output/FGPropertyManager.h"
+
+using namespace std;
 
 namespace JSBSim {
 
@@ -44,14 +50,14 @@ static const char *IdHdr = ID_FUNCTION;
 CLASS IMPLEMENTATION
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
-FGFunction::FGFunction(FGPropertyManager* propMan, Element* el, string prefix)
+FGFunction::FGFunction(FGPropertyManager* propMan, Element* el, const string& prefix)
                                       : PropertyManager(propMan), Prefix(prefix)
 {
   Element* element;
   string operation, property_name;
-  int size = el->GetNumElements();
   cached = false;
   cachedValue = -HUGE_VAL;
+  invlog2val = 1.0/log10(2.0);
 
   property_string = "property";
   value_string = "value";
@@ -68,6 +74,9 @@ FGFunction::FGFunction(FGPropertyManager* propMan, Element* el, string prefix)
   quotient_string = "quotient";
   pow_string = "pow";
   exp_string = "exp";
+  log2_string = "log2";
+  ln_string = "ln";
+  log10_string = "log10";
   abs_string = "abs";
   sin_string = "sin";
   cos_string = "cos";
@@ -99,6 +108,12 @@ FGFunction::FGFunction(FGPropertyManager* propMan, Element* el, string prefix)
     Type = eQuotient;
   } else if (operation == pow_string) {
     Type = ePow;
+  } else if (operation == log2_string) {
+    Type = eLog2;
+  } else if (operation == ln_string) {
+    Type = eLn;
+  } else if (operation == log10_string) {
+    Type = eLog10;
   } else if (operation == abs_string) {
     Type = eAbs;
   } else if (operation == sin_string) {
@@ -170,6 +185,9 @@ FGFunction::FGFunction(FGPropertyManager* propMan, Element* el, string prefix)
                operation == quotient_string ||
                operation == pow_string ||
                operation == exp_string ||
+               operation == log2_string ||
+               operation == ln_string ||
+               operation == log10_string ||
                operation == abs_string ||
                operation == sin_string ||
                operation == cos_string ||
@@ -255,6 +273,18 @@ double FGFunction::GetValue(void) const
   case eExp:
     temp = exp(temp);
     break;
+  case eLog2:
+    if (temp > 0.00) temp = log10(temp)*invlog2val;
+    else temp = -HUGE_VAL;
+    break;
+  case eLn:
+    if (temp > 0.00) temp = log(temp);
+    else temp = -HUGE_VAL;
+    break;
+  case eLog10:
+    if (temp > 0.00) temp = log10(temp);
+    else temp = -HUGE_VAL;
+    break;
   case eAbs:
     temp = fabs(temp);
     break;
@@ -320,39 +350,10 @@ double FGFunction::GetValue(void) const
 
 string FGFunction::GetValueAsString(void) const
 {
-  char buffer[20];
-  string value;
-
-  sprintf(buffer,"%9.6f",GetValue());
-  value = string(buffer);
-  return value;
-}
-
-//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-double FGFunction::GaussianRandomNumber(void) const
-{
-  static double V1, V2, S;
-  static int phase = 0;
-  double X;
-
-  if (phase == 0) {
-    do {
-      double U1 = (double)rand() / RAND_MAX;
-      double U2 = (double)rand() / RAND_MAX;
+  ostringstream buffer;
 
-      V1 = 2 * U1 - 1;
-      V2 = 2 * U2 - 1;
-      S = V1 * V1 + V2 * V2;
-    } while(S >= 1 || S == 0);
-
-      X = V1 * sqrt(-2 * log(S) / S);
-  } else
-    X = V2 * sqrt(-2 * log(S) / S);
-
-  phase = 1 - phase;
-
-  return X;
+  buffer << setw(9) << setprecision(6) << GetValue();
+  return buffer.str();
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -360,7 +361,12 @@ double FGFunction::GaussianRandomNumber(void) const
 void FGFunction::bind(void)
 {
   if ( !Name.empty() ) {
-    string tmp = PropertyManager->mkPropertyName(Prefix + Name, false); // Allow upper case
+    string tmp;
+    if (Prefix.empty())
+      tmp  = PropertyManager->mkPropertyName(Name, false); // Allow upper case
+    else
+      tmp  = PropertyManager->mkPropertyName(Prefix + "/" + Name, false); // Allow upper case
+
     PropertyManager->Tie( tmp, this, &FGFunction::GetValue);
   }
 }