Skip to content

Commit

Permalink
Add roundmultiple function (JSBSim-Team#1170)
Browse files Browse the repository at this point in the history
Co-authored-by: Bertrand Coconnier <[email protected]>
  • Loading branch information
seanmcleod and bcoconni authored Oct 9, 2024
1 parent a41349f commit 5979050
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
11 changes: 10 additions & 1 deletion JSBSim.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,7 @@
<xs:element name="random"/>
<xs:element name="urandom"/>
<xs:element name="pi"/>
<xs:element name="roundmultiple"/>
<!--xs:element ref="rotation_alpha_local" /-->
<!--xs:element ref="rotation_beta_local" /-->
<!--xs:element ref="rotation_gamma_local" /-->
Expand Down Expand Up @@ -1625,7 +1626,15 @@
</xs:complexType>
</xs:element>
<xs:element name="value" type="xs:double" />

<xs:element name="roundmultiple">
<xs:complexType>
<xs:choice maxOccurs="2" minOccurs="1">
<xs:group ref="func_group" />
<xs:element ref="value" />
<xs:element ref="property" />
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="mod">
<xs:complexType>
<xs:choice maxOccurs="2" minOccurs="2">
Expand Down
9 changes: 9 additions & 0 deletions src/math/FGFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,15 @@ void FGFunction::Load(Element* el, FGPropertyValue* var, FGFDMExec* fdmex,
return y != 0.0 ? fmod(p[0]->GetValue(), y) : HUGE_VAL;
};
Parameters.push_back(new aFunc<decltype(f), 2>(f, fdmex, element, Prefix, var));
} else if (operation == "roundmultiple") {
auto f = [](const decltype(Parameters)& p)->double {
double multiple = p.size() == 1 ? 1.0 : p[1]->GetValue();
return round((p[0]->GetValue() / multiple)) * multiple;
};
if (element->GetNumElements() == 1)
Parameters.push_back(make_MathFn(round, fdmex, element, Prefix, var));
else
Parameters.push_back(new aFunc<decltype(f), 1>(f, fdmex, element, Prefix, var, 2));
} else if (operation == "atan2") {
auto f = [](const decltype(Parameters)& p)->double {
return atan2(p[0]->GetValue(), p[1]->GetValue());
Expand Down
10 changes: 10 additions & 0 deletions src/math/FGFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ A function definition consists of an operation, a value, a table, or a property
- floor (takes 1 arg)
- ceil (takes 1 arg)
- fmod (takes 2 args)
- roundmultiple (takes 2 args)
- lt (less than, takes 2 args)
- le (less equal, takes 2 args)
- gt (greater than, takes 2 args)
Expand Down Expand Up @@ -511,6 +512,15 @@ refers to one or more instances of a property, value, or table.
</fmod>
@endcode
Example: fmod(18.5, 4.2) evaluates to 1.7
- @b roundmultiple returns the floating-point rounding of X to a multiple of M.
round(X/M) * M
@code
<roundmultiple>
{property, value, table, or other function element}
{property, value, table, or other function element}
</roundmultiple>
@endcode
Example: roundmultiple(93.43, 5.0) evaluates to 95.0
- @b lt returns a 1 if the value of the first immediate child element is less
than the value of the second immediate child element, returns 0
otherwise
Expand Down

0 comments on commit 5979050

Please sign in to comment.