This site is supported by donations to The OEIS Foundation.

Template:Expr

From OeisWiki
Jump to navigationJump to search

The {{expr}} mathematical function template evaluates an expression yielding a numeric result (although an undefined or empty string parameter/subparameter evaluates as an empty string), otherwise an error message is returned. The expression may be an anonymous function (similar to, though not quite a lambda expression) using up to eight subparameters, all of which must be yielding real numeric values.

Usage

[edit]
{{expr|expression}}

or

{{expr|anonymous function (using <<<1>>> up to <<<8>>> to refer to up to eight numeric values)|up to eight numeric values separated by semicolons}}

where in either expression or anonymous function we are allowed to use:

  • either log or ln (whereas in {{#expr: ... }} one may only use ln for the natural logarithm);
  • &minus; or - (whereas in {{#expr: ... }} one may only use -, i.e. an hyphen, for the minus sign).

Examples

[edit]

Examples with valid input

[edit]

Note: an undefined or empty string parameter/subparameter evaluates as an empty string.

Code Result
{{expr}}
{{expr|}}
{{expr|-1}} −1
{{expr|{{op|-}}1}} −1
{{expr|-0}} −0
{{expr|-(5 - 5)}} −0
{{expr|{{op|-}}(5 {{op|-}} 5)}} −0
{{expr|123/100}} 1.23
{{expr|1 + 9}} 10
{{expr|not 10}} 0
{{expr|(7 <= 8) * 9}} 9
{{expr|2 + 3}} 5
{{expr|0^(-1)}} INF
{{expr|-0^(-1)}} −INF
{{expr|(-0)^(-1)}} −INF
{{expr|-(0^(-1))}} −INF
{{expr|0^0}} 1
{{expr|0^1}} 0
{{expr|5^2 * 7 * 13 * 17}} 38675
{{expr|cos(pi/3)}} 0.5
{{expr|ln (e^2)}} 2
{{expr|log (e^2)}} 2
{{expr|log log (e^(e^2))}} 2

Examples using the {{math}} template

[edit]

The code

{{math| 0.5 {{-)|u&u}} 3 {{=}} 0.5{{^|0.5{{^|0.5}}}} {{=}} <!-- -->{{expr| 0.5^( 0.5^( 0.5 ) ) }}{{...|ldots}} |&&}}

yields the HTML+CSS formatted

0.5 ⇈ 3 = 0.5 0.5 0.5 = 0.61254732653607

The code

{{math| 0.5 {{-)|u&u|tex}} 3 {{=}} 0.5{{^|0.5{{^|0.5|tex}}|tex}} {{=}} <!-- -->{{expr| 0.5^( 0.5^( 0.5 ) ) }}{{...|ldots|tex}} |$$}}

yields the LaTeX formatted

0.53=0.50.50.5=0.61254732653607

Examples with invalid input

[edit]
Code Result
{{expr|1/0}} Division by zero.
{{expr|0/0}} Division by zero.
{{expr|6,500}} Expression error: Unrecognized punctuation character ",".
{{expr|- one}} Expression error: Unrecognized word "one".
{{expr|3 blobs}} Expression error: Unrecognized word "blobs".
{{expr|INF}} Expression error: Unrecognized word "inf".
{{expr|NAN}} Expression error: Unrecognized word "nan".

Formatted numbers

[edit]

This template requires unformatted numbers, it will not recognize formatted numbers, e.g. comma separated, which is by design since formatted numbers will break expression parsers. To remove the formatting from a number, you can wrap the number first in {{formatnum:number|R}}.[1]

Code Result
{{expr|1,000.50}} Expression error: Unrecognized punctuation character ",".
{{expr|{{formatnum:1,000.50|R}}}} 1000.5
{{formatnum:{{expr|{{formatnum:1,000.50|R}}}}}} 1,000.5

Examples with anonymous functions

[edit]
Code Result
{{expr| <<<1>>>^2 | 3 + 4 }} 49
{{expr| <<<1>>> + <<<2>>> | 3; 4 }} 7
{{expr| <<<1>>>^<<<2>>> | 3; 4 }} 81
{{expr| <<<1>>>^<<<2>>> | 5 - 2; 2 + 2 }} 81
{{expr| <<<2>>>^<<<1>>> | 3; 4 }} 64
{{expr| <<<1>>>^2 + <<<2>>>^2 | 3; 4 }} 25
{{expr| 5 * <<<1>>> + <<<2>>> | 3; 4 }} 19
{{expr| {{phi}}^<<<1>>> | 2 }} 2.6180339887499
{{expr| <<<1>>>^<<<2>>> | {{phi}}; 2 }} 2.6180339887499
{{expr| cos <<<1>>> | pi/3 }} 0.5
{{expr| sin <<<1>>> + cos <<<2>>> | pi/6 ; pi/3 }} 1
{{expr| ln <<<1>>> | e^2 }} 2
{{expr| log <<<1>>> | e^2 }} 2
{{expr| log log <<<1>>> | e^(e^2) }} 2
{{expr| log <<<1>>> + log log <<<2>>> | e^2 ; e^(e^2) }} 4
{{expr| <<<1>>>^<<<3>>> + <<<2>>>^<<<3>>> | sin(pi/6); cos(pi/6); 2 }} 1
{{expr| (sin <<<1>>>)^<<<2>>> + (cos <<<1>>>)^<<<2>>> | pi/6; 2 }} 1
{{expr| <<<1>>>+<<<2>>>+<<<3>>>+<<<4>>>+<<<5>>>+<<<6>>>+<<<7>>>+<<<8>>> | 1; 20; 300; 4000; 50000; 600000; 7000000; 80000000 }} 87654321

Examples with anonymous functions and anonymous subfunctions

[edit]

The code

: {{expr| <<<1>>>^2 + <<<2>>>^2 | 
  sin <<<3>>> ; 
  cos <<<3>>> ; 
  pi/3 
}}

yields

1

The code

: {{expr| <<<1>>> * <<<2>>> + <<<1>>> * <<<3>>> + <<<2>>> * <<<3>>> | 
  <<<4>>> + 1 ; 
  <<<4>>>^2 + 1 ; 
  <<<4>>>^3 + 1 ;
  5 - 2   
}}

yields (<<<1>>> is 4, <<<2>>> is 10, <<<3>>> is 28, so we get 4 * 10 + 4 * 28 + 10 * 28 = 432)

432

Anonymous functions that don't work

[edit]
Code Result Comment
{{expr| <<<1>>>(<<<2>>>) | cos; pi/3 }}      Expression error: Missing operand for cos.      Since each subparameter is parenthesized, we get (cos)(pi/3).
{{expr| {{cos|<<<1>>>}} | pi/3 }} Expression error: Unexpected < operator. The {{cos}} template is called before <<<1>>> is replaced by pi/3.

Test

[edit]

The code

{{expr| {{#replace: {{#replace: <<<1>>> |<< | {{((}} }} | >>| {{))}} }} | << Fibonacci {{!}} <<<2>>> >> ; 10 }} 

yields

Expression error: Unrecognized punctuation character "{".

Maximum and minimum numbers allowed

[edit]

Testing for maximum and minimum numbers allowed with {{expr}}

Code Result Code Result
{{expr|{{formatnum:99,999,999,999,999|R}}}} 99999999999999   {{expr|{{formatnum:-99,999,999,999,999|R}}}} −99999999999999
{{expr|{{formatnum:999,999,999,999,999|R}}}} 1.0E+15   {{expr|{{formatnum:-999,999,999,999,999|R}}}} −1.0E+15
{{expr|2^1023}} 8.9884656743116E+307   {{expr|2^(-1023)}} 1.1125369292536E−308
{{expr|(2*0.999999999999999)*(2^1023)}} 1.7976931348623E+308   {{expr|1 / ((2*0.999999999999999)*(2^1023))}} 5.562684646268E−309
{{expr|2^1024 - 1}} INF   {{expr|1 / (2^1024 -1)}} 0
{{expr|2^1024}} INF   {{expr|2^(-1024)}} 5.562684646268E−309
{{expr|2^(-1025)}} 2.781342323134E−309   {{expr|2^(-1026)}} 1.390671161567E−309
{{expr|2^(-1027)}} 6.953355807835E−310   {{expr|2^(-1028)}} 3.4766779039175E−310
{{expr|2^(-1029)}} 1.7383389519588E−310   {{expr|2^(-1074)}} 4.9406564584125E−324
{{expr|2^(-1075)}} 0   {{expr|2^(-2048)}} 0
{{expr|2^1024}} INF   {{expr|-(2^(2^1024))}} −INF
{{expr|2^1024 - 2^1024}} NAN   {{expr|(2^1024 - 1) / (2^1024 - 1)}} NAN
{{expr|(2^1024)^0}} 1   {{expr|(2^1024)^-(2^1024)}} 0
{{expr|(2^1024 - 1) + ((2^1024 - 1) / (2^1024 - 1))}} NAN

Code

[edit]
<noinclude>{{Documentation}}<!--

  Inside {{#expr: ... }} we must:

  * remove "operator space" annotations (which are for the {{math}} template): @opsp@
  * convert log to ln  
  * convert {{sym|-}}, i.e. &minus;, into a hyphus -, i.e. either a hyphen or minus (depending on context)    

--></noinclude><includeonly>{{#replace: {{#expr: 
{{#replace: 
  {{#replace: 
    {{#replace:
      {{#replace:
        {{#replace:
          {{#replace:
           {{#replace:
              {{#replace:
                {{#replace:
                  {{#replace: 
                    {{#replace: {{{1|}}} |@opsp@|}}
                  |<<<1>>>|({{#explode:{{{2|}}}|;|0}})
                  }}
                |<<<2>>>|({{#explode:{{{2|}}}|;|1}})
                }}
              |<<<3>>>|({{#explode:{{{2|}}}|;|2}})
              }}
            |<<<4>>>|({{#explode:{{{2|}}}|;|3}})
            }}
          |<<<5>>>|({{#explode:{{{2|}}}|;|4}})
          }}
        |<<<6>>>|({{#explode:{{{2|}}}|;|5}})
        }}
      |<<<7>>>|({{#explode:{{{2|}}}|;|6}})
      }}
    |<<<8>>>|({{#explode:{{{2|}}}|;|7}})
    }}
  | log | ln }}
| {{sym|-}} |-}}  
}} |-| {{sym|-}} }}</includeonly>

See also

[edit]

Notes

[edit]
[edit]