Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
MainPage
Community portal
Recent changes
Random page
Help
Special pages
Donate
Search
Search
English
Appearance
Log in
Request account
Personal tools
Log in
Request account
Export translations
Translate
English
Language statistics
Message group statistics
Export
Tools
Tools
move to sidebar
hide
Actions
Language statistics
Message group statistics
Export
General
Printable version
In other projects
Appearance
move to sidebar
hide
Settings
Group
Category:Maintenance
Help:Blocking users
Help:Bots
Help:Categories
Help:Contents
Help:Deletion and undeletion
Help:Diff
Help:Editing pages
Help:ExpandTemplates
Help:Extension:ParserFunctions
Help:External searches
Help:Formatting
Help:Images
Help:Links
Help:Magic words
Help:Managing files
Help:Moving a page
Help:Namespaces
Help:Navigation
Help:Parser functions in templates
Help:Patrolled edits
Help:Preferences
Help:Protected pages
Help:Random page
Help:Random root page
Help:Range blocks
Help:Recent changes
Help:Redirects
Help:Skins
Help:Special pages
Help:Starting a new page
Help:Substitution
Help:System message
Help:Tables
Help:Talk pages
Help:TemplateData
Help:Templates
Help:TemplateStyles
Help:Tracking changes
Help:Undelete
Help:User contributions
Help:Watchlist
Help:What links here
Manual:Expr parser function syntax
Manual:Extending wiki markup
Manual:Hooks
Manual:Magic words
Manual:Messages API
Manual:Parser functions
Module:Arguments/doc
Module:Int/doc
Module:Message box/configuration/doc
Module:Message box/doc
Module:Template translation/doc
Module:Transcluder/doc
Module:Version/doc
Module:Yesno/doc
News
Parser extension tags
Parser function extensions
Project:Language policy
Template:Admin tip
Template:Advanced user manual
Template:Anontools
Template:ApiEx
Template:Autocat
Template:Auxiliary template common notice
Template:Branching
Template:Bugfix
Template:Bugzilla
Template:Button
Template:Candidate
Template:Caution
Template:Clarify
Template:Collapse top
Template:Component
Template:Deprecated-inline
Template:Description missing
Template:Distinguish
Template:Documentation
Template:Documentation subpage
Template:Done
Template:EmptyCatGood
Template:Escape template list
Template:Excerpt
Template:Extension development
Template:Fixtext
Template:For
Template:Further
Template:Help
Template:High-risk
Template:Historical
Template:I18n navigation
Template:In progress
Template:Intricate template
Template:Introduced-inline
Template:IRC
Template:Localized link
Template:Localized link/messagedoc
Template:Magic words
Template:Main
Template:MainPage
Template:Markup
Template:Markup/row
Template:Mbox templates
Template:Module rating
Template:Move
Template:MW version
Template:MW version/layout
Template:N/a
Template:Navbar
Template:Newarticletext
Template:No
Template:Not done
Template:Note
Template:Optional
Template:Parser functions nav
Template:Partially done
Template:PD Help Page
Template:Phabricator
Template:Ptag
Template:Purge
Template:Recentchangestext
Template:Removed-inline
Template:Required
Template:Rev
Template:Security
Template:See
Template:See also
Template:Sp-contributions-footer
Template:Sp-contributions-footer-anon
Template:Special page
Template:Template shortcut
Template:Template test cases notice
Template:TemplateData header
Template:Thankyou
Template:To do
Template:Tracked
Template:Translatable
Template:Translatable template name
Template:Unusedtemplatestext
Template:Update
Template:Used in system
Template:Watchlists navigation
Template:Why
Template:WikiNonStop News
Template:Yes
TemplateData
Language
aa - Afar
aae - Arbëresh
ab - Abkhazian
abr - Abron
abs - Ambonese Malay
ace - Acehnese
acf - Saint Lucian Creole
acm - Iraqi Arabic
ady - Adyghe
ady-cyrl - Adyghe (Cyrillic script)
aeb - Tunisian Arabic
aeb-arab - Tunisian Arabic (Arabic script)
aeb-latn - Tunisian Arabic (Latin script)
af - Afrikaans
aig - Antiguan and Barbudan Creole English
aln - Gheg Albanian
alt - Southern Altai
am - Amharic
ami - Amis
an - Aragonese
ang - Old English
ann - Obolo
anp - Angika
apc - Levantine Arabic
ar - Arabic
arc - Aramaic
arn - Mapuche
arq - Algerian Arabic
ary - Moroccan Arabic
arz - Egyptian Arabic
as - Assamese
ase - American Sign Language
ast - Asturian
atj - Atikamekw
av - Avaric
avk - Kotava
awa - Awadhi
ay - Aymara
az - Azerbaijani
azb - South Azerbaijani
ba - Bashkir
ban - Balinese
ban-bali - Balinese (Balinese script)
bar - Bavarian
bbc - Batak Toba
bbc-latn - Batak Toba (Latin script)
bcc - Southern Balochi
bci - Baoulé
bcl - Central Bikol
bdr - West Coast Bajau
be - Belarusian
be-tarask - Belarusian (Taraškievica orthography)
bew - Betawi
bg - Bulgarian
bgc - Haryanvi
bgn - Western Balochi
bh - Bhojpuri
bho - Bhojpuri
bi - Bislama
bjn - Banjar
blk - Pa'O
bm - Bambara
bn - Bangla
bo - Tibetan
bpy - Bishnupriya
bqi - Bakhtiari
br - Breton
brh - Brahui
bs - Bosnian
btm - Batak Mandailing
bto - Rinconada Bikol
bug - Buginese
bug-bugi - Buginese (Buginese script)
bxr - Russia Buriat
ca - Catalan
cbk-zam - Chavacano
ccp - Chakma
cdo - Mindong
cdo-hant - Mindong (Traditional Han script)
cdo-latn - Mindong (Latin script)
ce - Chechen
ceb - Cebuano
ch - Chamorro
chn - Chinook Jargon
cho - Choctaw
chr - Cherokee
chy - Cheyenne
ckb - Central Kurdish
co - Corsican
cop - Coptic
cps - Capiznon
cpx - Puxian
cpx-hans - Puxian (Simplified Han script)
cpx-hant - Puxian (Traditional Han script)
cpx-latn - Puxian (Latin script)
cr - Cree
crh - Crimean Tatar
crh-cyrl - Crimean Tatar (Cyrillic script)
crh-latn - Crimean Tatar (Latin script)
crh-ro - Dobrujan Tatar
cs - Czech
csb - Kashubian
cu - Church Slavic
cv - Chuvash
cy - Welsh
da - Danish
dag - Dagbani
de - German
de-at - Austrian German
de-ch - Swiss High German
de-formal - German (formal address)
dga - Southern Dagaare
din - Dinka
diq - Dimli
dlg - Dolgan
dsb - Lower Sorbian
dtp - Central Dusun
dty - Doteli
dua - Duala
dv - Divehi
dz - Dzongkha
ee - Ewe
efi - Efik
egl - Emilian
el - Greek
eml - Emiliano-Romagnolo
en - English
en-ca - Canadian English
en-gb - British English
eo - Esperanto
es - Spanish
es-419 - Latin American Spanish
es-formal - Spanish (formal address)
et - Estonian
eu - Basque
ext - Extremaduran
fa - Persian
fat - Fanti
ff - Fula
fi - Finnish
fit - Tornedalen Finnish
fj - Fijian
fo - Faroese
fon - Fon
fr - French
frc - Cajun French
frp - Arpitan
frr - Northern Frisian
fur - Friulian
fvr - Fur
fy - Western Frisian
ga - Irish
gaa - Ga
gag - Gagauz
gan - Gan
gan-hans - Gan (Simplified Han script)
gan-hant - Gan (Traditional Han script)
gcf - Guadeloupean Creole
gcr - Guianan Creole
gd - Scottish Gaelic
gl - Galician
gld - Nanai
glk - Gilaki
gn - Guarani
gom - Goan Konkani
gom-deva - Goan Konkani (Devanagari script)
gom-latn - Goan Konkani (Latin script)
gor - Gorontalo
got - Gothic
gpe - Ghanaian Pidgin
grc - Ancient Greek
gsw - Alemannic
gu - Gujarati
guc - Wayuu
gur - Frafra
guw - Gun
gv - Manx
ha - Hausa
hak - Hakka Chinese
hak-hans - Hakka (Simplified Han script)
hak-hant - Hakka (Traditional Han script)
hak-latn - Hakka (Latin script)
haw - Hawaiian
he - Hebrew
hi - Hindi
hif - Fiji Hindi
hif-latn - Fiji Hindi (Latin script)
hil - Hiligaynon
hke - Hunde
hno - Northern Hindko
ho - Hiri Motu
hoc-latn - Ho (Latin script)
hr - Croatian
hrx - Hunsrik
hsb - Upper Sorbian
hsn - Xiang
ht - Haitian Creole
hu - Hungarian
hu-formal - Hungarian (formal address)
hy - Armenian
hyw - Western Armenian
hz - Herero
ia - Interlingua
iba - Iban
ibb - Ibibio
id - Indonesian
ie - Interlingue
ig - Igbo
igl - Igala
ii - Sichuan Yi
ik - Inupiaq
ike-cans - Eastern Canadian (Aboriginal syllabics)
ike-latn - Eastern Canadian (Latin script)
ilo - Iloko
inh - Ingush
io - Ido
is - Icelandic
isv-cyrl - Interslavic (Cyrillic script)
isv-latn - Interslavic (Latin script)
it - Italian
iu - Inuktitut
ja - Japanese
jam - Jamaican Creole English
jbo - Lojban
jut - Jutish
jv - Javanese
ka - Georgian
kaa - Kara-Kalpak
kab - Kabyle
kai - Karekare
kaj - Jju
kbd - Kabardian
kbd-cyrl - Kabardian (Cyrillic script)
kbp - Kabiye
kcg - Tyap
kea - Kabuverdianu
kg - Kongo
kge - Komering
khw - Khowar
ki - Kikuyu
kiu - Kirmanjki
kj - Kuanyama
kjh - Khakas
kjp - Eastern Pwo
kk - Kazakh
kk-arab - Kazakh (Arabic script)
kk-cn - Kazakh (China)
kk-cyrl - Kazakh (Cyrillic script)
kk-kz - Kazakh (Kazakhstan)
kk-latn - Kazakh (Latin script)
kk-tr - Kazakh (Turkey)
kl - Kalaallisut
km - Khmer
kn - Kannada
knc - Central Kanuri
ko - Korean
ko-kp - Korean (North Korea)
koi - Komi-Permyak
kr - Kanuri
krc - Karachay-Balkar
kri - Krio
krj - Kinaray-a
krl - Karelian
ks - Kashmiri
ks-arab - Kashmiri (Arabic script)
ks-deva - Kashmiri (Devanagari script)
ksh - Colognian
ksw - S'gaw Karen
ku - Kurdish
ku-arab - Kurdish (Arabic script)
ku-latn - Kurdish (Latin script)
kum - Kumyk
kus - Kusaal
kv - Komi
kw - Cornish
ky - Kyrgyz
la - Latin
lad - Ladino
lb - Luxembourgish
lbe - Lak
lez - Lezghian
lfn - Lingua Franca Nova
lg - Ganda
li - Limburgish
lij - Ligurian
liv - Livonian
ljp - Lampung Api
lki - Laki
lld - Ladin
lmo - Lombard
ln - Lingala
lo - Lao
loz - Lozi
lrc - Northern Luri
lt - Lithuanian
ltg - Latgalian
lua - Luba-Lulua
lus - Mizo
luz - Southern Luri
lv - Latvian
lzh - Literary Chinese
lzz - Laz
mad - Madurese
mag - Magahi
mai - Maithili
map-bms - Banyumasan
mdf - Moksha
mg - Malagasy
mh - Marshallese
mhr - Eastern Mari
mi - Māori
min - Minangkabau
mk - Macedonian
ml - Malayalam
mn - Mongolian
mnc - Manchu
mnc-latn - Manchu (Latin script)
mnc-mong - Manchu (Mongolian script)
mni - Manipuri
mnw - Mon
mo - Moldovan
mos - Mossi
mr - Marathi
mrh - Mara
mrj - Western Mari
ms - Malay
ms-arab - Malay (Jawi script)
mt - Maltese
mui - Musi
mus - Muscogee
mwl - Mirandese
my - Burmese
myv - Erzya
mzn - Mazanderani
na - Nauru
nah - Nahuatl
nan - Minnan
nan-hant - Minnan (Traditional Han script)
nan-latn-pehoeji - Minnan (Pe̍h-ōe-jī)
nan-latn-tailo - Minnan (Tâi-lô)
nap - Neapolitan
nb - Norwegian Bokmål
nds - Low German
nds-nl - Low Saxon
ne - Nepali
new - Newari
ng - Ndonga
nia - Nias
nit - Southeastern Kolami
niu - Niuean
nl - Dutch
nl-informal - Dutch (informal address)
nmz - Nawdm
nn - Norwegian Nynorsk
no - Norwegian
nod - Northern Thai
nog - Nogai
nov - Novial
nqo - N’Ko
nr - South Ndebele
nrm - Norman
nso - Northern Sotho
nup - Nupe
nv - Navajo
ny - Nyanja
nyn - Nyankole
nyo - Nyoro
nys - Nyungar
oc - Occitan
ojb - Northwestern Ojibwa
olo - Livvi-Karelian
om - Oromo
or - Odia
os - Ossetic
pa - Punjabi
pag - Pangasinan
pam - Pampanga
pap - Papiamento
pap-aw - Papiamento (Aruba)
pcd - Picard
pcm - Nigerian Pidgin
pdc - Pennsylvania German
pdt - Plautdietsch
pfl - Palatine German
pi - Pali
pih - Pitcairn-Norfolk
pl - Polish
pms - Piedmontese
pnb - Western Punjabi
pnt - Pontic
prg - Prussian
ps - Pashto
pt - Portuguese
pt-br - Brazilian Portuguese
pwn - Paiwan
qqq - Message documentation
qu - Quechua
qug - Chimborazo Highland Quichua
rgn - Romagnol
rif - Riffian
rki - Arakanese
rm - Romansh
rmc - Carpathian Romani
rmy - Vlax Romani
rn - Rundi
ro - Romanian
roa-tara - Tarantino
rsk - Pannonian Rusyn
ru - Russian
rue - Rusyn
rup - Aromanian
ruq - Megleno-Romanian
ruq-cyrl - Megleno-Romanian (Cyrillic script)
ruq-latn - Megleno-Romanian (Latin script)
rut - Rutul
rw - Kinyarwanda
ryu - Okinawan
sa - Sanskrit
sah - Yakut
sas - Sasak
sat - Santali
sc - Sardinian
scn - Sicilian
sco - Scots
sd - Sindhi
sdc - Sassarese Sardinian
sdh - Southern Kurdish
se - Northern Sami
se-fi - Northern Sami (Finland)
se-no - Northern Sami (Norway)
se-se - Northern Sami (Sweden)
sei - Seri
ses - Koyraboro Senni
sg - Sango
sgs - Samogitian
sh - Serbo-Croatian
sh-cyrl - Serbo-Croatian (Cyrillic script)
sh-latn - Serbo-Croatian (Latin script)
shi - Tachelhit
shi-latn - Tachelhit (Latin script)
shi-tfng - Tachelhit (Tifinagh script)
shn - Shan
shy - Shawiya
shy-latn - Shawiya (Latin script)
si - Sinhala
simple - Simple English
sjd - Kildin Sami
sje - Pite Sami
sk - Slovak
skr - Saraiki
skr-arab - Saraiki (Arabic script)
sl - Slovenian
sli - Lower Silesian
sm - Samoan
sma - Southern Sami
smn - Inari Sami
sms - Skolt Sami
sn - Shona
so - Somali
sq - Albanian
sr - Serbian
sr-ec - Serbian (Cyrillic script)
sr-el - Serbian (Latin script)
srn - Sranan Tongo
sro - Campidanese Sardinian
ss - Swati
st - Southern Sotho
stq - Saterland Frisian
sty - Siberian Tatar
su - Sundanese
sv - Swedish
sw - Swahili
syl - Sylheti
szl - Silesian
szy - Sakizaya
ta - Tamil
tay - Atayal
tcy - Tulu
tdd - Tai Nuea
te - Telugu
tet - Tetum
tg - Tajik
tg-cyrl - Tajik (Cyrillic script)
tg-latn - Tajik (Latin script)
th - Thai
ti - Tigrinya
tig - Tigre
tk - Turkmen
tl - Tagalog
tly - Talysh
tly-cyrl - Talysh (Cyrillic script)
tn - Tswana
to - Tongan
tok - Toki Pona
tpi - Tok Pisin
tr - Turkish
tru - Turoyo
trv - Taroko
ts - Tsonga
tt - Tatar
tt-cyrl - Tatar (Cyrillic script)
tt-latn - Tatar (Latin script)
ttj - Tooro
tum - Tumbuka
tw - Twi
ty - Tahitian
tyv - Tuvinian
tzm - Central Atlas Tamazight
udm - Udmurt
ug - Uyghur
ug-arab - Uyghur (Arabic script)
ug-latn - Uyghur (Latin script)
uk - Ukrainian
ur - Urdu
uz - Uzbek
uz-cyrl - Uzbek (Cyrillic script)
uz-latn - Uzbek (Latin script)
ve - Venda
vec - Venetian
vep - Veps
vi - Vietnamese
vls - West Flemish
vmf - Main-Franconian
vmw - Makhuwa
vo - Volapük
vot - Votic
vro - Võro
wa - Walloon
wal - Wolaytta
war - Waray
wls - Wallisian
wlx - Wali
wo - Wolof
wuu - Wu
wuu-hans - Wu (Simplified Han script)
wuu-hant - Wu (Traditional Han script)
xal - Kalmyk
xh - Xhosa
xmf - Mingrelian
xsy - Saisiyat
yi - Yiddish
yo - Yoruba
yrl - Nheengatu
yua - Yucatec Maya
yue - Cantonese
yue-hans - Cantonese (Simplified Han script)
yue-hant - Cantonese (Traditional Han script)
za - Zhuang
zea - Zeelandic
zgh - Standard Moroccan Tamazight
zgh-latn - Standard Moroccan Tamazight (Latin script)
zh - Chinese
zh-cn - Chinese (China)
zh-hans - Simplified Chinese
zh-hant - Traditional Chinese
zh-hk - Chinese (Hong Kong)
zh-mo - Chinese (Macau)
zh-my - Chinese (Malaysia)
zh-sg - Chinese (Singapore)
zh-tw - Chinese (Taiwan)
zu - Zulu
Format
Export for off-line translation
Export in native format
Export in CSV format
Fetch
<languages/> {{move|Help:Extension:ParserFunctions/Expr parser function syntax}} {{Magic words}} <span lang="en" dir="ltr" class="mw-content-ltr">The <code>#expr</code> and <code>#ifexpr</code> functions of MediaWiki's {{ll|Extension:ParserFunctions|ParserFunctions}} extension evaluate [[w:Expression (mathematics)|mathematical]] and [[w:Boolean expression|Boolean]] expressions—that is, expressions involving numbers and boolean values.</span> <span lang="en" dir="ltr" class="mw-content-ltr">It does not work with arbitrary [[w:String (computer science)|strings]], as do most of the {{ll|Help:Extension:ParserFunctions|other parser functions}}.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The syntax of the <code>#expr</code> function is:</span> :<code><nowiki>{{</nowiki> #expr: ''expression'' <nowiki>}}</nowiki></code> <span lang="en" dir="ltr" class="mw-content-ltr">Spaces are not needed, except between two adjacent words (for operators and constants).</span> <span lang="en" dir="ltr" class="mw-content-ltr">Inside numbers, no grouping separators, also known as [[w:Decimal_separator|decimal]] and [[w:Thousands_separators|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 [[w:ASCII|ASCII]] decimal digits 0–9.</span><!-- it doesn't support any "higher" Unicode characters equivalent to the same digits, right? --> <span lang="en" dir="ltr" class="mw-content-ltr">In expressions embedded within templates, be careful when using {{ll|Magic words|magic words}} for date and time elements, such as <code><nowiki>{{CURRENTHOUR}}</nowiki></code> as their return value may be formatted differently on translated pages, which will break expressions (instead, use the parser function <code>{{ll|Help:Extension:ParserFunctions##time|2=<nowiki/>#time}}</code> with the appropriate <code>xn</code> flags before each formatting element).</span> <span lang="en" dir="ltr" class="mw-content-ltr">Similar errors will occur with some other magic words that return quantities (make sure you include the appropriate ''raw formatting'' flag).</span> <div lang="en" dir="ltr" class="mw-content-ltr"> ==General== </div> <span lang="en" dir="ltr" class="mw-content-ltr">Technically, an expression is a string representing a [[w:Tree structure|tree structure]] with [[w:Data type|type]]/[[w:Value (computer science)|value]] pairs as nodes, with [[w:Binary operation|binary operators]] in [[w:Infix notation|infix notation]], [[w:Unary operation|unary operators]] in [[w:Polish notation|prefix notation]], and end nodes represented by numbers and constants.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Letters in operators and names of constants are [[w:Case sensitivity|case-insensitive]].</span> The {{ll|Extension:ParserFunctions|ParserFunctions}} extension determines which operators and constants, and what numbers, are supported. <span lang="en" dir="ltr" class="mw-content-ltr">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.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The extension also determines the precedence of the operators and the error messages, and it converts all literal numbers to [[w:Floating point|floating-point values]].</span> <span lang="en" dir="ltr" class="mw-content-ltr">For the rest of the implementation, the extension uses [[w:PHP|PHP]] functions and operators, so any type conversions and peculiarities of certain operators are the properties of those PHP functions and operators themselves.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Also, the format of results is entirely determined by PHP.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The only data types used are the PHP data types [https://php.net/language.types.float float] ([[w:Double precision floating-point format|double precision floating-point format]]) and [https://php.net/language.types.integer integer] (64-bit [[w:Integer (computer science)|integer]]).</span> The range for type integer is from −2<sup>63</sup> = −9,223,372,036,854,775,808 through 2<sup>63</sup> − 1 = [[w:Power_of_two#2^63-1|9,223,372,036,854,775,807]]. Type float allows fractions and very large numbers, but only in the range ±2<sup>53</sup> = ±{{formatnum:{{#expr:trunc(2^53)}}}} can all integer values be exactly represented in type float (see {{ll|Manual:Representation of numbers in expr parser function}}). <span lang="en" dir="ltr" class="mw-content-ltr">[[w:Type system#Dynamic typing|Dynamic typing]] is applied.</span> <span lang="en" dir="ltr" class="mw-content-ltr">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).</span> <span lang="en" dir="ltr" class="mw-content-ltr">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).</span> <span lang="en" dir="ltr" class="mw-content-ltr">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:</span> *{{xpsoc|numfh|(trunc2^trunc62+trunc512)-2^62}} *{{xpsoc|numfh|(trunc2^trunc62+trunc512)+(trunc2^trunc62+trunc1535)}} *{{xpsoc|numfh|2^63+2047}} Apart from that, a numerical value outside the range of type integer is converted to float, except in the case of <code>trunc</code> (and <code>mod</code>, which involves applying <code>trunc</code> to the arguments first). <div lang="en" dir="ltr" class="mw-content-ltr"> There are 31 operators (excluding two synonyms), two constants, and the unsigned numbers in ordinary decimal notation. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> == Operators, numbers, and constants == </div> Since literal numbers are of type float, <code>trunc</code> is sometimes used in the examples to construct an integer-type argument, to demonstrate the result of an operator for this case. {| class="wikitable sortable" ! ''<span lang="en" dir="ltr" class="mw-content-ltr">Operator</span>'' ! ''<abbr title="number of arguments">Args</abbr>'' ! ''<span lang="en" dir="ltr" class="mw-content-ltr">Operation</span>'' ! ''<span lang="en" dir="ltr" class="mw-content-ltr">PHP</span>'' ! ''<span lang="en" dir="ltr" class="mw-content-ltr">Data type</span>'' ! ''<abbr title="Precedence">Prec</abbr>'' ! ''<span lang="en" dir="ltr" class="mw-content-ltr">Examples</span>'' |- | (number) || 0 | <span lang="en" dir="ltr" class="mw-content-ltr">unsigned number in ordinary decimal notation (unary plus and minus and e are treated as operators, see elsewhere in this table)</span> | [https://php.net/function.floatval <span lang="en" dir="ltr" class="mw-content-ltr">floatval</span>] || <span lang="en" dir="ltr" class="mw-content-ltr">float</span> || n.a. | {| class="wikitable" | {{xpett|1234567890123456789}} |- | {{xpett|123456789.0123456789}} |} |- | '''<code>+</code>''' || 1 | <span lang="en" dir="ltr" class="mw-content-ltr">[[w:Unary operation#Unary negative and positive|unary <code>+</code> sign]]</span> | <span lang="en" dir="ltr" class="mw-content-ltr">(nothing)</span> || <span lang="en" dir="ltr" class="mw-content-ltr">same as argument</span> || n.a. || {| class="wikitable" {{xpettu|+|1}} {{xpettu|+|-1}} {{xpettu|+|trunc1}} |} |- | <code>'''-'''</code> || 1 | <span lang="en" dir="ltr" class="mw-content-ltr">[[w:Unary operation#Unary negative and positive|unary <code>-</code> sign]] (negation)</span> | [https://php.net/language.operators.arithmetic -] || same as argument|| 10 || {| class="wikitable" {{xpettu|-|12}} {{xpettu|-|trunc12}} {{xpettu|-|trunc(-2^63)}} |} |- | '''<code>pi</code>''' || 0 | <span lang="en" dir="ltr" class="mw-content-ltr">constant [[w:pi|π]]</span> || [https://php.net/function.pi pi]() || float || n.a. | {| class="wikitable" | {{xpett|pi}} |} |- | '''<code>e</code>''' as subexpression || 0 | constant [[w:e (mathematical constant)|e]] || [https://php.net/function.exp exp](1) || float || n.a. | {| class="wikitable" | {{xpett|e}} |} |- | '''<code>e</code>''' between subexpressions|| 2 || *10^ || [https://php.net/language.operators.arithmetic *] [https://php.net/function.pow pow] (10,..) || float unless the factor on the left is of type integer and the exponent is non-negative and of type integer || 10 | {| class="wikitable" {{xpettb|2|e|3}} {{xpettb|-2.3|e|-4}} {{xpettb|(trunc2)|e|(trunc-3)}} {{xpettb|(trunc2)|e|(trunc0)}} {{xpettb|(trunc2)|e|(trunc18)}} {{xpettb|(trunc2)|e|(trunc19)}} {{xpett|6e(5-2)e-2}} {{xpett|1e.5}} |} <span lang="en" dir="ltr" class="mw-content-ltr">Wrong:</span> {| class="wikitable" |{{xpett|e4}} |} |- | '''<code>exp</code>''' || 1 | [[w:Exponential function|exponential function e<sup>x</sup>]] | [https://php.net/function.exp exp] || float || 9 || {| class="wikitable" {{xpettu|exp|43}} {{xpettu|exp| trunc0}} {{xpettu|exp|709}} {{xpettu|exp|-744}} |} Compare: {| class="wikitable" |{{xpett|e^43}} |- |{{xpett|trunc exp43}} |} |- | '''<code>ln</code>''' || 1 | [[w:Natural logarithm|natural logarithm]] | [https://php.net/function.log log] || float || 9 || {| class="wikitable" {{xpettu|ln|2}} {{xpettu|ln| trunc1}} {{xpettu|ln|8.9e307}} {{xpettu|ln|.5e-323}} |} Hence, the common logarithm of e.g. 2: {| class="wikitable" |{{xpett|ln2/ln10}} |} |- | '''<code>abs</code>''' || 1 | [[w:Absolute value|absolute value]] | [https://php.net/function.abs abs] || same as argument, but never negative || 9 || {| class="wikitable" {{xpettu|abs|-2}} {{xpettu|abs| trunc-2}} {{xpettu|abs| trunc-2^63}} |} |- | '''<code>sqrt</code>''' || 1 | [[w:Square root|square root]] | [https://php.net/function.sqrt sqrt] || float || 9 || {| class="wikitable" {{xpettu|sqrt| 4}} {{xpettu|sqrt| 2}} {{xpettu|sqrt| 1e19}} |} Negative arguments are not permitted: {| class="wikitable" {{xpettu|sqrt|-1}} |} |- | '''<code>trunc</code>''' || 1 | [[w:Truncation|truncation]] | [https://php.net/language.types.integer#language.types.integer.casting (int)], i.e. [[w:Type conversion|type-casting]] to integer || integer || 9 || {| class="wikitable" {{xpettu|trunc|1.2}} {{xpettu|trunc|1.8}} {{xpettu|trunc|-1.2}} {{xpettu|trunc|(-2^64+1e5)}} {{xpettu|trunc|(-2^63+1e5)}} {{xpettu|trunc|(2^63)}} {{xpettu|trunc|(2^63+1e5)}} {{xpettu|trunc|(2^64+1e5)}} |} |- | '''<code>floor</code>''' || 1 | [[w:Floor function|floor function]] | [https://php.net/function.floor floor] || float || 9 || {| class="wikitable" {{xpettu|floor|1.2}} {{xpettu|floor|-1.2}} {{xpettu|floor| trunc3}} |} |- | '''<code>ceil</code>''' || 1 | [[w:Ceiling function|ceiling function]] | [https://php.net/function.ceil ceil] || float || 9 || {| class="wikitable" {{xpettu|ceil|1.2}} {{xpettu|ceil|-1.2}} {{xpettu|ceil| trunc3}} |} |- | '''<code>sin</code>''' || 1 | [[w:Sine function|sine]] | [https://php.net/function.sin sin] || float || 9 || {| class="wikitable" {{xpettu|sin|.1}} {{xpettu|sin| trunc1}} |} With an angle in degrees, e.g. 30°: {| class="wikitable" |{{xpett|sin(30*pi/180)}} |} |- | '''<code>cos</code>''' || 1 | [[w:Cosine function|cosine]] | [https://php.net/function.cos cos] || float || 9 || {| class="wikitable" {{xpettu|cos|.1}} {{xpettu|cos| trunc1}} |} |- | '''<code>tan</code>''' || 1 | [[w:Tangent function|tangent]] | [https://php.net/function.tan tan] || float || 9 || {| class="wikitable" {{xpettu|tan|.1}} {{xpettu|tan| trunc1}} |} |- | '''<code>asin</code>''' || 1 | [[w:Arcsine|arcsine]] | [https://php.net/function.asin asin] || float || 9 || {| class="wikitable" {{xpettu|asin|.1}} {{xpettu|asin| trunc1}} |} |- | '''<code>acos</code>''' || 1 | [[w:Arccosine|arccosine]] | [https://php.net/function.acos acos] || float || 9 || {| class="wikitable" {{xpettu|acos|.1}} {{xpettu|acos| trunc1}} {{xpettu|2*|acos 0}} |} |- | '''<code>atan</code>''' || 1 | [[w:Arctangent|arctangent]] | [https://php.net/function.atan atan] || float || 9 || {| class="wikitable" {{xpettu|atan|.1}} {{xpettu|atan| trunc1}} {{xpettu|4*|atan 1}} |} |- | '''<code>not</code>''' || 1 | [[w:Negation|negation]], logical NOT | [https://php.net/language.operators.logical !] || integer (1 or 0) || 9 || {| class="wikitable" {{xpettu|not|0}} {{xpettu|not|1}} {{xpettu|not|2}} {{xpettu|not| trunc1}} |} |- | '''<code>^</code>''' || 2 | [[w:Exponentiation|exponentiation]] (power) | [https://php.net/function.pow pow] || float unless the base is of type integer and the exponent is non-negative and of type integer || 8 || {| class="wikitable" {{xpettb|2|^|3}} {{xpettb|-2|^|3}} {{xpettb|-2|^|4}} {{xpettb|(trunc2)|^|(trunc-3)}} {{xpettb|(trunc2)|^|(trunc0)}} {{xpettb|(trunc2)|^|(trunc62)}} {{xpettb|(trunc2)|^|(trunc63)}} {{xpettb|(-2)|^|1.2}} {{xpettb|(-2)|^|.5}} |} |- | '''<code>*</code>''' || 2 | [[w:Multiplication|multiplication]] | [https://php.net/language.operators.arithmetic *] || integer if both arguments are integer, otherwise float || 7 || {| class="wikitable" {{xpettb|2|*|3}} {{xpettb|(trunc2)|*|3}} {{xpettb|2|*|trunc3}} {{xpettb|(trunc2)|*|trunc3}} {{xpettb|(trunc1e10)|*|trunc1e9}} |} |- | '''<code>/</code>''' (also written '''div''') || 2 || [[w:Division (mathematics)|division]] (div is not integer division<ref name="divmod">'''<code>div</code>''' and '''<code>mod</code>''' are different from all programming languages, see [[phab:T8068]]</ref>) | [https://php.net/language.operators.arithmetic /] || float, unless both arguments are integer and the mathematical result is an integer || 7 || {| class="wikitable" {{xpettb|6|/|3}} {{xpettb|(trunc6)|/|3}} {{xpettb|2|/|trunc6}} {{xpettb|(trunc6)|/|trunc3}} {{xpettb|(trunc6)|/|trunc4}} |} |- | '''<code>mod</code>''' || 2 | [[w:Modulo operation|modulo]] operation, remainder of division after truncating both operands to an integer.<ref name="divmod"/> | [https://php.net/language.operators.arithmetic %] || integer || 7 || {| class="wikitable" {{xpettb|30|mod|7}} {{xpettb|-30|mod|7}} {{xpettb|30|mod|-7}} {{xpettb|-30|mod|-7}} {{xpettb|30.5|mod|7.9}} |} May give unexpected results for some values of the second argument (see [[#Limitations and workarounds|this section]]): {| class="wikitable" {{xpettb|123|mod|2^64}} |} |- | '''<code>fmod</code>''' || 2 | [[w:Modulo operation|modulo]] operation, floating point. Returns first argument after subtracting an integer multiple of the second argument. | [https://php.net/function.fmod fmod] || float|| 7 || {| class="wikitable" {{xpettb|5.7|fmod|1.3}} {{xpettb|99.9|fmod|60}} {{xpettb|2.99|fmod|1}} {{xpettb|-2.99|fmod|1}} {{xpettb|2.99|fmod|-1}} {{xpettb|-2.99|fmod|-1}} |} |- | '''<code>+</code>''' || 2 || [[w:Addition|addition]] | [https://php.net/language.operators.arithmetic +] || integer if both arguments are integer, otherwise float || 6 || {| class="wikitable" {{xpettb|2|+|3}} {{xpettb|(trunc2)|+|3}} {{xpettb|2|+|trunc3}} {{xpettb|(trunc2)|+|trunc3}} {{xpettb|(trunc7e18)|+|trunc4e18}} |} |- | '''<code>-</code>''' || 2 || [[w:Subtraction|subtraction]] | [https://php.net/language.operators.arithmetic -] || integer if both arguments are integer, otherwise float || 6 || {| class="wikitable" {{xpettb|3|-|2}} {{xpettb|(trunc3)|-|2}} {{xpettb|2|-|trunc2}} {{xpettb|(trunc3)|-|trunc2}} {{xpettb|(trunc-7e18)|-|trunc4e18}} |} |- | '''<code>round</code>''' || 2 || [[w:rounding|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 | [https://php.net/function.round round] || float || 5 || {| class="wikitable" {{xpettb|9.876|round|2}} {{xpettb|(trunc1234)|round| trunc-2}} {{xpettb|4.5|round|0}} {{xpettb|-4.5|round|0}} {{xpettb|46.857|round|1.8}} {{xpettb|46.857|round|-1.8}} |} |- | '''<code>=</code>''' || 2 || [[w:Equality (mathematics)|equality]] (numerical incl. logical, not for strings) | [https://php.net/language.operators.comparison ==] || integer (1 or 0) || 4 || {| class="wikitable" {{xpettb|3.0|{{=}}|3}} {{xpettb|3.1|{{=}}|3}} {{xpettb|3.0|{{=}}|trunc3}} {{xpettb|3.1|{{=}}|trunc3}} {{xpettb|1e16|{{=}}|trunc(1e16)}} {{xpettb|1e16|{{=}}|trunc(1e16)+trunc1}} {{xpettb|trunc(1e16)|{{=}}|trunc(1e16)+trunc1}} |} wrong: {| class="wikitable" |{{xpett|a{{=}}a}} |} |- | '''<code><></code>''' (also written '''!=''') || 2 | [[w:Inequality|inequality]], logical ''xor''; not for strings (negation of =) | [https://php.net/language.operators.comparison !=] || integer (1 or 0) || 4 || {| class="wikitable" {{xpettb|3|<>|3}} {{xpettb|3|<>|4}} |} |- | '''<code><</code>''' || 2 || [[w:Inequality (mathematics)|less than]] (not for ordering of strings) | [https://php.net/language.operators.comparison <] || integer (1 or 0) || 4 || {| class="wikitable" {{xpettb|3|<|3}} {{xpettb|3|<|4}} {{xpettb|2.9|<|3}} {{xpettb|3.0|<|3}} {{xpettb|2.9|<|trunc3}} {{xpettb|3.0|<|trunc3}} {{xpettb|1e16|<|trunc(1e16)+trunc1}} |} Wrong: {| class="wikitable" {{xpettb|a|<|b}} |} |- | '''<code>></code>''' || 2 || [[w:Inequality (mathematics)|greater than]] (same as <, with arguments reversed) | [https://php.net/language.operators.comparison >] || integer (1 or 0) || 4 || {| class="wikitable" {{xpettb|4|>|3}} {{xpettb|3|>|3}} |} |- | '''<code><=</code>''' || 2 | [[w:Inequality (mathematics)|less than or equal to]] (same as >=, with arguments reversed) | [https://php.net/language.operators.comparison <=] || integer (1 or 0) || 4 || {| class="wikitable" {{xpettb|3|<{{=}}|4}} {{xpettb|3|<{{=}}|3}} |} |- | '''<code>>=</code>''' || 2 | [[w:Inequality (mathematics)|greater than or equal to]] (negation of <) | [https://php.net/language.operators.logical >=] || integer (1 or 0) || 4 || {| class="wikitable" {{xpettb|4|>{{=}}|3}} {{xpettb|3|>{{=}}|3}} |} |- | '''<code>and</code>''' || 2 | [[w:Logical conjunction|logical AND]] | [https://php.net/language.operators.logical &&] || integer (1 or 0) || 3 || {| class="wikitable" {{xpettb|3|and|4}} {{xpettb|-3|and|0}} {{xpettb|0|and|4}} {{xpettb|0|and|0}} |} |- | '''<code>or</code>''' || 2 | [[w:Logical disjunction|logical OR]] | [https://php.net/language.operators.logical <nowiki>||</nowiki>] || integer (1 or 0) || 2 || {| class="wikitable" {{xpettb|3|or|4}} {{xpettb|-3|or|0}} {{xpettb|0|or|4}} {{xpettb|0|or|0}} |} |} The logical operators <code>and</code>, <code>or</code>, and <code>not</code> 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 <code><nowiki>{{#expr: (2 < 3) + 1}}</nowiki></code> gives {{#expr: (2 < 3) + 1}}. To use <code>and</code>, <code>or</code>, and <code>not</code> in <code>#if</code>, <code>#ifeq</code>, or <code>#ifexist</code> 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 <code>#expr</code> or <code>#ifexpr</code>. Note that negation can also be achieved by subtracting the result from 1 (inside of <code>#expr</code> or <code>#ifexpr</code>) or by simply switching the then- and else-text(s) of <code>#if</code> <code>#ifeq</code> <code>#ifexists</code> or <code>#ifexpr</code>. Also, note that the construct <code><nowiki>{{#expr: {{#if:{{{a|}}}|1|0}} or {{#if:{{{b|}}}|1|0}} }}</nowiki></code> is equivalent to the simpler <code><nowiki>{{#if:{{{a|}}}{{{b|}}}|1|0}}}}</nowiki></code>. <span lang="en" dir="ltr" class="mw-content-ltr">Precedence is indicated in the "Prec" column above, a higher number means that the operator is applied earlier.</span> Examples (">" refers to going before, "~" means application from left to right): * <code>e</code> > <code>floor</code>, <code>not</code>, etc.: {{xpsoc|#expr:floor1.5e1}}, {{xpsoc|#expr:not0e1}} * <code>floor</code> > <code>^</code>: {{xpsoc|#expr:floor1.5^2}} * <code>^</code> > <code>*</code>: {{xpsoc|#expr:2*3^2}} * <code>*</code> ~ <code>/</code> ~ <code>mod</code>: {{xpsoc|#expr:12/3*2}}, {{xpsoc|#expr:111/3mod10}}, {{xpsoc|#expr:358mod10*2}}, * <code>*</code> > <code>+</code>, <code>-</code>: {{xpsoc|#expr:2+3*4}}, {{xpsoc|#expr:2-3*4}} * <code>+</code> ~ <code>-</code>: {{xpsoc|#expr:6-2+3}}, {{xpsoc|#expr:-2+3}} * <code>+</code>, <code>-</code> > <code>round</code>: {{xpsoc|#expr:1.234round2-1}} * <code>round</code> > <code>=</code> etc.: {{xpsoc|1=#expr:1.23=1.234round2}} * <code>=</code> etc. > <code>and</code>: {{xpsoc|1=#expr:1 and 2=1}} * <code>and</code> > <code>or</code>: {{xpsoc|1=#expr:1 or 1 and 0}} In the case of equal precedence number, evaluation is from left to right: *{{xpsoc|#expr:12/2*3}} *{{xpsoc|#expr:3^3^3}} Parentheses can force a different precedence: {{xpsoc|#expr:(2+3)*4}} Blank spaces are good for readability but not needed for working properly, except between words (including "<code>e</code>"), and not allowed within numbers: *{{xpd0|d=|#expr:7mod3}} *{{xpd0|d=|#expr:7.5round0}} *{{xpd0|d=|#expr:0and1}} *{{xpd0|d=|#expr:0or not0}} *{{xpd0|d=|#expr:0ornot0}} *{{xpd0|d=|#expr:123 456}} *{{xpd0|d=|#expr:not not3}} *{{xpd0|d=|#expr:notnot3}} *{{xpd0|d=|#expr:---2}} *{{xpd0|d=|#expr:-+-2}} *{{xpd0|d=|#expr:2*-3}} *{{xpd0|d=|#expr:-not-not-not0}} *{{xpd0|d=|#expr:2*/3}} *{{xpd0|d=|#expr:sinln1.1}} *{{xpd0|d=|#expr:sin ln1.1}} For scientific notation <code>e</code> is treated as an operator. An <code>e</code> between subexpressions works just like <code>*10^</code>, except that together with the unary minus it has the highest precedence (e.g., before a separate <code>^</code>), and that the implicit 10 is of type integer, not float. An <code>e</code> as subexpression (i.e., with each side either nothing or an operator) is [[w:Euler's constant|Euler's constant]]. An <code>e</code> with nothing or an operator on one side and a subexpression on the other gives an error message. === Rounding operators === {{hatnote|See also [[Manual:Rounding numbers]] for some more examples}} The following rounding operators are supported: * Uniformly distributed around 0 and [[w:Strict|non-strictly]] [[w:monotonic|monotonic function]]: ** ceil ** floor * 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 ==== Trunc converts a float to an integer by cutting off the decimal part. For values in the range <code>2^63 ≤ x ≤ 2^64</code>, it returns <code>x - 2^64</code>. Values larger than <code>2^64</code> returns <code>0</code>, and values less than <code>-2^63</code> returns <code>-2^63</code>. See the below examples: *{{xpsoc|#expr:trunc(-2*2^63-2^12)}} *{{xpsoc|#expr:trunc(-2*2^63+2^12)}} *{{xpsoc|#expr:trunc(-1*2^63-2^12)}} *{{xpsoc|#expr:trunc(-1*2^63+2^12)}} *{{xpsoc|#expr:trunc(0*2^63-2^12)}} *{{xpsoc|#expr:trunc(0*2^63+2^12)}} *{{xpsoc|#expr:trunc(1*2^63-2^12)}} *{{xpsoc|#expr:trunc(1*2^63+2^12)}} *{{xpsoc|#expr:trunc(2*2^63-2^12)}} *{{xpsoc|#expr:trunc(2*2^63+2^12)}} *{{xpsoc|#expr:trunc(2^64+1024)}} *{{xpsoc|#expr:trunc(3*2^63-2^12)}} *{{xpsoc|#expr:trunc(3*2^63+2^12)}} Converting a number to an integer type ensures precise display without rounding to 14 decimal places. x is truncated, <code>x = trunc x</code>, if it is an integer or a floating-point number representing an integer value. The expression <code>p mod 0</code> results in an error message when used in MediaWiki, not the PHP operator %: *{{xpsoc|#expr:-27mod0}} In PHP, when using the % operator, if <code>p % 0</code> is calculated and conditions are met where <code>0 < |q| < 1</code> and <code>q >= 2^64</code>, the result is an empty string. * <code><nowiki>{{formatnum:{{#expr:-123 mod .9}}}}</nowiki></code> → {{formatnum:{{#expr:-123 mod .9}}}} * <code><nowiki>{{formatnum:{{#expr:-123 mod -.9}}}}</nowiki></code> → {{formatnum:{{#expr:-123 mod -.9}}}} * <code><nowiki>{{formatnum:{{#expr:-123 mod (2^64)}}}}</nowiki></code> → {{formatnum:{{#expr:-123 mod (2^64)}}}} * <code><nowiki>{{formatnum:{{#expr:-123 mod 1e20}}}}</nowiki></code> → {{formatnum:{{#expr:-123 mod 1e20}}}} * Compare: ** <code><nowiki>{{formatnum:{{#expr:-123 mod (2^64-2048)}}}}</nowiki></code> → {{formatnum:{{#expr:-123 mod (2^64-2048)}}}} ==== Floor and ceil ==== When using '''floor''' or '''ceil''' with an integer, it's first converted to a floating-point number before the function is applied. *{{xpsoc|numf|floor (trunc1e17+trunc1)}} *{{xpsoc|numf|ceil (trunc1e17+trunc1)}} Use [[Template:Floor]] and [[Template:Ceil]] to get in such a case the integer-type expression for the exact result: *<code><nowiki>{{numf|{{floortrunc1e17+trunc1}}}}</nowiki></code> → {{numf|{{floor|trunc1e17+trunc1}}}} *<code><nowiki>{{numf|{{ceil|trunc1e17+trunc1}}}}</nowiki></code> → {{numf|{{ceil|trunc1e17+trunc1}}}} 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 ==== 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. {{Note|type=reminder|text=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 === Additions before round: * {{Xpd|#expr:1.234 + 1.234 round 1 + 1}} Modulo and multiplication operations have equal precedence and they are evaluated from left-to-right: * {{Xpd|#expr:3 * 4 mod 10 * 10}} When using spaces in expressions with precedence, the layout can sometimes be confusing: * {{Xpd|#expr:23+45 mod 10}} Instead, you can write: * {{Xpd|#expr:23 + 45 mod10}} Or you can parenthesis: * {{Xpd|#expr:23 + (45 mod 10)}} == Positional and named parameters== Positional parameters can be used in calculations inside a template. For example, in [[w:Template:To USD/data/2018]], the <nowiki>{{{1}}}</nowiki> 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: <syntaxhighlight lang="wikitext" inline>{{#expr: ({{{1}}} / 6.62) round {{#ifeq: {{{round}}} | yes | 0 | 2 }} }}</syntaxhighlight> Here <nowiki>{{{round}}}</nowiki> 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== For comparing a number of type float with one of type integer, the integer is converted to float. Therefore the operators <code>=</code>, <code><=</code> and <code>>=</code> are not necessarily [[w:Transitive relation|transitive]] with mixed types: *{{xpsoc|1=#expr:trunc1e16=1e16}} *{{xpsoc|1=#expr:1e16=trunc1e16+trunc1}} *{{xpsoc|1=#expr:trunc1e16=trunc1e16+trunc1}} *{{xpsoc|1=#expr:trunc1e16>=1e16}} *{{xpsoc|1=#expr:1e16>=trunc1e16+trunc1}} *{{xpsoc|1=#expr:trunc1e16>=trunc1e16+trunc1}} Similarly, if <code>a >= b</code> and <code>b = c</code>, that does not necessarily imply <code>a >= c</code>: *{{xpsoc|1=#expr:trunc1e16>=1e16}} *{{xpsoc|1=#expr:1e16=trunc1e16+trunc1}} *{{xpsoc|1=#expr:trunc1e16>=trunc1e16+trunc1}} However, <code><</code> and <code>></code> ''are'' properly transitive. ==Monotonicity== 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 [[w:Monotonic function|monotonic function]] of the dividend:<!-- DO THESE EXAMPLES ACTUALLY ILLUSTRATE THE ISSUE JUST REFERRED TO? THEY ALL GIVE THE SAME RESULT --> *{{xpsoc|numf|(trunc1e18-trunc2)/trunc3}} *{{xpsoc|numf|(trunc1e18-trunc1)/trunc3}} *{{xpsoc|numf|trunc1e18/trunc3}} ==Numbers as input== Leading zeros are allowed, as well as a trailing decimal point (for an integer) and trailing zeros in a number with a decimal point. *{{xpd0|#expr: +01.20}} *{{xpd0|#expr: 12.}} These equivalences apply also for <code>#ifeq</code> and <code>#switch</code>, see below. The part of the expression representing a number is a sequence of digits and points; due to [https://php.net/function.floatval 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: *{{xpd0|#expr:123}} *{{xpd0|#expr:123.456}} *{{xpd0|#expr:.456}} *{{xpd0|#expr:0}} Also accepted: *{{xpd0|#expr: 123.}} *{{xpd0|#expr:000123.4560 }} *{{xpd0|#expr:.}} With ignored part: *{{xpd0|#expr:123.456.789}} But wrong: *{{xpd0|#expr: 123,456}} *{{xpd0|#expr: 123 456}} *{{xpd0|#expr:١٢٣}} Combinations with the operator <code>e</code>: Float: *{{xpd0|#expr:2.3e-5}} *{{xpsoc|#expr:2e18}} Integer type: *{{xpsoc|#expr:(trunc123456789012345)e trunc4}} Compare: *{{xpsoc|#expr:123456789012345e4}} *{{xpsoc|#expr:trunc123456789012345e4}} *{{xpsoc|#expr:(trunc123456789012345)e4}} *{{xpd0|#expr: e5}} *{{xpd0|#expr: e}} *{{xpd0|#expr: E}} 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): *{{xpsoc|formatnum:1,2,,34.567,8|R}} Input of a number of type integer is not possible. A float can be converted to type integer with the function <code>trunc</code>. 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: * <code><nowiki>{{numf|{{trunc|12345678|90123456789}}}}</nowiki></code> → {{numf|{{trunc|12345678|90123456789}}}} The smallest positive float can be written: *{{xpsoc|#expr:.5e-323}} but we cannot use that output as input: *{{xpsoc|#expr:{{#expr:.5e-323}}}} All digits are used to determine the float to which a number is rounded, as demonstrated in a borderline case: <div style="word-break:break-all"> * {{xpsoc|hex|0.00000000000000011102230246251566636831481088739149080825}} * {{xpsoc|hex|0.00000000000000011102230246251566636831481088739149080826}} *{{xpsoc|numf|2^-53}} *{{xpsoc|numf|2^-53+2^-105}} </div> Similarly: <div style="word-break:break-all"> *{{xpsoc|numfh|4398046511104.00048828125}} *{{xpsoc|numfh|4398046511104.00048828125000000000000000000000000000000000000000000000001}} *2^42+2^-10 = 4,398,046,511,104.000,976,562,5 </div> 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 == 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: * {{xpsoc|#expr:trunc(2^52)}} * {{xpsoc|#expr:-trunc(2^52)}} * {{xpsoc|#expr:trunc1100000}} * {{xpsoc|#expr:trunc1200000}} * {{xpsoc|#expr:trunc1300000}} * {{xpsoc|#expr:trunc4100000}} 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 [[w:Zend Engine|Zend Engine]] which has been fixed [http://bugs.php.net/43053], but on Wikimedia apparently not yet: * {{xpsoc|#expr:2^52}} * {{xpsoc|#expr:-(2^52)}} * {{xpsoc|#expr:1100000}} * {{xpsoc|#expr:1200000}} * {{xpsoc|#expr:1300000}} * {{xpsoc|#expr:4100000}} * {{xpsoc|#expr:1/7}} <blockquote style="margin: 0.5em 2em; border: 1px solid #AAA; padding: 2px 0.5em; font-style: italic."> 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. </blockquote> 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: * {{xpsoc|numfh|4.1e6}} Thus we may want to either compare two results of <code>#expr</code> (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 <code>#expr</code> with the exact number may be confusing.<!-- HUH? --> The function <code>formatnum</code> adds commas (on the left of the decimal point only), but does not convert from or to scientific format: * {{xpsoc|formatnum:1234567.890123}} * {{xpsoc|formatnum:1234567.890123E16}} 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{{e|23}}. [[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: * {{xpsoc|numf|trunc3^trunc39}} * {{xpsoc|numf|trunc3^trunc40}} * {{xpsoc|numf|1/7}} ==Negative zero== Although the literal "<code>-0</code>" (the unary minus applied to 0) gives 0,<!-- no it doesn't — see the first example after "However" below --> some operations give the float value "[[w:Signed zero|−0]]" (preserving commutativity of <code>+</code> and <code>*</code>): Generating −0 with <code>*</code>, <code>/</code>, <code>ceil</code>, <code>round</code>: *{{xpsoc|#expr: -1*0}} *{{xpsoc|#expr: (-1e-200)*1e-200}} *{{xpsoc|#expr: -1/1e333}} *{{xpsoc|#expr: 0/-1}} *{{xpsoc|#expr: (-1e-200)/1e200}} *{{xpsoc|#expr: (1e-200)/-1e200}} *{{xpsoc|#expr: ceil(-.1)}} *{{xpsoc|#expr: -.2round0}} Passing −0 on with unary <code>+</code>, binary <code>+</code> and <code>-</code>, <code>*</code> (and hence operator <code>e</code>), <code>/</code>, <code>floor</code>, <code>ceil</code>: *{{xpsoc|#expr: +(-1*0)}} *{{xpsoc|#expr: (-1*0)+(-1*0)}} *{{xpsoc|#expr: (-1*0)-0}} *{{xpsoc|#expr: 1*(-1*0)}} **{{xpsoc|#expr: (-1*0)e0}} *{{xpsoc|#expr: (-1*0)/1}} *{{xpsoc|#expr: floor(-1*0)}} *{{xpsoc|#expr: ceil(-1*0)}} However: *{{xpsoc|#expr: -0}} *{{xpsoc|#expr: -10^-401}} Since 2011 the function <code>#ifexpr</code> takes both 0 and the float −0 as false: *{{xpsoc|#ifexpr:0|1|0}} *{{xpsoc|#ifexpr:-1*0|1|0}} Also, as argument of a logical operator −0 is taken as false: *{{xpsoc|#expr:not(-1*0)}} *{{xpsoc|#expr:(-1*0)and1}} *{{xpsoc|#expr:(-1*0)or0}} 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 <code>trunc0</code>. *{{xpsoc|#expr:0+(-1*0)}} ==Type conversion== A float can be converted to type integer by operator <code>trunc</code> (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== 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 <code>mod</code> gives strange errors for some fairly large values of the second argument: *{{xpsoc|#expr:123mod(2^64-1)}} The operator <code>round</code> 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 <code>floor</code> rounds integer-type numbers with an absolute value between 2^53 and 2^63 to float, and not necessarily downward. Similarly the operator <code>ceil</code> rounds these numbers not necessarily upward. ==Branching depending on an expression== The function <code>[[mw:Help:Extension:ParserFunctions##ifexpr|#ifexpr]]</code> produces one of two specified results, depending on the value of a boolean expression involving numbers and booleans (not strings). Examples: * <nowiki>{{#ifexpr: {{CURRENTDOW}} = 0 or {{CURRENTDOW}} = 6 | weekEND | weekDAY}}</nowiki> yields '''{{#ifexpr: {{CURRENTDOW}} = 0 or {{CURRENTDOW}} = 6|weekEND|weekDAY}}''' because today is {{CURRENTDAYNAME}} and so {{xpd0|CURRENTDOW|gives=is}}. * {{xpsoc|1=#expr:2^10=1024}} 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: *{{xpsoc|#expr:1024e20-1e23}} *{{xpsoc|1=#expr:1024e20-1e23=2.4e21}} Instead one may want to allow a relatively small difference that could be present due to rounding errors: *{{xpsoc|1=#expr:abs(1024e20-1e23-2.4e21)<1e8}} 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: <div class="word-break:break-all"> *{{xpsoc|#ifeq:12345678901234567|12345678901234568.0|1|0}} *{{xpsoc|#ifeq:12345678901234568.0|12345678901234568|1|0}} *{{xpsoc|#ifeq:12345678901234567|12345678901234568|1|0}} *{{xpsoc|#ifeq:12345678901234567|12345678901234567e0|1|0}} *{{xpsoc|#ifeq:12345678901234567e0|12345678901234568|1|0}} *{{xpsoc|#ifeq:12345678901234567|12345678901234568|1|0}} *{{xpsoc|num|trunc(2^62)-trunc1+trunc(2^62)}} *{{xpsoc|#ifeq:9223372036854775700|9223372036854775900|1|0}} *{{xpsoc|#ifeq:9223372036854775900|9223372036854775800|1|0}} *{{xpsoc|#ifeq:9223372036854775700|9223372036854775800|1|0}} </div> ==Comparisons== The functions <code>[[mw:Help:Extension:ParserFunctions##ifeq|#ifeq]]</code> and <code>[[mw:Help:Extension:ParserFunctions##switch|#switch]]</code> compare numbers and strings for equality using PHP operator [https://php.net/language.operators.comparison <code>==</code>], 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 <code>e</code> of scientific notation are taken as part of the number, instead of as operators. Without <code>e</code> 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. <div style="word-break:break-all"> * {{xpsoc|#ifeq:3|3.0|1|0}} * {{xpsoc|#ifeq:3|03|1|0}} * {{xpsoc|#ifeq:0.00003456|3.456E-05|1|0}} * {{xpsoc|#ifeq:1e23|.1e24|1|0}} although rounding both numbers to float gives different internal numbers: ** {{xpsoc|#expr:1e23-.1e24}} ** {{xpsoc|1=#expr:1e23=.1e24}} ** {{xpsoc|1=#ifexpr:1e23=.1e24|2=1|3=0}} * {{xpsoc|#ifeq:9034567890123456789|9034567890123456788|1|0}} (two numbers of type integer, therefore only true if exactly equal); compare: * {{xpsoc|#ifeq:9034567890123456700.0|9034567890123456800|1|0}} (due to the decimal point in one number, both are rounded to float before the comparison, so the comparison is cruder) </div> ==Error messages== The following examples show all known <code>[[mw:Help:Extension:ParserFunctions##expr|#expr]]</code> and <code>[[mw:Help:Extension:ParserFunctions##ifexpr|#ifexpr]]</code> error messages. The error strings are all within <code><strong class="error"></code> elements. * Division by zero **{{xpdoc|#expr:1/0}} * Missing operand **{{xpdoc|#expr:2+}} **{{xpdoc|#expr:2-}} **{{xpdoc|#expr:2*}} **{{xpdoc|#expr:2/}} * Unexpected number **{{xpdoc|#expr:1 2}} * Unexpected word **{{xpdoc|#expr:1 a}} * Unexpected operator **{{xpdoc|#ifexpr:1/*2}} **{{xpdoc|#ifexpr:1*/2}} **{{xpdoc|#expr:>1}} **{{xpdoc|#expr: 1 (2)}} * Unexpected bracket **{{xpdoc|#expr: (1}} **{{xpdoc|#expr: 1)}} * Unrecognized punctuation **{{xpdoc|#expr:{{{a}}}}} **{{xpdoc|#expr:2*123,456}} **{{xpdoc|#ifexpr:3%2}} * Unrecognized word **{{xpdoc|#ifexpr:abc}} **{{xpdoc|#expr:abc.def}} * Result is not a number **{{xpdoc|#expr:sqrt-1}} * Invalid argument **{{xpdoc|#expr:ln0}} **{{xpdoc|#expr:asin-22}} * Stack exhausted ** <code><nowiki>"{{#expr: {{x|34|(1+(}} 1 {{x|34|))}} }}"</nowiki></code> → "{{#ifeq:{{ns:4}}|Meta|{{#expr:{{x|34|(1+(}}1{{x|34|))}}}}|{{#expr:(1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+(1))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))}}}}" Note that this last example uses [[Template:x]], which generates multiple copies of a given string. However: * <code><nowiki>"{{#expr: {{x|33|(1+(}} 1 {{x|33|))}} }}"</nowiki></code> → "{{#ifeq:{{ns:4}}|Meta|{{#expr:{{x|33|(1+(}}1{{x|33|))}}}}|{{#expr:(1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+((1+(1))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))}}}}" '''Large numbers and infinity''' * <code><nowiki>{{#expr: {{x|102|1000*}} 18 }}</nowiki></code> → {{#ifeq:{{ns:4}}|Meta|{{#expr:{{x|102|1000*}}18}}|{{#expr:1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*18}}}} * <code><nowiki>{{#expr: {{x|102|1000*}} 179 }}</nowiki></code> → {{#ifeq:{{ns:4}}|Meta|{{#expr:{{x|102|1000*}}179}}|{{#expr:1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*179}}}} * <code><nowiki>{{#expr: {{x|102|1000*}} 180 }}</nowiki></code> → {{#ifeq:{{ns:4}}|Meta|{{#expr:{{x|102|1000*}}180}}|{{#expr:1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*1000*180}}}} 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: *{{xpsoc|#expr:1e309/1e308}} *{{xpsoc|#expr:1e200*1e200*1e-300}} but: *{{xpsoc|#expr:1e200*(1e200*1e-300)}} Notice that an infinite result is not considered an error: *<code><nowiki>{{#iferror: {{#expr:1e200*1e200*1e-300}} | error}}</nowiki></code> → {{#iferror: {{#expr:1e200*1e200*1e-300}} | error}} '''Miscellaneous malformed expressions or markup''' These are not errors, per se, but are probably not what was intended. * <code><nowiki>"#expr:3.4.5.6"</nowiki></code> → "{{#expr:3.4.5.6}}" * <code><nowiki>"{{{#expr:2*3}}}"</nowiki></code> → "{{{#expr:2*3}}}" (triple braces are interpreted as a reference to an undefined parameter named "#expr:2*3") * <code><nowiki>"{{#expr:2*3}}}"</nowiki></code> → "{{#expr:2*3}}}" (extra closing braces are interpreted as plain text) * <code><nowiki>"{{{#expr:2*3}}"</nowiki></code> → "<nowiki>{</nowiki>{{#expr:2*3}}"<!-- nowiki prevents runaway syntax higlighting --> (one extra opening brace is interpreted as plain text) * <code><nowiki>"{{#expr:2*3}"</nowiki></code> → "<nowiki>{</nowiki>{#expr:2*3}"<!-- nowiki prevents runaway syntax higlighting --> (too few opening or closing braces results in everything being interpreted as plain text) ==Checking for a number== Check whether a string is a valid numeric expression: *<code><nowiki>{{#if:{{#ifexpr:3}}|0|1}}</nowiki></code> gives {{#if:{{#ifexpr:3}}|0|1}} *<code><nowiki>{{#if:{{#ifexpr:3-2}}|0|1}}</nowiki></code> gives {{#if:{{#ifexpr:3-2}}|0|1}} *<code><nowiki>{{#if:{{#ifexpr:3 2}}|0|1}}</nowiki></code> gives {{#if:{{#ifexpr:3 2}}|0|1}} Find the value represented by a string if it is a valid numeric expression, otherwise just return the string: *<code><nowiki>{{#iferror:{{#expr:3}}|3}}</nowiki></code> gives {{#iferror:{{#expr:3}}|3}} *<code><nowiki>{{#iferror:{{#expr:3-2}}|3-2}}</nowiki></code> gives {{#iferror:{{#expr:3-2}}|3-2}} *<code><nowiki>{{#iferror:{{#expr:3 2}}|3 2}}</nowiki></code> gives {{#iferror:{{#expr:3 2}}|3 2}} Check whether a string is a number: *<code><nowiki>{{#ifeq:3|{{#expr:3}}|1|0}}</nowiki></code> gives {{#ifeq:3|{{#expr:3}}|1|0}} *<code><nowiki>{{#ifeq:-3|{{#expr:-3}}|1|0}}</nowiki></code> gives {{#ifeq:-3|{{#expr:-3}}|1|0}} *<code><nowiki>{{#ifeq:3.5|{{#expr:3.5}}|1|0}}</nowiki></code> gives {{#ifeq:3.5|{{#expr:3.5}}|1|0}} *<code><nowiki>{{#ifeq:03|{{#expr:03}}|1|0}}</nowiki></code> gives {{#ifeq:03|{{#expr:03}}|1|0}} *<code><nowiki>{{#ifeq:3-2|{{#expr:3-2}}|1|0}}</nowiki></code> gives {{#ifeq:3-2|{{#expr:3-2}}|1|0}} *<code><nowiki>{{#ifeq:3 2|{{#expr:3 2}}|1|0}}</nowiki></code> gives {{#ifeq:3 2|{{#expr:3 2}}|1|0}} ==Minus sign== Only the [[w:hyphen-minus|hyphen-minus]] character or [[w:Minus sign#Character codes|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, [[w:Dash#En dash|en dash]], [[w:Dash#Em dash|em dash]] and others), though often similar visually, are '''not''' valid minus signs, but punctuation signs or typographical variants. {|class="wikitable" |- | [[w:hyphen-minus|hyphen-minus]], typed directly as the character '-' (U+002D) | {{xpd0|#expr:-12|gives={{!!}}}} |- | hyphen-minus, typed as the numerical character reference <code>&#x2D;</code> | <code>"{{#expr:&#x2D;12}}"</code> | "{{#expr:-12}}" | [{{fullurl:Special:ExpandTemplates|input=%7B%7B%23expr:%26%23x2D;12%7D%7D}}] |- | hyphen-minus, typed as the numerical character reference <code>&#45;</code> | <code>"{{#expr:&#45;12}}"</code> | "{{#expr:-12}}" | [{{fullurl:Special:ExpandTemplates|input=%7B%7B%23expr:%26%2345;12%7D%7D}}] |- | [[w:Minus sign#Character codes|minus sign]], typed directly as the character '−' (U+2212) | {{xpd0|#expr:−12|gives={{!!}}}} |- | minus sign, typed as the numerical character reference <code>&#x2212;</code> | <code>"{{#expr:&#x2212;12}}"</code> | "{{#expr:−12}}" | [{{fullurl:Special:ExpandTemplates|input=%7B%7B%23expr:%26%23x2212;12%7D%7D}}] |- | minus sign, typed as the numerical character reference <code>&#8722;</code> | <code>"{{#expr:&#8722;12}}"</code> | "{{#expr:−12}}" | [{{fullurl:Special:ExpandTemplates|input=%7B%7B%23expr:%26%238722;12%7D%7D}}] |- | minus sign, typed as the symbolic character reference <code>&minus;</code> | <code>"{{#expr:&minus;12}}"</code> | "{{#expr:−12}}" | [{{fullurl:Special:ExpandTemplates|input=%7B%7B%23expr:%26minus;12%7D%7D}}] |- | figure dash, typed directly as the character '‒' (U+2012) | {{xpd0|#expr:‒12|gives={{!!}}}} |- | figure dash, typed as the numerical character reference <code>&#x2012;</code> | <code>"{{#expr:&#x2012;12}}"</code> | "{{#expr:‒12}}" | [{{fullurl:Special:ExpandTemplates|input=%7B%7B%23expr:%26%23x2012;12%7D%7D}}] |- | figure dash, typed as the numerical character reference <code>&#8210;</code> | <code>"{{#expr:&#8210;12}}"</code> | "{{#expr:‒12}}" | [{{fullurl:Special:ExpandTemplates|input=%7B%7B%23expr:%26%238210;12%7D%7D}}] |- | [[w:Dash#En dash|en dash]], typed directly as the character '–' (U+2013) | {{xpd0|#expr:–12|gives={{!!}}}} |- | en dash, typed as the numerical character reference <code>&#x2013;</code> | <code>"{{#expr:&#x2013;12}}"</code> | "{{#expr:–12}}" | [{{fullurl:Special:ExpandTemplates|input=%7B%7B%23expr:%26%23x2013;12%7D%7D}}] |- | en dash, typed as the numerical character reference <code>&#8211;</code> | <code>"{{#expr:&#8211;12}}"</code> | "{{#expr:–12}}" | [{{fullurl:Special:ExpandTemplates|input=%7B%7B%23expr:%26%238211;12%7D%7D}}] |- | en dash, typed as the symbolic character reference <code>&ndash;</code> | <code>"{{#expr:&ndash;12}}"</code> | "{{#expr:–12}}" | [{{fullurl:Special:ExpandTemplates|input=%7B%7B%23expr:%26ndash;12%7D%7D}}] |} 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== 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{{e|23}} Examples: *{{xpd0|#expr:three}} *{{xpd0|#expr:2<3}} *{{xpd0|#expr:2≤3}} *{{xpd0|#expr:2<{{=}}3}} *{{xpd0|#expr:2>3}} *{{xpd0|#expr:2≥3}} *{{xpd0|#expr:2>{{=}}3}} *{{xpd0|#expr:2*3}} *{{xpd0|#expr:2×3}} *{{xpd0|#expr:2,300}} *{{xpd0|#expr:6E23}} 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 [[mw:Help:Magic words|Magic words]] and return group separated results you can use formatnum:<br> <code><nowiki>{{formatnum: {{#expr: {{NUMBEROFPAGES:R}} - {{NUMBEROFFILES:R}} }} }}</nowiki></code> = {{formatnum: {{#expr: {{NUMBEROFPAGES:R}} - {{NUMBEROFFILES:R}} }} }} (instead of {{#expr: {{NUMBEROFPAGES:R}} - {{NUMBEROFFILES:R}} }}). == See also == *[[Manual:Representation of numbers in expr parser function]] *[[Manual:Converting between ParserFunctions syntax and TeX syntax]] *[[:Category:Mathematical templates]] and [[w:Help:Template#Handling_parameters|positional variables]] * https://github.com/wikimedia/mediawiki-extensions-ParserFunctions/blob/master/includes/ExprParser.php *[[w:Positional parameter]]s, as <nowiki>{{{1}}}</nowiki>. *[[Project:Sandbox]] to try the calculations. ==References== <references/> [[Category:Parser manuals{{#translation:}}]]
Search
Search
Export translations
Add topic