Skip to content
forked from JSBSim-Team/jsbsim

Commit 0d8e6e9

Browse files
seanmcleodbcoconni
andcommitted
Add roundmultiple function (JSBSim-Team#1170)
Co-authored-by: Bertrand Coconnier <[email protected]>
1 parent 8da2336 commit 0d8e6e9

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

JSBSim.xsd

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,7 @@
14261426
<xs:element name="random"/>
14271427
<xs:element name="urandom"/>
14281428
<xs:element name="pi"/>
1429+
<xs:element name="roundmultiple"/>
14291430
<!--xs:element ref="rotation_alpha_local" /-->
14301431
<!--xs:element ref="rotation_beta_local" /-->
14311432
<!--xs:element ref="rotation_gamma_local" /-->
@@ -1625,7 +1626,15 @@
16251626
</xs:complexType>
16261627
</xs:element>
16271628
<xs:element name="value" type="xs:double" />
1628-
1629+
<xs:element name="roundmultiple">
1630+
<xs:complexType>
1631+
<xs:choice maxOccurs="2" minOccurs="1">
1632+
<xs:group ref="func_group" />
1633+
<xs:element ref="value" />
1634+
<xs:element ref="property" />
1635+
</xs:choice>
1636+
</xs:complexType>
1637+
</xs:element>
16291638
<xs:element name="mod">
16301639
<xs:complexType>
16311640
<xs:choice maxOccurs="2" minOccurs="2">

src/math/FGFunction.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ void FGFunction::Load(Element* el, FGPropertyValue* var, FGFDMExec* fdmex,
527527
return y != 0.0 ? fmod(p[0]->GetValue(), y) : HUGE_VAL;
528528
};
529529
Parameters.push_back(new aFunc<decltype(f), 2>(f, fdmex, element, Prefix, var));
530+
} else if (operation == "roundmultiple") {
531+
auto f = [](const decltype(Parameters)& p)->double {
532+
double multiple = p.size() == 1 ? 1.0 : p[1]->GetValue();
533+
return round((p[0]->GetValue() / multiple)) * multiple;
534+
};
535+
if (element->GetNumElements() == 1)
536+
Parameters.push_back(make_MathFn(round, fdmex, element, Prefix, var));
537+
else
538+
Parameters.push_back(new aFunc<decltype(f), 1>(f, fdmex, element, Prefix, var, 2));
530539
} else if (operation == "atan2") {
531540
auto f = [](const decltype(Parameters)& p)->double {
532541
return atan2(p[0]->GetValue(), p[1]->GetValue());

src/math/FGFunction.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ A function definition consists of an operation, a value, a table, or a property
8888
- floor (takes 1 arg)
8989
- ceil (takes 1 arg)
9090
- fmod (takes 2 args)
91+
- roundmultiple (takes 2 args)
9192
- lt (less than, takes 2 args)
9293
- le (less equal, takes 2 args)
9394
- gt (greater than, takes 2 args)
@@ -511,6 +512,15 @@ refers to one or more instances of a property, value, or table.
511512
</fmod>
512513
@endcode
513514
Example: fmod(18.5, 4.2) evaluates to 1.7
515+
- @b roundmultiple returns the floating-point rounding of X to a multiple of M.
516+
round(X/M) * M
517+
@code
518+
<roundmultiple>
519+
{property, value, table, or other function element}
520+
{property, value, table, or other function element}
521+
</roundmultiple>
522+
@endcode
523+
Example: roundmultiple(93.43, 5.0) evaluates to 95.0
514524
- @b lt returns a 1 if the value of the first immediate child element is less
515525
than the value of the second immediate child element, returns 0
516526
otherwise

0 commit comments

Comments
 (0)