You are not currently logged in. Are you accessing the unsecure (http) portal? Click here to switch to the secure portal. |
Template:Evalns
Contents
Purpose
EVALuate, but No Scientific notation.
This template can be used to evaluate an expression, if the result has to be suitable as input for further expressions.
Returns
If the output is not valid as input, the expression is just reproduced, enclosed in parentheses. Otherwise it returns the number returned in the evaluation of the specified expression.
So this template just has the same effect as calling {{#expr: ...}}
directly, except that parentheses will appear around the source expression if that expression cannot be evaluated (for example a missing parameter value without a default), avoiding the error message generated by {{#expr: ...}}
.
{{evalns|1 + {{evalns|1234567890*{{{missingparameter}}}*1234567890}}}}
gives (1 + (1234567890*{{{missingparameter}}}*1234567890)).{{#expr:1 + {{#expr:1234567890*{{{missingparameter}}}*1234567890}}}}
gives Expression error: Unexpected < operator.
Examples
{{evalns|3*4}}
gives 12.{{evalns|1234567890*12}}
gives 14814814680.{{evalns|1234567890*1234567890}}
gives 1.5241578750191E+18.
Note: the scientific notation of result numbers are no longer a problem when these results are further inserted in other expressions. So this version no longer invalidates numbers in scientific notations, as they are directly evaluatable. The protection of parentheses is not even needed, due to numeric operator precedences for the "e" operator (which multiplies a number by a power of 10 given by a (possibly negative) number in its second operand:
{{evalns|1 + {{evalns|1234567890*1234567890}}}}
gives 1.5241578750191E+18.{{evalns|{{evalns|1234567890*1234567890}} + 1}}
gives 1.5241578750191E+18.{{evalns|2 * {{evalns|1234567890*1234567890}}}}
gives 3.0483157500382E+18.{{evalns|{{evalns|1234567890*1234567890}} + 1}}
gives 3.0483157500382E+18.
Using {{#expr: ...}}
directly produces the same result :
{{#expr:1 + {{#expr:1234567890*1234567890}}}}
gives 1.5241578750191E+18.{{#expr:{{#expr:1234567890*1234567890}} + 1}}
gives 1.5241578750191E+18.{{#expr:2 * {{#expr:1234567890*1234567890}}}}
gives 3.0483157500382E+18.{{#expr:{{#expr:1234567890*1234567890}} + 1}}
gives 3.0483157500382E+18.
Source code
(simplified and formatted, removing the "subst" parameter stuff which is also included)
{{#if: {{#ifexpr: {{#expr: {{{1}}} }} }} | ({{{1}}}) | {{#expr: {{{1}}} }} }}
This first tries to evaluate the expression, which may return either a number, or a special value like INF or NAN, or an error message. Then the #ifexpr reevaluates this string : only numbers will pass, but as there's no return expression, an empty string will be returned ; if there's an evaluation error (including when evaluating INF or NAN values), only an error message will be regenerated. Finally the #if just test for the presence or not of the error message. If there's one, the parameter 1 could not be evaluated to a finite number, so the parameter is returned between parentheses ; otherwise, the parameter is reevaluted to return its value
Note that if the expression in parameter 1 is just a simple number, it will be with those zeroes stripped, and exponents will be normalized : "{{#expr:1.50}}" = "1.5", "{{#expr:.00001}}" = "1.0E-5". Numbers are returned with at most 13 significant digits, either as integers or fractional numbers if possible, but using normalized the exponential notation for all numbers whose absolute value is lower than 0.0001 or higher than or equal to 1.0E+15.
The above documentation is transcluded from Template:Evalns/doc. (edit | history) Editors can experiment in this template's sandbox (create | mirror) and testcases (create) pages. Please add categories and interwikis to the /doc subpage. Subpages of this template. |