Chiyansama(留言 | 贡献) (// Edit via Wikiplus) |
Chiyansama(留言 | 贡献) |
||
(未显示同一用户的9个中间版本) | |||
第1行: | 第1行: | ||
{{操作指南|shortcut1=H:CALC}} |
{{操作指南|shortcut1=H:CALC}} |
||
|subject = 关於仅需要使用简单的'''[[实数]]运算'''的场合 |
|||
|other = 需要使用到[[复数 (数学)|复数]]、[[复分析|复变数]]、[[四元数]]或特殊[[代数]]运算的场合 |
|||
|Template:复变运算 |
|||
}} |
|||
借助于Mediawiki的扩展“函数解析器”,进行计算变得可行。<tt>expr解析器</tt>函数可以用于计算代数表达式。语法为:{{Squote| |
借助于Mediawiki的扩展“函数解析器”,进行计算变得可行。<tt>expr解析器</tt>函数可以用于计算代数表达式。语法为:{{Squote| |
||
:{{((}}#expr: ''表达式'' {{))}} |
:{{((}}#expr: ''表达式'' {{))}} |
||
第59行: | 第55行: | ||
== 样例 == |
== 样例 == |
||
*<code><nowiki>{{#expr: (100 - 32) / 9 * 5 round 0 }}</nowiki></code> = {{#expr: (100 - 32) / 9 * 5 round 0 }}: |
*<code><nowiki>{{#expr: (100 - 32) / 9 * 5 round 0 }}</nowiki></code> = {{#expr: (100 - 32) / 9 * 5 round 0 }}:摄氏、华氏温度的度单位转换]] |
||
*<code><nowiki>{{#expr: 3 < 2 < 1}}</nowiki></code> = {{#expr: 3 < 2 < 1}} |
*<code><nowiki>{{#expr: 3 < 2 < 1}}</nowiki></code> = {{#expr: 3 < 2 < 1}} |
||
*<code><nowiki>{{#expr: 2 = 2 = 1}}</nowiki></code> = {{#expr: 2 = 2 = 1}}:与数学表达式不同,“=”、“<”、“>”等运算符有返回值 |
*<code><nowiki>{{#expr: 2 = 2 = 1}}</nowiki></code> = {{#expr: 2 = 2 = 1}}:与数学表达式不同,“=”、“<”、“>”等运算符有返回值 |
||
第66行: | 第62行: | ||
*<code><nowiki>{{#expr: 999 * 999 round -2.9}}</nowiki></code> = {{#expr: 999 * 999 round -2.9}}:round的危险用法 |
*<code><nowiki>{{#expr: 999 * 999 round -2.9}}</nowiki></code> = {{#expr: 999 * 999 round -2.9}}:round的危险用法 |
||
*<code><nowiki>{{#expr: 1 or 1 and 0}}</nowiki></code> = {{#expr: 1 or 1 and 0}}:优先级演示 |
*<code><nowiki>{{#expr: 1 or 1 and 0}}</nowiki></code> = {{#expr: 1 or 1 and 0}}:优先级演示 |
||
*<code><nowiki>{{#expr: 10.9 mod 2}}</nowiki></code> = {{#expr: 10.9 mod 2}} |
*<code><nowiki>{{#expr: 10.9 mod 2}}</nowiki></code> = {{#expr: 10.9 mod 2}}:mod的危险用法 |
||
*<code><nowiki>{{#expr: 30------7}}</nowiki></code> = {{#expr: 30-----7}} |
*<code><nowiki>{{#expr: 30------7}}</nowiki></code> = {{#expr: 30-----7}} |
||
*<code><nowiki>{{#expr: 30+++++7}}</nowiki></code> = {{#expr: 30+++++7}}:“+”、“-”运算符有两种含义 |
*<code><nowiki>{{#expr: 30+++++7}}</nowiki></code> = {{#expr: 30+++++7}}:“+”、“-”运算符有两种含义 |
||
*<code><nowiki>{{#expr: .}}</nowiki></code> = {{#expr: .}} |
*<code><nowiki>{{#expr: .}}</nowiki></code> = {{#expr: .}} |
||
*<code><nowiki>{{#expr: .1.1}}</nowiki></code> = {{#expr: .1.1}}: |
*<code><nowiki>{{#expr: .1.1}}</nowiki></code> = {{#expr: .1.1}}:实数表达法的多样性(多出的小数点会被无视) |
||
*<code><nowiki>{{#expr: ( ( ) )}}</nowiki></code> = {{#expr: ( ( ) )}}:特殊的 |
*<code><nowiki>{{#expr: ( ( ) )}}</nowiki></code> = {{#expr: ( ( ) )}}:特殊的括号用法,没有实际意义 |
||
*<code><nowiki>{{#expr: 999999999*...*999999999}}</nowiki></code> = {{#expr: 999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999}}: |
*<code><nowiki>{{#expr: 999999999*...*999999999}}</nowiki></code> = {{#expr: 999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999*999999999}}:数字上溢的结果 |
||
*<code><nowiki>{{#expr:(-1)^0.5}}</nowiki></code> = {{#expr:(-1)^0.5}}:无法对 |
*<code><nowiki>{{#expr:(-1)^0.5}}</nowiki></code> = {{#expr:(-1)^0.5}}:无法对虚数单位进行计算 |
||
*<code><nowiki>{{Root|(-1)}}</nowiki></code> = {{Root|(-1)}}:使用[[Template:Root|根号模板]](详见[[Template:复变运算]]) |
|||
*<code><nowiki>{{复变运算|(-1)^0.5}}</nowiki></code> = {{复变运算|(-1)^0.5}}:使用[[Template:复变运算|复变运算模板]] |
|||
*<code><nowiki>{{复变运算|e^(((-1)^0.5) * pi)}}</nowiki></code> = {{复变运算|e^(((-1)^0.5) * pi)}}:[[欧拉恒等式]] |
|||
*<code><nowiki>{{#expr:e^(((-1)^0.5) * pi)}}</nowiki></code> = {{#expr:e^(((-1)^0.5) * pi)}}:对比使用普通运算功能的[[欧拉恒等式]] |
|||
== 详细运算子、函数、常数说明 == |
== 详细运算子、函数、常数说明 == |
||
第90行: | 第82行: | ||
|- |
|- |
||
| {{anchor|一元减|负号}}</code>'''-'''</code> || 1 |
| {{anchor|一元减|负号}}</code>'''-'''</code> || 1 |
||
| |
| |一元 '''<tt>-</tt>''' 符号 (负号) |
||
| [http://php.net/manual/en/language.operators.arithmetic.php -] || 与参数相同 || {{nts|10}} || |
| [http://php.net/manual/en/language.operators.arithmetic.php -] || 与参数相同 || {{nts|10}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第98行: | 第90行: | ||
|} |
|} |
||
|- |
|- |
||
| {{anchor|科学记号}}'''<code>e</code>'''<br> (位於子表达式之间)|| 2 || *10^<br>以10為底的指数<br> |
| {{anchor|科学记号}}'''<code>e</code>'''<br> (位於子表达式之间)|| 2 || *10^<br>以10為底的指数<br>科学记号|| [http://php.net/manual/en/language.operators.arithmetic.php *] [http://php.net/manual/en/function.pow.php pow] (10,..) || 浮点数,除非左边的参数是整数且指数是非负整数 || {{nts|10}} |
||
| |
| |
||
第119行: | 第111行: | ||
|- |
|- |
||
| '''<code>exp</code>''' || 1 |
| '''<code>exp</code>''' || 1 |
||
| [ |
| [自然指数函数 e<sup>x</sup> |
||
| [http://php.net/manual/en/function.exp.php exp] || 浮点数 || {{nts|9}} || |
| [http://php.net/manual/en/function.exp.php exp] || 浮点数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第138行: | 第130行: | ||
|- |
|- |
||
| '''<code>ln</code>''' || 1 |
| '''<code>ln</code>''' || 1 |
||
| |
| 自然对数 |
||
| [http://php.net/manual/en/function.log.php <sup>log</sup>] || 浮点数 || {{nts|9}} || |
| [http://php.net/manual/en/function.log.php <sup>log</sup>] || 浮点数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第153行: | 第145行: | ||
|- |
|- |
||
| '''<code>abs</code>''' || 1 |
| '''<code>abs</code>''' || 1 |
||
| |
|绝对值 |
||
| [http://php.net/manual/en/function.abs.php abs] || 同於输入值,并且非负 || {{nts|9}} || |
| [http://php.net/manual/en/function.abs.php abs] || 同於输入值,并且非负 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第162行: | 第154行: | ||
|- |
|- |
||
| '''<code>sqrt</code>''' || 1 |
| '''<code>sqrt</code>''' || 1 |
||
| |
|平方根 |
||
| [http://php.net/manual/en/function.sqrt.php sqrt] || 浮点数 || {{nts|9}} || |
| [http://php.net/manual/en/function.sqrt.php sqrt] || 浮点数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第169行: | 第161行: | ||
{{xpettu|sqrt| 1e19}} |
{{xpettu|sqrt| 1e19}} |
||
|} |
|} |
||
此平方根计算无法处理结果為 |
此平方根计算无法处理结果為虚数或复数的情况<br>即对负值进行平方根会发生错误: |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettu|sqrt|-1}} |
{{xpettu|sqrt|-1}} |
||
第175行: | 第167行: | ||
|- |
|- |
||
| '''<code>trunc</code>''' || 1 |
| '''<code>trunc</code>''' || 1 |
||
| |
| 截尾函数 |
||
| [http://php.net/manual/en/language.types.integer.php#language.types.integer.casting (int)], 即 |
| [http://php.net/manual/en/language.types.integer.php#language.types.integer.casting (int)], 即转换型态]]為整数 || 整数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettu|trunc|1.2}} |
{{xpettu|trunc|1.2}} |
||
第189行: | 第181行: | ||
|- |
|- |
||
| '''<code>floor</code>''' || 1 |
| '''<code>floor</code>''' || 1 |
||
|向下取整函数 |
|||
| [http://php.net/manual/en/function.floor.php floor] || 浮点数 || {{nts|9}} || |
| [http://php.net/manual/en/function.floor.php floor] || 浮点数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第198行: | 第190行: | ||
|- |
|- |
||
| '''<code>ceil</code>''' || 1 |
| '''<code>ceil</code>''' || 1 |
||
|向上取整函数 |
|||
| [http://php.net/manual/en/function.ceil.php ceil] || 浮点数 || {{nts|9}} || |
| [http://php.net/manual/en/function.ceil.php ceil] || 浮点数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第207行: | 第199行: | ||
|- |
|- |
||
| '''<code>sin</code>''' || 1 |
| '''<code>sin</code>''' || 1 |
||
| |
|正弦函数 |
||
| [http://php.net/manual/en/function.sin.php sin] || 浮点数 || {{nts|9}} || |
| [http://php.net/manual/en/function.sin.php sin] || 浮点数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第220行: | 第212行: | ||
|- |
|- |
||
| '''<code>cos</code>''' || 1 |
| '''<code>cos</code>''' || 1 |
||
| |
|餘弦函数 |
||
| [http://php.net/manual/en/function.cos.php cos] || 浮点数 || {{nts|9}} || |
| [http://php.net/manual/en/function.cos.php cos] || 浮点数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第228行: | 第220行: | ||
|- |
|- |
||
| '''<code>tan</code>''' || 1 |
| '''<code>tan</code>''' || 1 |
||
| |
|正切函数 |
||
| [http://php.net/manual/en/function.tan.php tan] || 浮点数 || {{nts|9}} || |
| [http://php.net/manual/en/function.tan.php tan] || 浮点数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第236行: | 第228行: | ||
|- |
|- |
||
| '''<code>asin</code>''' || 1 |
| '''<code>asin</code>''' || 1 |
||
| |
|反正弦 |
||
| [http://php.net/manual/en/function.asin.php asin] || 浮点数 || {{nts|9}} || |
| [http://php.net/manual/en/function.asin.php asin] || 浮点数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第244行: | 第236行: | ||
|- |
|- |
||
| '''<code>acos</code>''' || 1 |
| '''<code>acos</code>''' || 1 |
||
| |
|反餘弦 |
||
| [http://php.net/manual/en/function.acos.php acos] || 浮点数 || {{nts|9}} || |
| [http://php.net/manual/en/function.acos.php acos] || 浮点数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第253行: | 第245行: | ||
|- |
|- |
||
| '''<code>atan</code>''' || 1 |
| '''<code>atan</code>''' || 1 |
||
| |
|反正切 |
||
| [http://php.net/manual/en/function.atan.php atan] || 浮点数 || {{nts|9}} || |
| [http://php.net/manual/en/function.atan.php atan] || 浮点数 || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第262行: | 第254行: | ||
|- |
|- |
||
| '''<code>not</code>''' || 1 |
| '''<code>not</code>''' || 1 |
||
| |
|逻辑非 |
||
| [http://www.php.net/manual/en/language.operators.logical.php !] || 以整数表达<br>的 |
| [http://www.php.net/manual/en/language.operators.logical.php !] || 以整数表达<br>的布林值<br> (1 或 0) || {{nts|9}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettu|not|0}} |
{{xpettu|not|0}} |
||
第272行: | 第264行: | ||
|- |
|- |
||
| '''<code>^</code>''' || 2 |
| '''<code>^</code>''' || 2 |
||
| |
| 幂 (指数) |
||
| [http://php.net/manual/en/function.pow.php pow] || 原则上為浮点数。若底数為整数且指数為非负整数则為整数 || {{nts|8}} || |
| [http://php.net/manual/en/function.pow.php pow] || 原则上為浮点数。若底数為整数且指数為非负整数则為整数 || {{nts|8}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第287行: | 第279行: | ||
|- |
|- |
||
| '''<code>*</code>''' || 2 |
| '''<code>*</code>''' || 2 |
||
| |
| 乘法 |
||
| [http://php.net/manual/en/language.operators.arithmetic.php *] || 若两参数為整数,则為整数,否则為浮点数 || {{nts|7}} || |
| [http://php.net/manual/en/language.operators.arithmetic.php *] || 若两参数為整数,则為整数,否则為浮点数 || {{nts|7}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第298行: | 第290行: | ||
|- |
|- |
||
| '''<code>/</code>''' (或写作'''div''') || 2 |
| '''<code>/</code>''' (或写作'''div''') || 2 |
||
|| |
|| 除法 <br>(div非整数除法<ref name="divmod">'''<code>div</code>'''及'''<code>mod</code>''' 在几乎所有编程语言中都不同。</ref>) |
||
| [http://php.net/manual/en/language.operators.arithmetic.php /] || 原则上為浮点数。若两参数皆為整数且 |
| [http://php.net/manual/en/language.operators.arithmetic.php /] || 原则上為浮点数。若两参数皆為整数且整除则為整数 || {{nts|7}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettb|6|/|3}} |
{{xpettb|6|/|3}} |
||
第309行: | 第301行: | ||
|- |
|- |
||
| '''<code>mod</code>''' || 2 |
| '''<code>mod</code>''' || 2 |
||
| |
| 模除。 实行带餘除法<br>时会将前后两数截<br>成整数。<ref name="divmod"/> |
||
| [http://php.net/manual/en/language.operators.arithmetic.php %] || 整数 || {{nts|7}} || |
| [http://php.net/manual/en/language.operators.arithmetic.php %] || 整数 || {{nts|7}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第319行: | 第311行: | ||
|} |
|} |
||
部分 |
部分除数无法取餘数 (见[[#Limitations and workarounds|限制章节]]): |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettb|123|mod|2^64}} |
{{xpettb|123|mod|2^64}} |
||
第325行: | 第317行: | ||
|- |
|- |
||
| '''<code>fmod</code>''' || 2 |
| '''<code>fmod</code>''' || 2 |
||
| |
| 模除,浮点数版本。<br>返回减去第二参数之整数<br>倍的第一参数。 |
||
| [https://www.php.net/manual/en/function.fmod.php fmod] || 浮点数|| {{nts|7}} || |
| [https://www.php.net/manual/en/function.fmod.php fmod] || 浮点数|| {{nts|7}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第337行: | 第329行: | ||
|- |
|- |
||
| '''<code>+</code>''' || 2 |
| '''<code>+</code>''' || 2 |
||
|| |
|| 加法 |
||
| [http://php.net/manual/en/language.operators.arithmetic.php +] || 若两参数皆為整数则為整数,其餘浮点数 || {{nts|6}} || |
| [http://php.net/manual/en/language.operators.arithmetic.php +] || 若两参数皆為整数则為整数,其餘浮点数 || {{nts|6}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第348行: | 第340行: | ||
|- |
|- |
||
| '''<code>-</code>''' || 2 |
| '''<code>-</code>''' || 2 |
||
|| |
|| 减法 |
||
| [http://php.net/manual/en/language.operators.arithmetic.php -] || 若两参数皆為整数则為整数,其餘浮点数 || {{nts|6}} || |
| [http://php.net/manual/en/language.operators.arithmetic.php -] || 若两参数皆為整数则為整数,其餘浮点数 || {{nts|6}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第359行: | 第351行: | ||
|- |
|- |
||
| '''<code>round</code>''' || 2 |
| '''<code>round</code>''' || 2 |
||
|| 将指定的小数位数<br>做 |
|| 将指定的小数位数<br>做四捨五入,若為<br>负值则会对指数<br>為数做四捨五入。 |
||
| [http://php.net/manual/en/function.round.php round] || 浮点数 || {{nts|5}} || |
| [http://php.net/manual/en/function.round.php round] || 浮点数 || {{nts|5}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第371行: | 第363行: | ||
|- |
|- |
||
| '''<code>=</code>''' || 2 |
| '''<code>=</code>''' || 2 |
||
|| |
|| 相等(数字或逻辑的<br>相等运算,不支援<br>字串比对) |
||
| [http://php.net/manual/en/language.operators.comparison.php ==] || 以整数表达<br>的 |
| [http://php.net/manual/en/language.operators.comparison.php ==] || 以整数表达<br>的布林值<br> (1 或 0) || {{nts|4}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettb|3.0|{{=}}|3}} |
{{xpettb|3.0|{{=}}|3}} |
||
第392行: | 第384行: | ||
|- |
|- |
||
| '''<code><</code>''' || 2 |
| '''<code><</code>''' || 2 |
||
|| |
|| 小於(不适用於字串<br>比较) |
||
| [http://php.net/manual/en/language.operators.comparison.php <] || 以整数表达<br>的 |
| [http://php.net/manual/en/language.operators.comparison.php <] || 以整数表达<br>的布林值<br> (1 或 0) || {{nts|4}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettb|3|<|3}} |
{{xpettb|3|<|3}} |
||
第410行: | 第402行: | ||
|- |
|- |
||
| '''<code>></code>''' || 2 |
| '''<code>></code>''' || 2 |
||
|| |
|| 大於(当参数颠倒时<br>与 < 相同) |
||
| [http://php.net/manual/en/language.operators.comparison.php >] || 以整数表达<br>的 |
| [http://php.net/manual/en/language.operators.comparison.php >] || 以整数表达<br>的布林值<br> (1 或 0) || {{nts|4}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettb|4|>|3}} |
{{xpettb|4|>|3}} |
||
第418行: | 第410行: | ||
|- |
|- |
||
| '''<code><=</code>''' || 2 |
| '''<code><=</code>''' || 2 |
||
|| |
|| 小於等於(当参数<br>颠倒时与 >= 相同) |
||
| [http://php.net/manual/en/language.operators.comparison.php <=] || 以整数表达<br>的 |
| [http://php.net/manual/en/language.operators.comparison.php <=] || 以整数表达<br>的布林值<br> (1 或 0) || {{nts|4}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettb|3|<{{=}}|4}} |
{{xpettb|3|<{{=}}|4}} |
||
第426行: | 第418行: | ||
|- |
|- |
||
| '''<code>>=</code>''' || 2 |
| '''<code>>=</code>''' || 2 |
||
| |
| 大於等於(<的否定) |
||
| [http://php.net/manual/en/language.operators.logical.php >=] || 以整数表达<br>的 |
| [http://php.net/manual/en/language.operators.logical.php >=] || 以整数表达<br>的布林值<br> (1 或 0) || {{nts|4}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettb|4|>{{=}}|3}} |
{{xpettb|4|>{{=}}|3}} |
||
第434行: | 第426行: | ||
|- |
|- |
||
| '''<code>and</code>''' || 2 |
| '''<code>and</code>''' || 2 |
||
| |
| 逻辑与 |
||
| [http://php.net/manual/en/language.operators.logical.php &&] || 以整数表达<br>的 |
| [http://php.net/manual/en/language.operators.logical.php &&] || 以整数表达<br>的布林值<br> (1 或 0) || {{nts|3}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettb|3|and|4}} |
{{xpettb|3|and|4}} |
||
第444行: | 第436行: | ||
|- |
|- |
||
| '''<code>or</code>''' || 2 |
| '''<code>or</code>''' || 2 |
||
| |
| 逻辑或 |
||
| [http://php.net/manual/en/language.operators.logical.php <nowiki>||</nowiki>] || 以整数表达<br>的 |
| [http://php.net/manual/en/language.operators.logical.php <nowiki>||</nowiki>] || 以整数表达<br>的布林值<br> (1 或 0) || {{nts|2}} || |
||
{| class="wikitable" |
{| class="wikitable" |
||
{{xpettb|3|or|4}} |
{{xpettb|3|or|4}} |
||
第454行: | 第446行: | ||
|- |
|- |
||
| {{anchor|一元加|正号}}'''<code>+</code>''' || 1 |
| {{anchor|一元加|正号}}'''<code>+</code>''' || 1 |
||
| |
| 一元 '''<tt>+</tt></code>''' 符号 (正号) |
||
| (不适用) || 与参数相同 || 不适用 || |
| (不适用) || 与参数相同 || 不适用 || |
||
{| class="wikitable" |
{| class="wikitable" |
||
第463行: | 第455行: | ||
|- |
|- |
||
| {{nowrap|(数字)}} || 0 |
| {{nowrap|(数字)}} || 0 |
||
| 普通十进制的无符号<br>数字( |
| 普通十进制的无符号<br>数字(一元加减和e]]<br>被视為运算符,请<br>参见本表的其他地方) || [http://nl.php.net/manual/en/function.floatval.php floatval] || 浮点数 || 不适用 |
||
| |
| |
||
{| class="wikitable" |
{| class="wikitable" |
||
第474行: | 第466行: | ||
|- |
|- |
||
| '''<code>e</code>'''作為<br>子表达式 || 0 |
| '''<code>e</code>'''作為<br>子表达式 || 0 |
||
| 数学常数 |
| 数学常数e || [http://php.net/manual/en/function.exp.php exp](1) || 浮点数 || 不适用 |
||
| |
| |
||
{| class="wikitable" |
{| class="wikitable" |
||
第482行: | 第474行: | ||
|- |
|- |
||
| '''<code>pi</code>''' || 0 |
| '''<code>pi</code>''' || 0 |
||
| 数学常数 |
| 数学常数圆周率 π]] || [http://php.net/manual/en/function.pi.php pi] || 浮点数 || 不适用 |
||
| |
| |
||
{| class="wikitable" |
{| class="wikitable" |
||
第519行: | 第511行: | ||
== 误差 == |
== 误差 == |
||
浮点误差有可能造成某些运算的不可预料的结果,特别是最后几位数字的不精确。因此在使用此函数进行实数运算时,注意不要进行比较运算。 |
|||
== 注意内部数字表示 == |
== 注意内部数字表示 == |
||
第527行: | 第519行: | ||
*<code><nowiki>{{#expr: -0.1 round 0}}</nowiki></code> = {{#expr: -0.1 round 0}} |
*<code><nowiki>{{#expr: -0.1 round 0}}</nowiki></code> = {{#expr: -0.1 round 0}} |
||
*<code><nowiki>{{#expr: 2017.5000 round 3}}</nowiki></code> = {{#expr: 2017.5000 round 3}}:使用时会有无法补零的情形。欲补零,可以使用{{tl|Round}}模板来解决 |
*<code><nowiki>{{#expr: 2017.5000 round 3}}</nowiki></code> = {{#expr: 2017.5000 round 3}}:使用时会有无法补零的情形。欲补零,可以使用{{tl|Round}}模板来解决 |
||
在返回值中,有可能出现无法'''再计算'''的数的情况,或者有一部分 |
在返回值中,有可能出现无法'''再计算'''的数的情况,或者有一部分有效数字没有显示出来。因此在使用此函数进行运算时,须注意这些问题。 |
||
== 参见 == |
|||
*[[Template:复变运算]]:MediaWiki原生的运算功能仅支援[[实数]]的运算。若需要[[复数 (数学)|复数]]或[[复分析|复变数]]甚至[[四元数]]的计算可使用[[Template:复变运算]]模板,如上述示例中对负值取平方根时。 |
|||
*[[Help:模板扩展语法]] |
|||
== 注释 == |
== 注释 == |
2023年7月20日 (四) 17:41的最新版本
本页是操作指南,介绍了有兽档案馆社区的实践经验及操作惯例。
本页不属于有兽档案馆的条例或规则;若本页内容与相关条例不一致的,以条例内容为准。
本页不属于有兽档案馆的条例或规则;若本页内容与相关条例不一致的,以条例内容为准。
借助于Mediawiki的扩展“函数解析器”,进行计算变得可行。expr解析器函数可以用于计算代数表达式。语法为:
|
表达式支持的运算符有:
运算符 | 名称 | 优先级 | 元数 | 结合性 | 样例 |
---|---|---|---|---|---|
+ | 正 | 9 | 1 | 右 | {{#expr: + 7}} = 7 |
- | 负 | 9 | 1 | 右 | {{#expr: - 7}} = -7 |
not | 逻辑非 | 9 | 1 | 右 | {{#expr: not 7}} = 0 |
* | 乘 | 8 | 2 | 左 | {{#expr: 30 * 7}} = 210 |
/ | 除 | 8 | 2 | 左 | {{#expr: 30 / 7}} = 4.2857142857143 |
div | 除 | 8 | 2 | 左 | {{#expr: 30 div 7}} = 4.2857142857143 |
mod | 模 | 8 | 2 | 左 | {{#expr: 30 mod 7}} = 2 |
+ | 加 | 6 | 2 | 左 | {{#expr: 30 + 7}} = 37 |
- | 减 | 6 | 2 | 左 | {{#expr: 30 - 7}} = 23 |
round | 舍入 | 5 | 2 | 左 | {{#expr: 30 / 7 round 7}} = 4.2857143 |
= | 等于 | 4 | 2 | 左 | {{#expr: 30 = 7}} = 0 |
< | 小于 | 4 | 2 | 左 | {{#expr: 30 < 7}} = 0 |
> | 大于 | 4 | 2 | 左 | {{#expr: 30 > 7}} = 1 |
<= | 小于等于 | 4 | 2 | 左 | {{#expr: 30 <= 7}} = 0 |
>= | 大于等于 | 4 | 2 | 左 | {{#expr: 30 >= 7}} = 1 |
<> | 不等于 | 4 | 2 | 左 | {{#expr: 30 <> 7}} = 1 |
!= | 不等于 | 4 | 2 | 左 | {{#expr: 30 != 7}} = 1 |
and | 逻辑与 | 3 | 2 | 左 | {{#expr: 30 and 7}} = 1 |
or | 逻辑或 | 2 | 2 | 左 | {{#expr: 30 or 7}} = 1 |
round运算对运算数正负,位数正负都有不同的表现,参见下例。
逻辑运算符把假映射为0,把真映射为非0,且返回值只有0或1。
同一表达式中先计算高优先级运算。括号优先级高于一切。
样例
{{#expr: (100 - 32) / 9 * 5 round 0 }}
= 38:摄氏、华氏温度的度单位转换]]{{#expr: 3 < 2 < 1}}
= 1{{#expr: 2 = 2 = 1}}
= 1:与数学表达式不同,“=”、“<”、“>”等运算符有返回值{{#expr: 999 * 999 round -2}}
= 998000{{#expr: 999 * 999 round -20}}
= 0{{#expr: 999 * 999 round -2.9}}
= 998000:round的危险用法{{#expr: 1 or 1 and 0}}
= 1:优先级演示{{#expr: 10.9 mod 2}}
= 0:mod的危险用法{{#expr: 30------7}}
= 23{{#expr: 30+++++7}}
= 37:“+”、“-”运算符有两种含义{{#expr: .}}
= 0{{#expr: .1.1}}
= 0.1:实数表达法的多样性(多出的小数点会被无视){{#expr: ( ( ) )}}
= :特殊的括号用法,没有实际意义{{#expr: 999999999*...*999999999}}
= INF:数字上溢的结果{{#expr:(-1)^0.5}}
= NAN:无法对虚数单位进行计算
详细运算子、函数、常数说明
表达式 | 参数 | 运算子 | PHP | 资料型态 | 优先级 | 范例 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
- | 1 | 一元 - 符号 (负号) | - | 与参数相同 |
| |||||||||||||||||||
e (位于子表达式之间) |
2 | *10^ 以10为底的指数 科学记号 |
* pow (10,..) | 浮点数,除非左边的参数是整数且指数是非负整数 |
错误范例:
| |||||||||||||||||||
exp |
1 | [自然指数函数 ex | exp | 浮点数 |
比较:
| |||||||||||||||||||
ln |
1 | 自然对数 | log | 浮点数 |
可用换底公式转换为常用对数,例如 log 2 ≒ 0.3010:
| |||||||||||||||||||
abs |
1 | 绝对值 | abs | 同于输入值,并且非负 |
| |||||||||||||||||||
sqrt |
1 | 平方根 | sqrt | 浮点数 |
此平方根计算无法处理结果为虚数或复数的情况
| |||||||||||||||||||
trunc |
1 | 截尾函数 | (int), 即转换型态]]为整数 | 整数 |
| |||||||||||||||||||
floor |
1 | 向下取整函数 | floor | 浮点数 |
| |||||||||||||||||||
ceil |
1 | 向上取整函数 | ceil | 浮点数 |
| |||||||||||||||||||
sin |
1 | 正弦函数 | sin | 浮点数 |
若要使用角度(如30°)可表达为:
| |||||||||||||||||||
cos |
1 | 余弦函数 | cos | 浮点数 |
| |||||||||||||||||||
tan |
1 | 正切函数 | tan | 浮点数 |
| |||||||||||||||||||
asin |
1 | 反正弦 | asin | 浮点数 |
| |||||||||||||||||||
acos |
1 | 反余弦 | acos | 浮点数 |
| |||||||||||||||||||
atan |
1 | 反正切 | atan | 浮点数 |
| |||||||||||||||||||
not |
1 | 逻辑非 | ! | 以整数表达 的布林值 (1 或 0) |
| |||||||||||||||||||
^ |
2 | 幂 (指数) | pow | 原则上为浮点数。若底数为整数且指数为非负整数则为整数 |
| |||||||||||||||||||
* |
2 | 乘法 | * | 若两参数为整数,则为整数,否则为浮点数 |
| |||||||||||||||||||
/ (或写作div) |
2 | 除法 (div非整数除法[1]) |
/ | 原则上为浮点数。若两参数皆为整数且整除则为整数 |
| |||||||||||||||||||
mod |
2 | 模除。 实行带余除法 时会将前后两数截 成整数。[1] |
% | 整数 |
部分除数无法取余数 (见限制章节):
| |||||||||||||||||||
fmod |
2 | 模除,浮点数版本。 返回减去第二参数之整数 倍的第一参数。 |
fmod | 浮点数 |
| |||||||||||||||||||
+ |
2 | 加法 | + | 若两参数皆为整数则为整数,其余浮点数 |
| |||||||||||||||||||
- |
2 | 减法 | - | 若两参数皆为整数则为整数,其余浮点数 |
| |||||||||||||||||||
round |
2 | 将指定的小数位数 做四舍五入,若为 负值则会对指数 为数做四舍五入。 |
round | 浮点数 |
| |||||||||||||||||||
= |
2 | 相等(数字或逻辑的 相等运算,不支援 字串比对) |
== | 以整数表达 的布林值 (1 或 0) |
错误用法:
| |||||||||||||||||||
< |
2 | 小于(不适用于字串 比较) |
< | 以整数表达 的布林值 (1 或 0) |
wrong:
| |||||||||||||||||||
> |
2 | 大于(当参数颠倒时 与 < 相同) |
> | 以整数表达 的布林值 (1 或 0) |
| |||||||||||||||||||
<= |
2 | 小于等于(当参数 颠倒时与 >= 相同) |
<= | 以整数表达 的布林值 (1 或 0) |
| |||||||||||||||||||
>= |
2 | 大于等于(<的否定) | >= | 以整数表达 的布林值 (1 或 0) |
| |||||||||||||||||||
and |
2 | 逻辑与 | && | 以整数表达 的布林值 (1 或 0) |
| |||||||||||||||||||
or |
2 | 逻辑或 | || | 以整数表达 的布林值 (1 或 0) |
| |||||||||||||||||||
+ |
1 | 一元 + 符号 (正号) | (不适用) | 与参数相同 | 不适用 |
| ||||||||||||||||||
(数字) | 0 | 普通十进制的无符号 数字(一元加减和e]] 被视为运算符,请 参见本表的其他地方) |
floatval | 浮点数 | 不适用 |
| ||||||||||||||||||
e 作为子表达式 |
0 | 数学常数e | exp(1) | 浮点数 | 不适用 |
| ||||||||||||||||||
pi |
0 | 数学常数圆周率 π]] | pi | 浮点数 | 不适用 |
|
错误
此函数进行错误检查并给出信息。
- Expression error: stack exhausted:表达式太长,栈耗尽;
- Expression error: unexpected number:不可预期的数字;
- Expression error: unexpected preg_match failure:运算符匹配失败;
- Expression error: unrecognised word "$1":不可识别的运算符;
- Expression error: unexpected $1 operator:不可预期的运算符;
- Expression error: Missing operand for $1:缺少运算数;
- Expression error: unexpected closing bracket:不可预料的“)”
- Expression error: unrecognised punctuation character "$1":不可识别的标点字符;
- Expression error: unclosed bracket:缺少“)”
- Division by zero:零除错误;
- In sqrt: Result is not a number:计算结果为虚数。
- Invalid argument for "$1": "$2":超出"$1"的定义域。
错误的样例
{{#expr: ---...---1}}(110个“-”)
= 表达式错误:堆栈耗尽。{{#expr: ( ) 1}}
= 表达式错误:预期外的数字。{{#expr: 2 d 1}}
= 表达式错误:无法识别词语“d”。{{#expr: * 1}}
= 表达式错误:预期外的*运算符。{{#expr: -}}
= 表达式错误:缺少-的操作数。{{#expr: ( 1 ) )}}
= 表达式错误:预期外的反括号。{{#expr: 一}}
= 表达式错误:无法识别标点符号“一”。{{#expr: ( 1}}
= 表达式错误:未封闭的括号。{{#expr: 1 / 0}}
= 除以零。{{#expr:sqrt-1}}
= sqrt内:结果不是数字。{{#expr:ln0}}
= ln的参数无效:<= 0。{{#expr:asin-22}}
= asin的参数无效:< -1或> 1。
误差
浮点误差有可能造成某些运算的不可预料的结果,特别是最后几位数字的不精确。因此在使用此函数进行实数运算时,注意不要进行比较运算。
注意内部数字表示
{{#expr: 100000000000000000}}
= 1.0E+17{{#expr: {{#expr: 100000000000000000}}}}
= 1.0E+17{{#expr: (-1.5-0.5 round 0)+2}}
= 0{{#expr: -0.1 round 0}}
= -0{{#expr: 2017.5000 round 3}}
= 2017.5:使用时会有无法补零的情形。欲补零,可以使用{{Round}}模板来解决
在返回值中,有可能出现无法再计算的数的情况,或者有一部分有效数字没有显示出来。因此在使用此函数进行运算时,须注意这些问题。