Jump to content

Template:Digit

From mediawiki.org


This template gives the digit at a given position of a given positive integer, expressed in a specified numeral system.

  • The first parameter gives the number.
  • The second parameter is the position of the required digit (1 being the rightmost, 2 the one to the left, etc.).
  • The third parameter is the radix of the numeral system (default:10), 2 for binary, 8 for octal, 16 for hexadecimal.

For a radix > 10, the value is given in decimals, for instance, 15 in the hexadecimal system will give for the rightmost digit 15 (which should be "F")

Usage:
{{Digit|decimal integer|digit no|numeral system number}}
All parameters must be positive integers.

Examples

[edit]
  • {{digit|13|1|2}} β†’ 1
  • {{digit|13|2|2}} β†’ 0
  • {{digit|13|3|2}} β†’ 1
  • {{digit|13|4|2}} β†’ 1
  • {{digit|9002543211234567|1}} β†’ 7
  • {{digit|9002543211234567|2}} β†’ 6
  • {{digit|9002543211234567|3}} β†’ 5
  • {{digit|9002543211234567|4}} β†’ 4
  • {{digit|9002543211234567|5}} β†’ 3
  • {{digit|9002543211234567|6}} β†’ 2
  • {{digit|9002543211234567|7}} β†’ 1
  • {{digit|9002543211234567|8}} β†’ 1
  • {{digit|9002543211234567|9}} β†’ 2
  • {{digit|9002543211234567|10}} β†’ 3
  • {{digit|9002543211234567|11}} β†’ 4
  • {{digit|9002543211234567|12}} β†’ 5
  • {{digit|9002543211234567|13}} β†’ 2
  • {{digit|9002543211234567|14}} β†’ 0
  • {{digit|9002543211234567|15}} β†’ 0
  • {{digit|9002543211234567|16}} β†’ 9
  • {{digit|9002543211234567|17}} β†’ 0
  • {{digit|9002543211234567|18}} β†’ 0
  • {{digit|9002543211234567|19}} β†’ 0
  • {{hex|9002543211234567}} β†’ 1.ffbc3ee2e4507hex*2^52
  • {{digit|9002543211234567|1|16}} β†’ 7
  • {{digit|9002543211234567|2|16}} β†’ 0
  • {{digit|9002543211234567|3|16}} β†’ 5
  • {{digit|9002543211234567|4|16}} β†’ 4
  • {{digit|9002543211234567|5|16}} β†’ 14
  • {{digit|9002543211234567|6|16}} β†’ 2
  • {{digit|9002543211234567|7|16}} β†’ 14
  • {{digit|9002543211234567|8|16}} β†’ 14
  • {{digit|9002543211234567|9|16}} β†’ 3
  • {{digit|9002543211234567|10|16}} β†’ 12
  • {{digit|9002543211234567|11|16}} β†’ 11
  • {{digit|9002543211234567|12|16}} β†’ 15
  • {{digit|9002543211234567|13|16}} β†’ 15
  • {{digit|9002543211234567|14|16}} β†’ 1
  • {{digit|9002543211234567|15|16}} β†’ 0

Large numbers of type integer

[edit]

Special care has been taken to make the result exact even for large numbers of type integer.

  • {{digit|trunc(9134567890e9)+trunc123456789|1}} β†’ 9
  • {{#expr:trunc16*trunc(9002543211234567)+trunc7}} β†’ 144040691379753079
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|1}} β†’ 9
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|2}} β†’ 7
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|3}} β†’ 0
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|4}} β†’ 3
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|5}} β†’ 5
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|6}} β†’ 7
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|7}} β†’ 9
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|8}} β†’ 7
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|9}} β†’ 3
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|10}} β†’ 1
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|11}} β†’ 9
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|12}} β†’ 6
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|13}} β†’ 0
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|14}} β†’ 4
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|15}} β†’ 0
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|16}} β†’ 4
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|17}} β†’ 4
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|18}} β†’ 1
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|19}} β†’ 0
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|1|1e3}} β†’ 79
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|2|1e3}} β†’ 753
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|3|1e3}} β†’ 379
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|4|1e3}} β†’ 691
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|5|1e3}} β†’ 40
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|6|1e3}} β†’ 144
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|7|1e3}} β†’ 0
  • {{digit|trunc90140406913e8+trunc79753079|7|1e3}} β†’ 9
  • {{digit|trunc90140406913e8+trunc79753079|8|1e3}} β†’ 0
  • {{hex|trunc1440406913e8+trunc79753079}} β†’ 1.ffbc3ee2e4507hex*2^56
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|1|16}} β†’ 7
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|2|16}} β†’ 7
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|3|16}} β†’ 0
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|4|16}} β†’ 5
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|5|16}} β†’ 4
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|6|16}} β†’ 14
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|7|16}} β†’ 2
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|8|16}} β†’ 14
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|9|16}} β†’ 14
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|10|16}} β†’ 3
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|11|16}} β†’ 12
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|12|16}} β†’ 11
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|13|16}} β†’ 15
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|14|16}} β†’ 15
  • {{digit|trunc((trunc(1440406913))e trunc(7+1)+trunc79753079)|15|16}} β†’ 1

Limitation

[edit]

This template applies function mod with as second argument the third parameter and a power of it. Therefore it does not work properly if one of these is equal to one of the values for which mod does not work properly. Known values are 2^n-1 and 2^n+1 for n >= 32:

  • {{digit|7|1|2^32-1}} β†’ 7
  • {{digit|2^40|1|2^32-1}} β†’ 256
  • {{digit|2^40|2|2^32-1}} β†’ 256

At least for powers with base <= 30 there are no errors with the test value 17 as first argument, see Help:Mod/powers.