烂好人什么意思| 肾结石可以喝什么饮料| 什么手机最贵| abs材质是什么| 眼睛的睛可以组什么词| 手上长汗疱疹用什么药| 12378是什么电话| 血糖高能吃什么| 爷爷的妹妹叫什么| 狗狗为什么会咬人| 脚气用什么药| 树脂是什么材质| 二月一号是什么星座| 杂面是什么面| 什么洗面奶好用| 维生素d3和d2有什么区别| 什么大米好吃| 1年是什么朝代| 梦见狗咬我是什么意思| 什么的回答| 龙的三合生肖是什么| 老是咳嗽挂什么科| 湿气重是什么原因造成的| 宫颈息肉有什么症状| 孕妇贫血吃什么药| 耳朵后面是什么穴位| 什么叫更年期| 山珍海味是什么意思| cool什么意思中文| 准确值是什么意思| 去韩国需要办理什么手续| 盲肠憩室是什么意思| 槊是什么意思| 湿气重会有什么症状| 1939年属什么生肖| 黄瓜不能和什么一起吃| 体内湿气重吃什么药| 竹者念什么| 什么是扁平疣| 叶公好龙是什么故事| 头发硬是什么原因| 感冒吃什么水果好得快| 三叉神经痛有什么症状| 一个尔一个玉念什么| 什么是主动脉夹层| 布洛芬什么时候起效| 低压高是什么原因| 身是什么结构| 皮肤湿疹用什么药膏| 甲类传染病指什么| 吃什么水果养胃| 清炖排骨放什么调料| 脚臭用什么药最好| 血糖高能吃什么食物| 喉咙痛是什么原因| 等离子是什么| 处女是什么象星座| 什么才是真正的爱情| 白玫瑰代表什么| 鱼石是什么| 2021年五行属什么| 夏天刮什么风| 上呼吸道感染吃什么药| bq是什么意思啊| 春节在什么时候| 多囊卵巢综合征是什么意思| 死缓什么意思| 为什么睡觉老是流口水| 眼睛干涩吃什么中成药| 梦见楼塌了是什么意思| 李白是什么| 吸入甲醛会有什么症状| 凿壁偷光告诉我们什么道理| 蒲公英和什么搭配最好| 定性和定量是什么意思| 脑瘫是什么原因引起的| 后下药什么时候下| 换肾是什么病| 痰是棕色的是什么原因| 五十知天命什么意思| 灵魂伴侣什么意思| 桃子不能和什么水果一起吃| 腰肌劳损用什么药最好| 两性关系是什么意思| 牛肉和什么菜包饺子好吃| 什么叫窝沟封闭| 手脚出汗多是什么原因| m2是什么单位| 财年是什么意思| 异常子宫出血是什么原因| 前戏是什么| 舌苔发白是什么原因引起的| 孙字五行属什么| 脚环肿是什么原因引起的| 尿道口流白色液体是什么病| 什么茶是绿茶| 梦到人死了是什么意思| 什么食物好消化| 耳朵烫是什么预兆| 经常自言自语是什么原因| 口服是什么意思| 肠易激综合征是什么病| 急性扁桃体发炎吃什么药| 什么美白效果最好最快| 洗衣机什么牌子好| 去冰和常温有什么区别| 梦见给别人理发是什么意思| 小水母吃什么| 官宣是什么意思| hr医学上是什么意思| 姑奶奶的老公叫什么| 公安局大队长是什么级别| 不妄作劳什么意思| 苦不堪言是什么意思| 盼头是什么意思| 商朝后面是什么朝代| 阴道壁是什么样的| 口腔溃疡吃什么好| 脑梗是什么病严重吗| 缘木求鱼什么意思| 甘油三酯高会引起什么病| 咽炎咳嗽吃什么药| 知天命是什么意思| 高碳钻是什么| 外传是什么意思| 子宫切除后要注意什么| 胃出血有什么症状表现| 同房痛什么原因引起的| 宫颈小有什么影响| 振五行属什么| fresh是什么意思| 山竹什么时候吃是应季| 黄晓明的老婆叫什么名字| 男性腰疼挂什么科| 肾结石发作有什么症状| 海龟汤是什么| 痛风有什么症状| 背疼应该挂什么科| vte是什么| 从未是什么意思| 讨好的笑是什么笑| 肺气肿是什么症状| 减肥吃什么油| 镇关西是什么意思| 9月10日是什么日子| 人活一辈子到底为了什么| 什么叫阴虚什么叫阳虚| 一岁半宝宝反复发烧是什么原因| yp是什么| 鸡和什么属相相冲| 建兰什么时候开花| 邓超属什么生肖| 类风湿吃什么食物好| 步步高升是什么意思| 男士检查精子挂什么科| 盆腔少量积液什么意思| 脉管炎吃什么药最好| 刚怀孕有什么办法打掉| 莲蓬乳是什么| 拔罐什么时候拔最好| 小孩一到晚上就发烧是什么原因| 性激素是查什么| 中耳炎是什么症状| 6.29是什么星座| 婴儿外阴粘连挂什么科| 手足口疫苗叫什么名字| 宽字五行属什么| 豌豆荚是什么| 铄字五行属什么| 请自重是什么意思| 什么病不能吃竹笋| 男人喝红糖水有什么好处| 柳絮是什么| 扁桃体挂什么科| 月经血黑是什么原因| 什么是盗汗症状| 肠道门诊看什么病| 神经元特异性烯醇化酶偏高是什么意思| 1700年是什么朝代| 降头术是什么| tpa是什么意思| 素数是什么| 肖想是什么意思| 高硼硅是什么材质| 是什么原因导致肥胖| 吐黑水是什么原因| 戾气什么意思| 20点是什么时辰| 什么是原发性高血压和继发性高血压| 骨骼闭合了还有什么办法可以长高| 空你几哇什么意思| 甲状腺结节有什么症状| 94属什么生肖| 斋醮什么意思| 雀的偏旁是什么| 混油皮是什么特征| 病毒为什么会变异| 猫癣用什么药| 营养师是干什么的| 梦到狗是什么征兆| 1.19是什么星座| 天朝是什么意思| 龙跟什么生肖配对最好| 认栽是什么意思| 法官是干什么的| 飞黄腾达是什么生肖| 心电轴右偏是什么意思| 什么叫人工智能| 真丝棉是什么面料| 腋下痛是什么病| 做生化是检查什么的| 冰箱什么品牌最好| 银子发黑是什么原因| 乳腺结节是什么病| 嘴上有痣代表什么| 央行放水是什么意思| 精子是什么颜色| 什么呀| 21三体高风险是什么原因造成的| 疖是什么意思| 中核集团是什么级别| 为什么会下雨| 小孩打嗝是什么原因| acth是什么激素| 为什么经常刷牙还牙黄| 腰脱是什么症状| 手指甲软薄是缺什么| 血沉高是什么意思| 今天什么日子| 女人喜欢什么类型男人| 澳门是什么时候被葡萄牙占领的| 大姨妈能吃什么水果| 乌鸦长什么样| 明信片是什么| 何德何能是什么意思| 猫摇尾巴是什么意思| 胆结石不能吃什么东西| 怀孕血压高对胎儿有什么影响| 棍子鱼又叫什么鱼| 罗勒叶在中国叫什么| 不来例假也没怀孕是什么原因| 宫颈癌早期什么症状| 超敏c反应蛋白正常说明什么| 散光跟近视有什么区别| 什么是丹毒| 怀孕分泌物是什么样的| 缩阳是什么意思| 1988是什么年| 闺蜜是什么样的关系| rh血型阳性是什么意思| 胃镜挂什么科| 堃字的寓意是什么意思| 为什么吃火龙果会拉肚子| 月亮是什么意思| 猪心炖什么补气补血| ipa是什么意思| 为什么睡不着| 丽珠兰是什么| s和m是什么意思啊| 谨守是什么意思| 三叉神经痛吃什么药| 吃什么补阴虚最好| 6月23号是什么日子| 什么验孕棒准确率高| 百度Jump to content

融入国家“一带一路”战略,构造辽宁综合交通运输...

From mediawiki.org
百度   湖南大学经贸学院教授陈乐一认为,改革开放以来的实践证明,经济体制改革力度越大,越能减缓经济周期波动,从而促进经济的平稳较快发展。

The #expr and #ifexpr functions of MediaWiki's ParserFunctions extension evaluate mathematical and Boolean expressions—that is, expressions involving numbers and Boolean values (true and false). It does not work with arbitrary strings, as do most of the other parser functions.

The syntax of the #expr function is:

{{ #expr: expression }}

Spaces are not needed, except between two adjacent words (for operators and constants). Inside numbers, no grouping separators, also known as decimal and thousands separators, (spaces, commas, apostrophes) are allowed; the only decimal separator supported is the period (dot, full stop); and currently the only supported digits are the ASCII decimal digits 0–9.

In expressions embedded within templates, be careful when using magic words for date and time elements, such as {{CURRENTHOUR}} as their return value may be formatted differently on translated pages, which will break expressions (instead, use the parser function #time with the appropriate xn flags before each formatting element). Similar errors will occur with some other magic words that return quantities (make sure you include the appropriate raw formatting flag).

General

[edit]

Technically, an expression is a string representing a tree structure with type/value pairs as nodes, with binary operators in infix notation, unary operators in prefix notation, and end nodes represented by numbers and constants. Letters in operators and names of constants are case-insensitive.

The ParserFunctions extension determines which operators and constants, and what numbers, are supported. The e in scientific notation (e.g., "2e3" for 2000) and the sign of a number are treated as operators, while the supported literal numbers are unsigned numbers in ordinary decimal format. The extension also determines the precedence of the operators and the error messages, and it converts all literal numbers to floating-point values.

For the rest of the implementation, the extension uses PHP functions and operators, so any type conversions and peculiarities of certain operators are the properties of those PHP functions and operators themselves. Also, the format of results is entirely determined by PHP.

The only data types used are the PHP data types float (double precision floating-point format) and integer (64-bit integer). The range for type integer is from ?263 = ?9,223,372,036,854,775,808 through 263 − 1 = 9,223,372,036,854,775,807. Type float allows fractions and very large numbers, but only in the range ±253 = ±9,007,199,254,740,992 can all integer values be exactly represented in type float (see Manual:Representation of numbers in expr parser function).

Dynamic typing is applied. The end nodes are all of type float (as mentioned, numbers are converted to float; this applies even for numbers with an integer value and format). The data type of the result of an operation depends on the operator, and for some operators on the type(s) of the argument(s), and in some cases on their value(s). If according to these rules the result is of type float, any argument of type integer is converted to float before the operation, and the result is also rounded to float:

  • {{numfh|(trunc2^trunc62+trunc512)-2^62}} → 0
  • {{numfh|(trunc2^trunc62+trunc512)+(trunc2^trunc62+trunc1535)}} → 9,223,372,036,854,776,000 (ca. 9.2e18) 1.0000000000000hex*2^63
  • {{numfh|2^63+2047}} → 9,223,372,036,854,778,000 (ca. 9.2e18) 1.0000000000001hex*2^63

Apart from that, a numerical value outside the range of type integer is converted to float, except in the case of trunc (and mod, which involves applying trunc to the arguments first).

There are 31 operators (excluding two synonyms), two constants, and the unsigned numbers in ordinary decimal notation.

Operators, numbers, and constants

[edit]

Since literal numbers are of type float, trunc is sometimes used in the examples to construct an integer-type argument, to demonstrate the result of an operator for this case.

Operator Args Operation PHP Data type Prec Examples
(number) 0 unsigned number in ordinary decimal notation (unary plus and minus and e are treated as operators, see elsewhere in this table) floatval float n.a.
{{#expr:1234567890123456789}} 1.2345678901235E+18
{{#expr:123456789.0123456789}} 123456789.01235
+ 1 unary + sign (nothing) same as argument n.a.
{{#expr:+1}} 1
{{#expr:+-1}} -1
{{#expr:+trunc1}} 1
- 1 unary - sign (negation) - same as argument 10
{{#expr:-12}} -12
{{#expr:-trunc12}} -12
{{#expr:-trunc(-2^63)}} 9.2233720368548E+18
pi 0 constant π pi() float n.a.
{{#expr:pi}} 3.1415926535898
e as subexpression 0 constant e exp(1) float n.a.
{{#expr:e}} 2.718281828459
e between subexpressions 2 *10^ * pow (10,..) float unless the factor on the left is of type integer and the exponent is non-negative and of type integer 10
{{#expr:2e3}} 2000
{{#expr:-2.3e-4}} -0.00023
{{#expr:(trunc2)e(trunc-3)}} 0.002
{{#expr:(trunc2)e(trunc0)}} 2
{{#expr:(trunc2)e(trunc18)}} 2000000000000000000
{{#expr:(trunc2)e(trunc19)}} 2.0E+19
{{#expr:6e(5-2)e-2}} 60
{{#expr:1e.5}} 3.1622776601684

Wrong:

{{#expr:e4}} Expression error: Unexpected number.
exp 1 exponential function ex exp float 9
{{#expr:exp43}} 4.7278394682293E+18
{{#expr:exp trunc0}} 1
{{#expr:exp709}} 8.218407461555E+307
{{#expr:exp-744}} 9.8813129168249E-324

Compare:

{{#expr:e^43}} 4.7278394682293E+18
{{#expr:trunc exp43}} 4727839468229346304
ln 1 natural logarithm log float 9
{{#expr:ln2}} 0.69314718055995
{{#expr:ln trunc1}} 0
{{#expr:ln8.9e307}} 709.07967482591
{{#expr:ln.5e-323}} -744.44007192138

Hence, the common logarithm of e.g. 2:

{{#expr:ln2/ln10}} 0.30102999566398
abs 1 absolute value abs same as argument, but never negative 9
{{#expr:abs-2}} 2
{{#expr:abs trunc-2}} 2
{{#expr:abs trunc-2^63}} 9.2233720368548E+18
sqrt 1 square root sqrt float 9
{{#expr:sqrt 4}} 2
{{#expr:sqrt 2}} 1.4142135623731
{{#expr:sqrt 1e19}} 3162277660.1684

Negative arguments are not permitted:

{{#expr:sqrt-1}} In sqrt: Result is not a number.
trunc 1 truncation (int), i.e. type-casting to integer integer 9
{{#expr:trunc1.2}} 1
{{#expr:trunc1.8}} 1
{{#expr:trunc-1.2}} -1
{{#expr:trunc(-2^64+1e5)}} 98304
{{#expr:trunc(-2^63+1e5)}} -9223372036854675456
{{#expr:trunc(2^63)}} -9223372036854775808
{{#expr:trunc(2^63+1e5)}} -9223372036854675456
{{#expr:trunc(2^64+1e5)}} 98304
floor 1 floor function floor float 9
{{#expr:floor1.2}} 1
{{#expr:floor-1.2}} -2
{{#expr:floor trunc3}} 3
ceil 1 ceiling function ceil float 9
{{#expr:ceil1.2}} 2
{{#expr:ceil-1.2}} -1
{{#expr:ceil trunc3}} 3
sin 1 sine sin float 9
{{#expr:sin.1}} 0.099833416646828
{{#expr:sin trunc1}} 0.8414709848079

With an angle in degrees, e.g. 30°:

{{#expr:sin(30*pi/180)}} 0.5
cos 1 cosine cos float 9
{{#expr:cos.1}} 0.99500416527803
{{#expr:cos trunc1}} 0.54030230586814
tan 1 tangent tan float 9
{{#expr:tan.1}} 0.10033467208545
{{#expr:tan trunc1}} 1.5574077246549
asin 1 arcsine asin float 9
{{#expr:asin.1}} 0.10016742116156
{{#expr:asin trunc1}} 1.5707963267949
acos 1 arccosine acos float 9
{{#expr:acos.1}} 1.4706289056333
{{#expr:acos trunc1}} 0
{{#expr:2*acos 0}} 3.1415926535898
atan 1 arctangent atan float 9
{{#expr:atan.1}} 0.099668652491162
{{#expr:atan trunc1}} 0.78539816339745
{{#expr:4*atan 1}} 3.1415926535898
not 1 negation, logical NOT ! integer (1 or 0) 9
{{#expr:not0}} 1
{{#expr:not1}} 0
{{#expr:not2}} 0
{{#expr:not trunc1}} 0
^ 2 exponentiation (power) pow float unless the base is of type integer and the exponent is non-negative and of type integer 8
{{#expr:2^3}} 8
{{#expr:-2^3}} -8
{{#expr:-2^4}} 16
{{#expr:(trunc2)^(trunc-3)}} 0.125
{{#expr:(trunc2)^(trunc0)}} 1
{{#expr:(trunc2)^(trunc62)}} 4611686018427387904
{{#expr:(trunc2)^(trunc63)}} 9.2233720368548E+18
{{#expr:(-2)^1.2}} NAN
{{#expr:(-2)^.5}} NAN
* 2 multiplication * integer if both arguments are integer, otherwise float 7
{{#expr:2*3}} 6
{{#expr:(trunc2)*3}} 6
{{#expr:2*trunc3}} 6
{{#expr:(trunc2)*trunc3}} 6
{{#expr:(trunc1e10)*trunc1e9}} 1.0E+19
/ (also written div) 2 division (div is not integer division[1]) / float, unless both arguments are integer and the mathematical result is an integer 7
{{#expr:6/3}} 2
{{#expr:(trunc6)/3}} 2
{{#expr:2/trunc6}} 0.33333333333333
{{#expr:(trunc6)/trunc3}} 2
{{#expr:(trunc6)/trunc4}} 1.5
mod 2 modulo operation, remainder of division after truncating both operands to an integer.[1] % integer 7
{{#expr:30mod7}} 2
{{#expr:-30mod7}} -2
{{#expr:30mod-7}} 2
{{#expr:-30mod-7}} -2
{{#expr:30.5mod7.9}} 2

May give unexpected results for some values of the second argument (see this section):

{{#expr:123mod2^64}} Division by zero.
fmod 2 modulo operation, floating point. Returns first argument after subtracting an integer multiple of the second argument. fmod float 7
{{#expr:5.7fmod1.3}} 0.5
{{#expr:99.9fmod60}} 39.9
{{#expr:2.99fmod1}} 0.99
{{#expr:-2.99fmod1}} -0.99
{{#expr:2.99fmod-1}} 0.99
{{#expr:-2.99fmod-1}} -0.99
+ 2 addition + integer if both arguments are integer, otherwise float 6
{{#expr:2+3}} 5
{{#expr:(trunc2)+3}} 5
{{#expr:2+trunc3}} 5
{{#expr:(trunc2)+trunc3}} 5
{{#expr:(trunc7e18)+trunc4e18}} 1.1E+19
- 2 subtraction - integer if both arguments are integer, otherwise float 6
{{#expr:3-2}} 1
{{#expr:(trunc3)-2}} 1
{{#expr:2-trunc2}} 0
{{#expr:(trunc3)-trunc2}} 1
{{#expr:(trunc-7e18)-trunc4e18}} -1.1E+19
round 2 rounds off the number on the left to a multiple of 1/10 raised to a power, with the exponent equal to the truncated value of the number given on the right round float 5
{{#expr:9.876round2}} 9.88
{{#expr:(trunc1234)round trunc-2}} 1200
{{#expr:4.5round0}} 5
{{#expr:-4.5round0}} -5
{{#expr:46.857round1.8}} 46.9
{{#expr:46.857round-1.8}} 50
= 2 equality (numerical incl. logical, not for strings) == integer (1 or 0) 4
{{#expr:3.0=3}} 1
{{#expr:3.1=3}} 0
{{#expr:3.0=trunc3}} 1
{{#expr:3.1=trunc3}} 0
{{#expr:1e16=trunc(1e16)}} 1
{{#expr:1e16=trunc(1e16)+trunc1}} 1
{{#expr:trunc(1e16)=trunc(1e16)+trunc1}} 0

wrong:

{{#expr:a=a}} Expression error: Unrecognized word "a".
<> (also written !=) 2 inequality, logical xor; not for strings (negation of =) != integer (1 or 0) 4
{{#expr:3<>3}} 0
{{#expr:3<>4}} 1
< 2 less than (not for ordering of strings) < integer (1 or 0) 4
{{#expr:3<3}} 0
{{#expr:3<4}} 1
{{#expr:2.9<3}} 1
{{#expr:3.0<3}} 0
{{#expr:2.9<trunc3}} 1
{{#expr:3.0<trunc3}} 0
{{#expr:1e16<trunc(1e16)+trunc1}} 0

wrong:

{{#expr:a<b}} Expression error: Unrecognized word "a".
> 2 greater than (same as <, with arguments reversed) > integer (1 or 0) 4
{{#expr:4>3}} 1
{{#expr:3>3}} 0
<= 2 less than or equal to (same as >=, with arguments reversed) <= integer (1 or 0) 4
{{#expr:3<=4}} 1
{{#expr:3<=3}} 1
>= 2 greater than or equal to (negation of <) >= integer (1 or 0) 4
{{#expr:4>=3}} 1
{{#expr:3>=3}} 1
and 2 logical AND && integer (1 or 0) 3
{{#expr:3and4}} 1
{{#expr:-3and0}} 0
{{#expr:0and4}} 0
{{#expr:0and0}} 0
or 2 logical OR || integer (1 or 0) 2
{{#expr:3or4}} 1
{{#expr:-3or0}} 1
{{#expr:0or4}} 1
{{#expr:0or0}} 0

The logical operators and, or, and not interpret an input value of 0 as false and any other number as true, and return 0 for a false result and 1 for true. These output values also apply to the relation operators; thus {{#expr: (2 < 3) + 1}} gives 2.

To use and, or, and not in #if, #ifeq, or #ifexist contexts, one can use 1 as then-text (true case) and 0 as else-text (false case), then combine the results with the logical operators provided by #expr or #ifexpr. Note that negation can also be achieved by subtracting the result from 1 (inside of #expr or #ifexpr) or by simply switching the then- and else-text(s) of #if #ifeq #ifexists or #ifexpr. Also, note that the construct {{#expr: {{#if:{{{a|}}}|1|0}} or {{#if:{{{b|}}}|1|0}} }} is equivalent to the simpler {{#if:{{{a|}}}{{{b|}}}|1|0}}}}.

Precedence is indicated in the "Prec" column above, a higher number means that the operator is applied earlier. Examples (">" refers to going before, "~" means application from left to right):

  • e > floor, not, etc.: {{#expr:floor1.5e1}} → 15, {{#expr:not0e1}} → 1
  • floor > ^: {{#expr:floor1.5^2}} → 1
  • ^ > *: {{#expr:2*3^2}} → 18
  • * ~ / ~ mod: {{#expr:12/3*2}} → 8, {{#expr:111/3mod10}} → 7, {{#expr:358mod10*2}} → 16,
  • * > +, -: {{#expr:2+3*4}} → 14, {{#expr:2-3*4}} → -10
  • + ~ -: {{#expr:6-2+3}} → 7, {{#expr:-2+3}} → 1
  • +, - > round: {{#expr:1.234round2-1}} → 1.2
  • round > = etc.: {{#expr:1.23=1.234round2}} → 1
  • = etc. > and: {{#expr:1 and 2=1}} → 0
  • and > or: {{#expr:1 or 1 and 0}} → 1

In the case of equal precedence number, evaluation is from left to right:

  • {{#expr:12/2*3}} → 18
  • {{#expr:3^3^3}} → 19683

Parentheses can force a different precedence: {{#expr:(2+3)*4}} → 20

Blank spaces are good for readability but not needed for working properly, except between words (including "e"), and not allowed within numbers:

  • {{#expr:7mod3}} gives 1 [1]
  • {{#expr:7.5round0}} gives 8 [2]
  • {{#expr:0and1}} gives 0 [3]
  • {{#expr:0or not0}} gives 1 [4]
  • {{#expr:0ornot0}} gives Expression error: Unrecognized word "ornot". [5]
  • {{#expr:123 456}} gives Expression error: Unexpected number. [6]
  • {{#expr:not not3}} gives 1 [7]
  • {{#expr:notnot3}} gives Expression error: Unrecognized word "notnot". [8]
  • {{#expr:---2}} gives -2 [9]
  • {{#expr:-+-2}} gives 2 [10]
  • {{#expr:2*-3}} gives -6 [11]
  • {{#expr:-not-not-not0}} gives -1 [12]
  • {{#expr:2*/3}} gives Expression error: Unexpected / operator. [13]
  • {{#expr:sinln1.1}} gives Expression error: Unrecognized word "sinln". [14]
  • {{#expr:sin ln1.1}} gives 0.095165945236752 [15]

For scientific notation e is treated as an operator. An e between subexpressions works just like *10^, except that together with the unary minus it has the highest precedence (e.g., before a separate ^), and that the implicit 10 is of type integer, not float. An e as subexpression (i.e., with each side either nothing or an operator) is Euler's constant. An e with nothing or an operator on one side and a subexpression on the other gives an error message.

Rounding operators

[edit]
See also Manual:Rounding numbers for some more examples

The following rounding operators are supported:

  • Symmetric with respect to 0 and non-strictly monotonic on each side of 0:
    • trunc
    • round
  • Otherwise related to 0 and periodic on each side of 0:
    • mod

Trunc

[edit]

Trunc converts a float to an integer by cutting off the decimal part. For values in the range 2^63 ≤ x ≤ 2^64, it returns x - 2^64. Values larger than 2^64 returns 0, and values less than -2^63 returns -2^63.

See the below examples:

  • {{#expr:trunc(-2*2^63-2^12)}} → -4096
  • {{#expr:trunc(-2*2^63+2^12)}} → 4096
  • {{#expr:trunc(-1*2^63-2^12)}} → 9223372036854771712
  • {{#expr:trunc(-1*2^63+2^12)}} → -9223372036854771712
  • {{#expr:trunc(0*2^63-2^12)}} → -4096
  • {{#expr:trunc(0*2^63+2^12)}} → 4096
  • {{#expr:trunc(1*2^63-2^12)}} → 9223372036854771712
  • {{#expr:trunc(1*2^63+2^12)}} → -9223372036854771712
  • {{#expr:trunc(2*2^63-2^12)}} → -4096
  • {{#expr:trunc(2*2^63+2^12)}} → 4096
  • {{#expr:trunc(2^64+1024)}} → 0
  • {{#expr:trunc(3*2^63-2^12)}} → 9223372036854771712
  • {{#expr:trunc(3*2^63+2^12)}} → -9223372036854771712

Converting a number to an integer type ensures precise display without rounding to 14 decimal places.

x is truncated, x = trunc x, if it is an integer or a floating-point number representing an integer value.

The expression p mod 0 results in an error message when used in MediaWiki, not the PHP operator %:

  • {{#expr:-27mod0}}Division by zero.

In PHP, when using the % operator, if p % 0 is calculated and conditions are met where 0 < |q| < 1 and q >= 2^64, the result is an empty string.

  • {{formatnum:{{#expr:-123 mod .9}}}}Division by zero.
  • {{formatnum:{{#expr:-123 mod -.9}}}}Division by zero.
  • {{formatnum:{{#expr:-123 mod (2^64)}}}}Division by zero.
  • {{formatnum:{{#expr:-123 mod 1e20}}}} → ?123
  • Compare:
    • {{formatnum:{{#expr:-123 mod (2^64-2048)}}}} → ?123

Floor and ceil

[edit]

When using floor or ceil with an integer, it's first converted to a floating-point number before the function is applied.

  • {{numf|floor (trunc1e17+trunc1)}} → 100,000,000,000,000,000
  • {{numf|ceil (trunc1e17+trunc1)}} → 100,000,000,000,000,000

Use Template:Floor and Template:Ceil to get in such a case the integer-type expression for the exact result:

  • {{numf|{{floortrunc1e17+trunc1}}}} → 100,000,000,000,000,000
  • {{numf|{{ceil|trunc1e17+trunc1}}}} → 100,000,000,000,000,000

To check whether the internal result of an expression x is mathematically an integer one can simply test "(x) = floor (x)", or similarly with ceil (not with trunc because for large floats we would get false negatives, and not with round0, because for odd numbers between 2^52 and 2^53, we would get false negatives).

Safety margins

[edit]

To round an integer x down to the nearest multiple of 7, you can do the following:

  • 7*((x-3)/7 round 0)
  • 7*floor((x+.5)/7)
  • 7*ceil((x-6.5)/7)

To round x to the nearest multiple of 7, you can do the following:

  • 7*((x/7) round 0)
  • 7*floor((x+3.5)/7)
  • 7*ceil((x-3.5)/7)

In these cases, the sign of x doesn't matter, even for rounding, because the value is never exactly halfway between integers. All methods provide the same safety margin of 0.5 in x, or 1/14 after dividing by 7, to allow for rounding errors.

For any real value of x, the difference between the two problems is equivalent to a shift in x by 3.5. However, in this case with integer x values, the shift is 3.

Precedence

[edit]

Additions before round:

  • {{#expr:1.234 + 1.234 round 1 + 1}} ? 2.47

Modulo and multiplication operations have equal precedence and they are evaluated from left-to-right:

  • {{#expr:3 * 4 mod 10 * 10}} ? 20

When using spaces in expressions with precedence, the layout can sometimes be confusing:

  • {{#expr:23+45 mod 10}} ? 28

Instead, you can write:

  • {{#expr:23 + 45 mod10}} ? 28

Or you can parenthesis:

  • {{#expr:23 + (45 mod 10)}} ? 28

Positional and named parameters

[edit]

Positional parameters can be used in calculations inside a template.

For example, in w:Template:To USD/data/2018, the {{{1}}} positional parameter refers to the amount of local currency to convert to US dollars, and the second positional parameter specifies the country whose currency is being used. When Chinese currency is specified (CHN), the effective template code is:

{{#expr: ({{{1}}} / 6.62) round {{#ifeq: {{{round}}} | yes | 0 | 2 }} }}

Here {{{round}}} is an optional named parameter. If it is equal to "yes", then the converted value is displayed to the nearest US dollar. The default is rounding to the nearest penny.

Transitivity

[edit]

For comparing a number of type float with one of type integer, the integer is converted to float. Therefore the operators =, <= and >= are not necessarily transitive with mixed types:

  • {{#expr:trunc1e16=1e16}} → 1
  • {{#expr:1e16=trunc1e16+trunc1}} → 1
  • {{#expr:trunc1e16=trunc1e16+trunc1}} → 0
  • {{#expr:trunc1e16>=1e16}} → 1
  • {{#expr:1e16>=trunc1e16+trunc1}} → 1
  • {{#expr:trunc1e16>=trunc1e16+trunc1}} → 0

Similarly, if a >= b and b = c, that does not necessarily imply a >= c:

  • {{#expr:trunc1e16>=1e16}} → 1
  • {{#expr:1e16=trunc1e16+trunc1}} → 1
  • {{#expr:trunc1e16>=trunc1e16+trunc1}} → 0

However, < and > are properly transitive.

Monotonicity

[edit]

When dividing numbers of type integer, a small change in the dividend can change the type of the result. Therefore, if the absolute value of the result is greater than 2^53, it is not always a monotonic function of the dividend:

  • {{numf|(trunc1e18-trunc2)/trunc3}} → 333,333,333,333,333,300
  • {{numf|(trunc1e18-trunc1)/trunc3}} → 333,333,333,333,333,300
  • {{numf|trunc1e18/trunc3}} → 333,333,333,333,333,300

Numbers as input

[edit]

Leading zeros are allowed, as well as a trailing decimal point (for an integer) and trailing zeros in a number with a decimal point.

  • "{{#expr: +01.20}}" gives "1.2" [16]
  • "{{#expr: 12.}}" gives "12" [17]

These equivalences apply also for #ifeq and #switch, see below.

The part of the expression representing a number is a sequence of digits and points; due to floatval a second point and any digits and points immediately after it are ignored, and do not give an error message. Do not use group separators; a comma is not a recognized symbol for an expression, and an error for an unexpected number is returned if there is a space between two numeric values.

Thus a number can only consist of (following an optional leading sign):

  • one or more digits; or
  • zero or more digits, a decimal point, and zero or more digits.

Numbers in other scripts are not supported.

Examples:

  • "{{#expr:123}}" gives "123" [18]
  • "{{#expr:123.456}}" gives "123.456" [19]
  • "{{#expr:.456}}" gives "0.456" [20]
  • "{{#expr:0}}" gives "0" [21]

Also accepted:

  • "{{#expr: 123.}}" gives "123" [22]
  • "{{#expr:000123.4560}}" gives "123.456" [23]
  • "{{#expr:.}}" gives "0" [24]

With ignored part:

  • "{{#expr:123.456.789}}" gives "123.456" [25]

But wrong:

  • "{{#expr: 123,456}}" gives "Expression error: Unrecognized punctuation character ","." [26]
  • "{{#expr: 123 456}}" gives "Expression error: Unexpected number." [27]
  • "{{#expr:???}}" gives "Expression error: Unrecognized punctuation character "?"." [28]

Combinations with the operator e:

Float:

  • "{{#expr:2.3e-5}}" gives "2.3E-5" [29]
  • {{#expr:2e18}} → 2.0E+18

Integer type:

  • {{#expr:(trunc123456789012345)e trunc4}} → 1234567890123450000

Compare:

  • {{#expr:123456789012345e4}} → 1.2345678901235E+18
  • {{#expr:trunc123456789012345e4}} → 1234567890123450112
  • {{#expr:(trunc123456789012345)e4}} → 1.2345678901235E+18
  • "{{#expr: e5}}" gives "Expression error: Unexpected number." [30]
  • "{{#expr: e}}" gives "2.718281828459" [31]
  • "{{#expr: E}}" gives "2.718281828459" [32]

Commas can be removed as follows (obviously this is most useful when the value is provided by a template call, parser function, or some other magic word):

  • {{formatnum:1,2,,34.567,8|R}} → 1234.5678

Input of a number of type integer is not possible. A float can be converted to type integer with the function trunc. An integer with a value that is not a float value can be constructed, e.g. with Template:Trunc, where the number is given in two pieces:

  • {{numf|{{trunc|12345678|90123456789}}}} → 1,234,567,890,123,456,800

The smallest positive float can be written:

  • {{#expr:.5e-323}} → 4.9406564584125E-324

but we cannot use that output as input:

  • {{#expr:4.9406564584125E-324}} → 0

All digits are used to determine the float to which a number is rounded, as demonstrated in a borderline case:

  • {{hex|0.00000000000000011102230246251566636831481088739149080825}}1.0000000000000hex*2^-53
  • {{hex|0.00000000000000011102230246251566636831481088739149080826}}1.0000000000001hex*2^-53
  • {{numf|2^-53}} → .00000000000000011102230246251565<Expression error: Missing operand for <.
  • {{numf|2^-53+2^-105}} → .00000000000000011102230246251568<Expression error: Missing operand for <.

Similarly:

  • {{numfh|4398046511104.00048828125}} → 4,398,046,511,104 (ca. 4.4e12) 1.0000000000000hex*2^42
  • {{numfh|4398046511104.00048828125000000000000000000000000000000000000000000000001}} → 4,398,046,511,104.001 (ca. 4.4e12) 1.0000000000001hex*2^42
  • 2^42+2^-10 = 4,398,046,511,104.000,976,562,5

Thus we see that the value halfway the two consecutive floats is rounded down in this case, while the other decimal fractions between the two floats are rounded to the nearest of the two.

Numbers as output

[edit]

The MediaWiki software simply passes on the literal result of the PHP computation, except that logical values are changed to 1 and 0. Therefore the format can depend on the server.

A number of type integer is displayed without rounding and in ordinary decimal notation:

  • {{#expr:trunc(2^52)}} → 4503599627370496
  • {{#expr:-trunc(2^52)}} → -4503599627370496
  • {{#expr:trunc1100000}} → 1100000
  • {{#expr:trunc1200000}} → 1200000
  • {{#expr:trunc1300000}} → 1300000
  • {{#expr:trunc4100000}} → 4100000

while a number of type double is rounded to 14 significant digits, while inconsistently displaying some numbers in scientific format. This is reportedly a bug in the Zend Engine which has been fixed [33], but on Wikimedia apparently not yet:

  • {{#expr:2^52}} → 4.5035996273705E+15
  • {{#expr:-(2^52)}} → -4.5035996273705E+15
  • {{#expr:1100000}} → 1100000
  • {{#expr:1200000}} → 1200000
  • {{#expr:1300000}} → 1300000
  • {{#expr:4100000}} → 4100000
  • {{#expr:1/7}} → 0.14285714285714

Note: Internally, the expression may be computed with more digits (typically 18 significant digits, for example on Wikimedia servers which are using IEEE 64-bit double in the implementation of PHP used by MediaWiki, but possibly more depending on the hardware architecture supported by PHP, which may have been itself compiled to use "long double" with an extended precision using 80- to 128-bit binary formats), so the formatted value returned by #expr will not exhibit some small differences.

If MediaWiki is installed on a server whose PHP engine was compiled for an architecture using different binary storage formats for its C/C++ datatype "double" (possibly with less precision than the IEEE 64-bit format), and possibly optimized for speed (the compiled C code or its linked-in mathematical libraries may then not use "strict" IEEE rounding modes for every floating point operation, but may keep some precision for intermediate results by not rounding them at each step, or could also compute results faster using internal values with less precision), you will get different results in PHP: MediaWiki will not be able to use the same precision (or the same range of magnitudes), and the results of calculations may vary between servers. When installing MediaWiki on a PHP server, make sure you look at PHP configuration options.

For example if the architecture supports fast floating points only with 32-bit format, you'll get only 7 or 8 significant digits, and the 14 digits displayed by MediaWiki may exceed what the server can really compute. As well you may get unexpected "infinite" values or zero where another server could have returned accurate values.

For some representable round numbers, notably some multiples of 100,000, scientific notation is produced, which, if reused in an expression, is not even exactly equal to the original number:

  • {{numfh|4.1e6}} → 4,099,999.9999999995 (ca. 4.1e6) 1.f47cfffffffffhex*2^21

Thus we may want to either compare two results of #expr (for equality up to 14 digits) or compare two expressions, such as 4100000 and 4000000+100000 (for exact equality); depending on context and intention, the negative result of the comparison of the result of #expr with the exact number may be confusing.

The function formatnum adds commas (on the left of the decimal point only), but does not convert from or to scientific format:

  • {{formatnum:1234567.890123}} → 1,234,567.890123
  • {{formatnum:1234567.890123E16}} → 12,345,678,901,230,000,000,000

The number output is suitable for many other calculation programs, also the scientific notation. In that sense output like 6E23 is more convenient than 6×10Template:Valid.

Template:Num displays a number with high accuracy (such that in the case of float the specific internal value is reconstructed when using the output as input), with the variant Template:Numf showing thousands separators:

  • {{numf|trunc3^trunc39}} → 4,052,555,153,018,976,000
  • {{numf|trunc3^trunc40}} → 12,157,665,459,056,929,000
  • {{numf|1/7}} → .14285714285714285

Negative zero

[edit]

Although the literal "-0" (the unary minus applied to 0) gives 0, some operations give the float value "?0" (preserving commutativity of + and *):

Generating ?0 with *, /, ceil, round:

  • {{#expr: -1*0}} → -0
  • {{#expr: (-1e-200)*1e-200}} → -0
  • {{#expr: -1/1e333}} → -0
  • {{#expr: 0/-1}} → -0
  • {{#expr: (-1e-200)/1e200}} → -0
  • {{#expr: (1e-200)/-1e200}} → -0
  • {{#expr: ceil(-.1)}} → -0
  • {{#expr: -.2round0}} → -0

Passing ?0 on with unary +, binary + and -, * (and hence operator e), /, floor, ceil:

  • {{#expr: +(-1*0)}} → -0
  • {{#expr: (-1*0)+(-1*0)}} → -0
  • {{#expr: (-1*0)-0}} → -0
  • {{#expr: 1*(-1*0)}} → -0
    • {{#expr: (-1*0)e0}} → -0
  • {{#expr: (-1*0)/1}} → -0
  • {{#expr: floor(-1*0)}} → -0
  • {{#expr: ceil(-1*0)}} → -0

However:

  • {{#expr: -0}} → -0
  • {{#expr: -10^-401}} → -0

Since 2011 the function #ifexpr takes both 0 and the float ?0 as false:

  • {{#ifexpr:0|1|0}} → 0
  • {{#ifexpr:-1*0|1|0}} → 0

Also, as argument of a logical operator ?0 is taken as false:

  • {{#expr:not(-1*0)}} → 1
  • {{#expr:(-1*0)and1}} → 0
  • {{#expr:(-1*0)or0}} → 0

If an expression of type float can have the value ?0, then an operation that will remove the minus sign from a possible ?0 but not affect any other result is the addition of 0. If an expression may be of type integer then one can add trunc0.

  • {{#expr:0+(-1*0)}} → 0

Type conversion

[edit]

A float can be converted to type integer by operator trunc (however, note that for 2^63 <= x <= 2^64, we get x ? 2^64, and for larger x we get 0; for x < ?2^63 we get ?2^63).

An expression of type integer can be converted to float by adding 0. Note that for integers greater than 2^53, this involves rounding.

Limitations and workarounds

[edit]

The operator trunc gives the correct mathematical result of rounding toward 0 to an integer for integer-type numbers and for floats x inside the integer range: ?2^63 <= x < 2^63. To also get the correct mathematical result for floats outside this range is simple, because these floats all have an integer value, so they can be left unchanged. Template:Trunc does this.

The operator mod gives strange errors for some fairly large values of the second argument:

  • {{#expr:123mod(2^64-1)}}Division by zero.

The operator round with second argument 0 gives wrong results for odd numbers between 2^52 and 2^53, even though the exact results are representable as float. Also, the operator rounds integer-type numbers with an absolute value between 2^53 and 2^63 to float.

The operator floor rounds integer-type numbers with an absolute value between 2^53 and 2^63 to float, and not necessarily downward. Similarly the operator ceil rounds these numbers not necessarily upward.

Branching depending on an expression

[edit]

The function #ifexpr produces one of two specified results, depending on the value of a boolean expression involving numbers and booleans (not strings). Examples:

  • {{#ifexpr: {{CURRENTDOW}} = 0 or {{CURRENTDOW}} = 6 | weekEND | weekDAY}} yields weekDAY because today is Thursday and so "{{CURRENTDOW}}" is "4" [34].
  • {{#expr:2^10=1024}} → 1

Note that rounding errors can affect a comparison, even if they are not visible in the displayed values: the internal values are compared. This applies even to large integers:

  • {{#expr:1024e20-1e23}} → 2.4E+21
  • {{#expr:1024e20-1e23=2.4e21}} → 0

Instead one may want to allow a relatively small difference that could be present due to rounding errors:

  • {{#expr:abs(1024e20-1e23-2.4e21)<1e8}} → 1

Again, for comparing a number of type float with one of type integer, the integer is converted to float. In this case the type is determined by the format of the number, e.g. 2 is an integer, but 2.0 and 2e0 are floats; also 12345678901234567890 is a float, because it is too large for an integer.

Again, equality is not transitive with mixed types:

  • {{#ifeq:12345678901234567|12345678901234568.0|1|0}} → 1
  • {{#ifeq:12345678901234568.0|12345678901234568|1|0}} → 1
  • {{#ifeq:12345678901234567|12345678901234568|1|0}} → 0
  • {{#ifeq:12345678901234567|12345678901234567e0|1|0}} → 1
  • {{#ifeq:12345678901234567e0|12345678901234568|1|0}} → 1
  • {{#ifeq:12345678901234567|12345678901234568|1|0}} → 0
  • {{num|trunc(2^62)-trunc1+trunc(2^62)}} → 9223372036854775807
  • {{#ifeq:9223372036854775700|9223372036854775900|1|0}} → 0
  • {{#ifeq:9223372036854775900|9223372036854775800|1|0}} → 0
  • {{#ifeq:9223372036854775700|9223372036854775800|1|0}} → 0

Comparisons

[edit]

The functions #ifeq and #switch compare numbers and strings for equality using PHP operator ==, the same as the equality operator mentioned above, but now applied directly to the expanded wikitext of the arguments. For comparison as numbers no expressions (not even constants) are allowed, but in this case the unary plus and minus and the e of scientific notation are taken as part of the number, instead of as operators. Without e and decimal point the type is integer, otherwise it is float. As mentioned above, when an integer is compared with a float, the integer is converted to float first.

  • {{#ifeq:3|3.0|1|0}} → 1
  • {{#ifeq:3|03|1|0}} → 1
  • {{#ifeq:0.00003456|3.456E-05|1|0}} → 1
  • {{#ifeq:1e23|.1e24|1|0}} → 1 although rounding both numbers to float gives different internal numbers:
    • {{#expr:1e23-.1e24}} → 0
    • {{#expr:1e23=.1e24}} → 1
    • {{#ifexpr:1e23=.1e24|1|0}} → 1
  • {{#ifeq:9034567890123456789|9034567890123456788|1|0}} → 0 (two numbers of type integer, therefore only true if exactly equal); compare:
  • {{#ifeq:9034567890123456700.0|9034567890123456800|1|0}} → 1 (due to the decimal point in one number, both are rounded to float before the comparison, so the comparison is cruder)

Error messages

[edit]

The following examples show all known #expr and #ifexpr error messages. The error strings are all within <strong class="error"> elements.

  • Division by zero
    • "{{#expr:1/0}}" → "Division by zero." [35]
  • Missing operand
    • "{{#expr:2+}}" → "Expression error: Missing operand for +." [36]
    • "{{#expr:2-}}" → "Expression error: Missing operand for -." [37]
    • "{{#expr:2*}}" → "Expression error: Missing operand for *." [38]
    • "{{#expr:2/}}" → "Expression error: Missing operand for /." [39]
  • Unexpected number
    • "{{#expr:1 2}}" → "Expression error: Unexpected number." [40]
  • Unexpected word
    • "{{#expr:1 a}}" → "Expression error: Unrecognized word "a"." [41]
  • Unexpected operator
    • "{{#ifexpr:1/*2}}" → "Expression error: Unexpected * operator." [42]
    • "{{#ifexpr:1*/2}}" → "Expression error: Unexpected / operator." [43]
    • "{{#expr:>1}}" → "Expression error: Unexpected > operator." [44]
    • "{{#expr: 1 (2)}}" → "Expression error: Unexpected ( operator." [45]
  • Unexpected bracket
    • "{{#expr: (1}}" → "Expression error: Unclosed bracket." [46]
    • "{{#expr: 1)}}" → "Expression error: Unexpected closing bracket." [47]
  • Unrecognized punctuation
    • "{{#expr:{{{a}}}}}" → "Expression error: Unrecognized punctuation character "{"." [48]
    • "{{#expr:2*123,456}}" → "Expression error: Unrecognized punctuation character ","." [49]
    • "{{#ifexpr:3%2}}" → "Expression error: Unrecognized punctuation character "%"." [50]
  • Unrecognized word
    • "{{#ifexpr:abc}}" → "Expression error: Unrecognized word "abc"." [51]
    • "{{#expr:abc.def}}" → "Expression error: Unrecognized word "abc"." [52]
  • Result is not a number
    • "{{#expr:sqrt-1}}" → "In sqrt: Result is not a number." [53]
  • Invalid argument
    • "{{#expr:ln0}}" → "Invalid argument for ln: less than or equal to 0." [54]
    • "{{#expr:asin-22}}" → "Invalid argument for asin: less than -1 or greater than 1." [55]
  • Stack exhausted
    • "{{#expr: {{x|34|(1+(}} 1 {{x|34|))}} }}" → "Expression error: Stack exhausted."

Note that this last example uses Template:x, which generates multiple copies of a given string. However:

  • "{{#expr: {{x|33|(1+(}} 1 {{x|33|))}} }}" → "34"

Large numbers and infinity

  • {{#expr: {{x|102|1000*}} 18 }} → 1.8E+307
  • {{#expr: {{x|102|1000*}} 179 }} → 1.79E+308
  • {{#expr: {{x|102|1000*}} 180 }} → INF

On Wikimedia wikis this last example gives "INF", but depending on the operating system of the server it may also give, e.g., "1.#INF".

INF also appears when an intermediate result is out of range:

  • {{#expr:1e309/1e308}} → INF
  • {{#expr:1e200*1e200*1e-300}} → INF

but:

  • {{#expr:1e200*(1e200*1e-300)}} → 1.0E+100

Notice that an infinite result is not considered an error:

  • {{#iferror: {{#expr:1e200*1e200*1e-300}} | error}} → INF

Miscellaneous malformed expressions or markup

These are not errors, per se, but are probably not what was intended.

  • "#expr:3.4.5.6" → "3.4"
  • "{{{#expr:2*3}}}" → "{{{#expr:2*3}}}"   (triple braces are interpreted as a reference to an undefined parameter named "#expr:2*3")
  • "{{#expr:2*3}}}" → "6}"   (extra closing braces are interpreted as plain text)
  • "{{{#expr:2*3}}" → "{6"   (one extra opening brace is interpreted as plain text)
  • "{{#expr:2*3}" → "{{#expr:2*3}"   (too few opening or closing braces results in everything being interpreted as plain text)

Checking for a number

[edit]

Check whether a string is a valid numeric expression:

  • {{#if:{{#ifexpr:3}}|0|1}} gives 1
  • {{#if:{{#ifexpr:3-2}}|0|1}} gives 1
  • {{#if:{{#ifexpr:3 2}}|0|1}} gives 0

Find the value represented by a string if it is a valid numeric expression, otherwise just return the string:

  • {{#iferror:{{#expr:3}}|3}} gives 3
  • {{#iferror:{{#expr:3-2}}|3-2}} gives 1
  • {{#iferror:{{#expr:3 2}}|3 2}} gives 3 2

Check whether a string is a number:

  • {{#ifeq:3|{{#expr:3}}|1|0}} gives 1
  • {{#ifeq:-3|{{#expr:-3}}|1|0}} gives 1
  • {{#ifeq:3.5|{{#expr:3.5}}|1|0}} gives 1
  • {{#ifeq:03|{{#expr:03}}|1|0}} gives 1
  • {{#ifeq:3-2|{{#expr:3-2}}|1|0}} gives 0
  • {{#ifeq:3 2|{{#expr:3 2}}|1|0}} gives 0

Minus sign

[edit]

Only the hyphen-minus character or minus sign character, typed directly, work as a minus sign operator in expressions.

  • The HTML character references (by name or by numeric code point value) are not recognized when evaluating expressions: numerical character references are converted only when generating the final HTML document (after expansion of templates and parser functions)
  • Only a handful of character references by name are substituted early by MediaWiki, all others are interpreted only by the browser.
  • The other dash characters (such as the hyphen, the figure dash, en dash, em dash and others), though often similar visually, are not valid minus signs, but punctuation signs or typographical variants.
hyphen-minus|, typed directly as the character '-' (U+002D) "{{#expr:-12}}" "-12" [56]
hyphen-minus, typed as the numerical character reference &#x2D; "{{#expr:&#x2D;12}}" "Expression error: Unrecognized punctuation character "&"." [57]
hyphen-minus, typed as the numerical character reference &#45; "{{#expr:&#45;12}}" "Expression error: Unrecognized punctuation character "&"." [58]
minus sign, typed directly as the character '?' (U+2212) "{{#expr:?12}}" "-12" [59]
minus sign, typed as the numerical character reference &#x2212; "{{#expr:&#x2212;12}}" "Expression error: Unrecognized punctuation character "&"." [60]
minus sign, typed as the numerical character reference &#8722; "{{#expr:&#8722;12}}" "Expression error: Unrecognized punctuation character "&"." [61]
minus sign, typed as the symbolic character reference &minus; "{{#expr:&minus;12}}" "-12" [62]
figure dash, typed directly as the character '?' (U+2012) "{{#expr:?12}}" "Expression error: Unrecognized punctuation character "?"." [63]
figure dash, typed as the numerical character reference &#x2012; "{{#expr:&#x2012;12}}" "Expression error: Unrecognized punctuation character "&"." [64]
figure dash, typed as the numerical character reference &#8210; "{{#expr:&#8210;12}}" "Expression error: Unrecognized punctuation character "&"." [65]
en dash, typed directly as the character '–' (U+2013) "{{#expr:–12}}" "Expression error: Unrecognized punctuation character "–"." [66]
en dash, typed as the numerical character reference &#x2013; "{{#expr:&#x2013;12}}" "Expression error: Unrecognized punctuation character "&"." [67]
en dash, typed as the numerical character reference &#8211; "{{#expr:&#8211;12}}" "Expression error: Unrecognized punctuation character "&"." [68]
en dash, typed as the symbolic character reference &ndash; "{{#expr:&ndash;12}}" "Expression error: Unrecognized punctuation character "&"." [69]

Also many other calculation programs require a hyphen. Therefore, in order to be able to copy rendered numbers and expressions to the edit box or input them through a copy operation into other calculation programs, displayed minus signs also need to be hyphens.

Displaying numbers and numeric expressions

[edit]

A point of consideration can also be the possibility to apply the rendered output to #expr or #ifexpr, or to input it without conversion into other calculation programs. This would require the following:

  • use digits, not words
  • as mentioned above, use the hyphen as minus sign
  • use *, <=, >=, and <>, not ×, ≤, ≥, or ≠
  • do not use thousands separators (however, some programs allow them)
  • use output like 6E23 or 6e23 rather than 6×10Template:Valid

Examples:

  • "{{#expr:three}}" gives "Expression error: Unrecognized word "three"." [70]
  • "{{#expr:2<3}}" gives "1" [71]
  • "{{#expr:2≤3}}" gives "Expression error: Unrecognized punctuation character "≤"." [72]
  • "{{#expr:2<=3}}" gives "1" [73]
  • "{{#expr:2>3}}" gives "0" [74]
  • "{{#expr:2≥3}}" gives "Expression error: Unrecognized punctuation character "≥"." [75]
  • "{{#expr:2>=3}}" gives "0" [76]
  • "{{#expr:2*3}}" gives "6" [77]
  • "{{#expr:2×3}}" gives "Expression error: Unrecognized punctuation character "×"." [78]
  • "{{#expr:2,300}}" gives "Expression error: Unrecognized punctuation character ","." [79]
  • "{{#expr:6E23}}" gives "6.0E+23" [80]

If the number is the result of a computation by MediaWiki and unsuitable for use in a new computation due to application of a formatting function such as #formatnum or a formatting template, one can copy the wikitext and apply the additional computation before the formatting. However, when templates are used, and copying is done to another wiki, these templates have to be copied too, or substituted.

If you want to calculate with Magic words and return group separated results you can use formatnum:
{{formatnum: {{#expr: {{NUMBEROFPAGES:R}} - {{NUMBEROFFILES:R}} }} }} = 1,892,091 (instead of 1892091).

See also

[edit]

References

[edit]
  1. 1.0 1.1 div and mod are different from all programming languages, see phab:T8068
流泪痣是什么意思 白内障是什么引起的 羟苯乙酯是什么 lee属于什么档次 先父遗传是什么意思
糖宝是什么意思 伯爵是什么意思 经血发黑是什么原因 全糖是什么意思 湿热内蕴证有什么症状
亚硝酸钠是什么东西 为什么会得经期综合症 中国中铁是做什么的 饭圈什么意思 背影杀是什么意思
屁多是什么情况 yolo是什么 耳聋吃什么药 头发长的慢是什么原因 泰州有什么好玩的地方
五月初是什么星座hcv8jop0ns9r.cn 月经不调是什么意思hcv9jop4ns3r.cn 苦甲水是什么hcv7jop7ns4r.cn 遭罪是什么意思hcv9jop1ns5r.cn 污蔑是什么意思hcv9jop5ns2r.cn
什么人容易得阿尔兹海默症hcv7jop5ns5r.cn 染什么颜色hcv8jop4ns8r.cn 梦到狗是什么征兆hcv9jop7ns4r.cn 手足口病是什么hcv8jop3ns6r.cn 惠字五行属什么hcv8jop0ns9r.cn
肛门坠胀是什么原因96micro.com 间歇是什么意思hcv9jop2ns7r.cn 儿童热感冒吃什么药hcv9jop1ns1r.cn 月子早餐吃什么好hcv9jop1ns3r.cn 喝醋有什么好处和坏处hcv8jop2ns8r.cn
一个不一个好念什么qingzhougame.com 96345是什么电话hcv8jop6ns1r.cn 一片冰心在玉壶的冰心是什么意思xscnpatent.com 2050年是什么年hcv9jop2ns8r.cn 什么寒什么冻hcv9jop6ns1r.cn
百度