Expressions
| This article is a stub. You can help the wiki by expanding it. |
Expressions (or SGExpressions) are a feature of the SimGear library and provide a nice way of implementing complex math formulas using XML syntax.
They are supported in many systems within the FlightGear code.
| Caution Expressions do not check if your math creates floating point exceptions (like division by zero conditions, taking the square root of a negative number, etc.). This can cause undefined behavior and may result in NaNs or even Cascading NaNs. |
Usage
Expressions are supported in
- Autopilot configuration files
- Particle system configuration files
- Animations (translate, rotate, scale, range, blend)
- The shader technique
- Conditions
Sample Expressions
This is a sample expression for c = sqrt(a*a + b^2). Children/arguments are parsed in the order they appear in in the file (or the order in which they are set via property methods).
<expression>
<sqrt>
<sum>
<product>
<property>/value/a</property>
<property>/value/a</property>
</product>
<pow>
<property>/value/b</property>
<value>2</value>
</pow>
</sum>
</sqrt>
</expression>
Supported elements
<abs> <!-- also: fabs -->
<acos>
<asin>
<atan>
<atan2>
<ceil>
<clip> <!-- <clipMin> <clipMax> -->
<cos>
<cosh>
<difference> <!-- also: dif -->
<div>
<exp>
<floor>
<log>
<log10>
<max>
<min>
<mod>
<pow>
<product> <!-- also: prod -->
<property> <!-- Unlike elsewhere, 'prop' does not work in expressions. -->
<rad2deg>
<deg2rad>
<sin>
<sinh>
<sqr>
<sqrt>
<sum>
<table> <!-- <entry><ind> value </ind><dep> value </dep></entry> -->
<tan>
<tanh>
<value>
Table
The table expression uses the following syntax, similarly to the <interpolation> element:
<table>
<!-- put an input element here, this can be <property> or any other operation -->
<entry>
<ind>0.0</ind> <!-- the value of the input (independent value) -->
<dep>0.0</dep> <!-- the value of the output (dependent value) -->
</entry>
... <!-- you can put as many entries as you want -->
</table>
The entries must be sorted by the independent (input) variable strictly ascending. If input is outside the range of the table, output will be determined from the first / last table row.
Example
The table:
| 0.0 | 5.0 |
| 0.5 | 7.5 |
| 0.9 | -5.0 |
| 1.0 | 25.0 |
translates to the following code:
<table>
<!-- put an input element here, this can be a <property> or any other operation -->
<entry>
<ind>0.0</ind>
<dep>5.0</dep>
</entry>
<entry>
<ind>0.5</ind>
<dep>7.5</dep>
</entry>
<entry>
<ind>0.9</ind>
<dep>-5.0</dep>
</entry>
<entry>
<ind>1.0</ind>
<dep>25.0</dep>
</entry>
</table>
Quantize continuous input values with nearest-match mode
| Caution This feature was added to the development branch (aka 'next') in 01/2026. After some more testing, it may be backported or just become part of the next release. |
As an alternative to interpolating, the <table> expression can also search the nearest match for an input and return a fixed value. This feature was developed to transform a continuous joystick axis reading from a throttle quadrant into discrete flaps setting numbers.
The discrete return values have to be written into the <dep> elements, the <ind> elements mark the reference input value (e.g. js axis reading for a certain flap setting). The <ind> values must be sorted and no duplicates are allowed.
The output will change "half way" between two <ind> values.
<expression>
<table>
<!-- event input goes here -->
<property></property>
<mode>nearest</mode>
<entry>
<ind>0</ind>
<dep>30</dep>
</entry>
<entry>
<ind>0.12</ind>
<dep>25</dep>
</entry>
<entry>
<ind>0.30</ind>
<dep>20</dep>
</entry>
<entry>
<ind>0.48</ind>
<dep>15</dep>
</entry>
<entry>
<ind>0.66</ind>
<dep>5</dep>
</entry>
<entry>
<ind>0.83</ind>
<dep>1</dep>
</entry>
<entry>
<ind>1.0</ind>
<dep>0</dep>
</entry>
</table>
</expression>
Clip
An expression to limit e.g. rotation:
<expression>
<clip>
<clipMin>-45</clipMin>
<clipMax>45</clipMax>
<property>orientation/pitch-deg</property>
</clip>
</expression>
Hints and tips
Rounding
While there is no element for rounding, this workaround can be used for that:
<expression>
<floor>
<sum>
<property>your/property/here</property>
<value>0.5</value>
</sum>
</floor>
</expression>