Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add roundmultiple function #1170

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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;

Check warning on line 533 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L530-L533

Added lines #L530 - L533 were not covered by tests
};
if (element->GetNumElements() == 1)
Parameters.push_back(new aFunc<decltype(f), 1>(f, fdmex, element, Prefix, var));

Check warning on line 536 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L535-L536

Added lines #L535 - L536 were not covered by tests
seanmcleod marked this conversation as resolved.
Show resolved Hide resolved
else
Parameters.push_back(new aFunc<decltype(f), 2>(f, fdmex, element, Prefix, var));

Check warning on line 538 in src/math/FGFunction.cpp

View check run for this annotation

Codecov / codecov/patch

src/math/FGFunction.cpp#L538

Added line #L538 was not covered by tests
seanmcleod marked this conversation as resolved.
Show resolved Hide resolved
} 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
Loading