「テンプレート:Mod」の版間の差分

提供:脳科学辞典
ナビゲーションに移動 検索に移動
bsd>Jo-Jo Eumerus
(Changed protection level for "Template:Mod": Allowing template editors on some templates with full protection dating back to pre-WP:TPROT times which aren't too often or too sensitively used. ([Edit=Require template editor access] (indefinite)...)
(1版 をインポートしました)
 
(相違点なし)

2020年12月29日 (火) 14:30時点における最新版

[edit] [purge] Documentation icon Template documentation
Description:

Implements the mathematical modulo operator. It should not be confused with the parser function mod (remainder of division after truncating both operands to an integer).

The returned result is always of the same sign as the divisor or zero, and its absolute value is lower than the absolute value of the divisor. This allows this template to be fully compatible with cyclic modular arithmetic (for exampel in calendar computations).

For example if the given modulus is 10, the result will always be positive or null and lower than 10 (all dividend values in that positive range will be returned without change). This is not true with the mod and fmod operators in expressions evaluated with the #expr: ParserFunction extension of MediaWiki.

However, this template returns 0 if the divisor is null (this template should never return any division by zero error).

Usage:
{{Mod|dividend|divisor}}
Examples with subtitution of expressions when saving pages:
  • {{subst:Mod|12|7}} = 5.
Examples with positive integer modulus (all results must be positive or zero):
  • {{Mod|12|10}} = 2.
  • {{Mod|10.1|10}} = 0.1.
    • compare: {{#expr: 10.1 mod 10}} = 0.
  • {{Mod|10|10}} = 0.
  • {{Mod|2|10}} = 2.
  • {{Mod|0|10}} = 0.
  • {{Mod|-2|10}} = 8.
  • {{Mod|-10|10}} = 0.
  • {{Mod|-10.1|10}} = 9.9.
  • {{Mod|-12|10}} = 8.
Examples with negative integer modulus (all results must be negative or zero):
  • {{Mod|12|-10}} = -8.
    • compare: {{#expr: 12 mod -10}} = 2.
    • compare: {{#expr: 12 fmod -10}} = 2.
  • {{Mod|10.1|-10}} = 0.1.
  • {{Mod|10|-10}} = 0.
  • {{Mod|2|-10}} = -8.
  • {{Mod|0|-10}} = 0.
  • {{Mod|-2|-10}} = -2.
  • {{Mod|-10|-10}} = 0.
  • {{Mod|-10.1|-10}} = -0.1.
  • {{Mod|-12|-10}} = -2.
Examples with positive non-integer modulus (all results must be positive or zero):
  • {{Mod|21.5|10.5}} = 0.5.
    • compare: {{#expr: 21.5 mod 10.5}} = 1.
    • compare: {{#expr: 21.5 fmod 10.5}} = 0.5.
  • {{Mod|21.1|10.5}} = 0.1.
  • {{Mod|21|10.5}} = 0.
  • {{Mod|20.9|10.5}} = 10.4.
  • {{Mod|11|10.5}} = 0.5.
  • {{Mod|10.6|10.5}} = 0.1.
  • {{Mod|10.5|10.5}} = 0.
  • {{Mod|10.1|10.5}} = 10.1.
  • {{Mod|10|10.5}} = 10.
  • {{Mod|2|10.5}} = 2.
  • {{Mod|0|10.5}} = 0.
  • {{Mod|-2|10.5}} = 8.5.
  • {{Mod|-10|10.5}} = 0.5.
  • {{Mod|-10.1|10.5}} = 0.4.
  • {{Mod|-10.5|10.5}} = 0.
  • {{Mod|-10.6|10.5}} = 10.4.
  • {{Mod|-11|10.5}} = 10.
  • {{Mod|-20.9|10.5}} = 0.1.
  • {{Mod|-21|10.5}} = 0.
  • {{Mod|-21.1|10.5}} = 10.4.
  • {{Mod|-21.5|10.5}} = 10.
Examples with negative non-integer modulus (all results must be positive or zero):
  • {{Mod|21.5|-10.5}} = -10.
    • compare: {{#expr: 21.5 mod -10.5}} = 1.
    • compare: {{#expr: 21.5 fmod -10.5}} = 0.5.
Examples with null modulus (all results are zero by design in this template, without error):
  • {{Mod|2|0}} = 0.
    • compare: {{#expr: 2 mod 0}} = 0 で除算しました。.
    • compare: {{#expr: 2 fmod 0}} = 0 で除算しました。.
  • {{Mod|0|0}} = 0.
    • compare: {{#expr: 0 mod 0}} = 0 で除算しました。.
    • compare: {{#expr: 0 fmod 0}} = 0 で除算しました。.
  • {{Mod|-2|0}} = 0.
    • compare: {{#expr: -2 mod 0}} = 0 で除算しました。.
    • compare: {{#expr: -2 fmod 0}} = 0 で除算しました。.
Examples with possible overflows on servers using 32-bit integers:
  • {{Mod|123|(2^32-1)}} → 123
    • compare: {{#expr: 123 mod (2^32-1)}} → 123 (wrong on 32-bit servers)
    • compare: {{#expr: 123 fmod (2^32-1)}} → 123 (works on recent versions of MediaWiki)
  • {{Mod|123|(2^64-1)}} → 123
    • compare: {{#expr: 123 mod (2^64-1)}}0 で除算しました。 (wrong on 32-bit and 64-bit serverw)
    • compare: {{#expr: 123 fmod (2^64-1)}} → 123 (works on recent versions of MediaWiki)
Examples with non numeric parameters (all results are expression errors by design in this template):
  • {{Mod|a|b}} = 構文エラー:「b」を認識できません。.
    • compare: {{#expr: a mod b}} = 構文エラー:「a」を認識できません。.
    • compare: {{#expr: a fmod b}} = 構文エラー:「a」を認識できません。.
See also: