Example: Computing Factorials
Description
This example serves to illustrate how complex operations can be built using the basics. Using <fact> would of course evaluate faster.
To run this example, pass a positive long value to the evaluator:
$ java -jar dist/xmlmath-1.1-SNAPSHOT.jar 4 < samples/factorial.xml 24 $
Note
Remember: xmlmath uses 64-bit, signed integers, making 263 - 1 the biggest
achievable number. This is not large enough to hold the result of factorials
larger than 20!
Expression
<expression xmlns="http://xmlmath.org/1.0">
<inputLong name="x"/>
<choose>
<if>
<lessThan>
<linkLong name="x"/>
<long value="0"/>
</lessThan>
</if>
<then>
<fail message="Cannot compute the factorial of a negative number."/>
</then>
<else>
<listProduct datatype="long">
<for iterator="i">
<long value="0"/>
<linkLong name="x"/>
<add datatype="long">
<linkLong name="i"/>
<long value="1"/>
</add>
</for>
</listProduct>
</else>
</choose>
</expression>
|

