# Number types

## base and expansion

Number ( for example angle in turns ) can be used as a :

• decimal number (base = 10 ) [1]
• ratio = rational number ( if number can not be represented as a ratio then it is irrational number )
• in lowest terms ( irreducible form ) : $\tfrac{1}{21}$
• reducible form
• in explicit normalized form ( only when denominator is odd )[2] : $\tfrac{3}{63} = \tfrac{3}{2^{6}-1}$
• decimal floating point number $0.\overline{047619}$ [3][4]
• finite expansion
• endless expansion
• continue infinitely without repeating (in which case the number is called irrational = non-repeating non-terminating decimal numbers[5])
• Recurring or repeating
• (strictly) periodic ( preperiod = 0 , preiod > 0 )
• mixed = eventually periodic ( preperiod > 0 , period > 0 )
• binary number ( base = 2 )[6]
• binary rational number ( ratio) $\tfrac{1}{10101}$
• binary real number
• binary floating point number ( scientific notation )
• binary fixed point number ( notation)
• with repeating sequences : $0.\overline{000011}$
• with endless expansion $0.000011000011000011000011...$

• 1D
• 2D
• dual[7]
• complex

## Examples

### finite

1/5 = 0.2 It is finite number ( terminating decimal ) : preperiod =0 and period = 0

There is also another representation as a preperiodic number with periodic part 0, like :

1/4 = 0.25(0) has a preperiod of 2 and a period of 1

### preperiodic

1/12= 1/(3*2^2) = 0.08(3) so preperiod=2 period=1

1/6=1/(2*3) = 0.1(6) so preperiod=1 preriod=2

77/600 = 77/(2^3*3*5^2) = 0.128(3) so preperiod=3 period = 1

### periodic

1/3 = 0.(3) preperiod=0 preriod=2 ;

1/7 = 0.142857  ; 6 repeating decimal digits

Binary expansion of $\tfrac{1}{21}$ has a period= 6 under doubling map

1/17 = 0.05882352 94117647  ; 16 repeating digits

1/19 = 0.052631578 947368421  ; 18 repeating digits

1/23 = 0.04347826086 95652173913  ; 22 repeating digits

1/29 = 0.0344827 5862068 9655172 4137931  ; 28 repeating digits

1/81 = 0.(012345679) ; (period 9)[8]

1/97 = 0.01030927 83505154 63917525 77319587 62886597 93814432 98969072 16494845 36082474 22680412 37113402 06185567  ; 96 repeating digits

1/267=0.(00374531835205992509363295880149812734082397)

For 3923/6173 decimal expansion has period= 3086 and preperiod 0 [9]

Fraction with period of binary expansion = 34 and decimal denominator :

$d= 2^{34}-1 = 17\ 179\ 869\ 183$

$4985538889/17179869183 = \frac{4985538889_{10}}{17179869183_{10}} = p0100101001001010010100100101001001 = .(0100101001001010010100100101001001) = 0.(0100101001001010010100100101001001) = 0.\overline{0100101001001010010100100101001001}_2 = \frac{100101001001010010100100101001001_2}{1111111111111111111111111111111111_2}$

#### 1/(2^p -1)

algorithm for :

• binary expansion $\frac{1}{2^p-1} = 0.((0)^{p-1}1)$
• decimal expasnion (?)

Decimal fraction 1/(2^p-1) has the same form in binary expansion as fraction 1/(2^p), but repeating. Example 1/(2^5)=0.00001 and 1/(2^5-1)=0.(00001)[10] So binary fraction has period p and preperiod 0.

p $\frac{1}{2^p-1}$ factors(denominator) binary expansion decimal expansion (preperiod ; period) of decimal expansion
2 $\frac{1}{3}$ ((3,1)) 0.(01) 0.(3) (0;1)
3 $\frac{1}{7}$ ((7,1)) 0.(001) 0.(142857) (0;6)
4 $\frac{1}{15}$ ((3,1),(5,1)) 0.(0001) 0.0(6) (1;1)
5 $\frac{1}{31}$ (31,1)) 0.(00001) 0.(032258064516129) (0;15)
6 $\frac{1}{63}$ ((3,2),(7,1)) 0.(000001) 0.(015873)
7 $\frac{1}{127}$ ((127,1)) 0.(0000001) 0.(007874015748031496062992125984251968503937)
8 $\frac{1}{255}$ ((3,1),(5,1),(17,1)) 0.(00000001) 0.0(0392156862745098)
9 $\frac{1}{511}$ ((7,1),(73,1)) 0.(000000001) 0.(001956947162426614481409)
10 $\frac{1}{1023}$ (3, 1), (11, 1), (31, 1)) 0.(0000000001) 0.(001955034213098729227761485826)

Maxima CAS code :

fpprec:100;
fpprintprec:100;
linel:200;
for i:2 thru 200 do print(i, "," ,2^i-1,",", ifactors(2^i-1), bfloat(1/(2^i-1)));


and output :

2 , 3 , [[3, 1]] 3.333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333b-1
3 , 7 , [[7, 1]] 1.428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571429b-1
4 , 15 , [[3, 1], [5, 1]] 6.666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667b-2
5 , 31 , [[31, 1]] 3.225806451612903225806451612903225806451612903225806451612903225806451612903225806451612903225806452b-2
6 , 63 , [[3, 2], [7, 1]] 1.587301587301587301587301587301587301587301587301587301587301587301587301587301587301587301587301587b-2
7 , 127 , [[127, 1]] 7.874015748031496062992125984251968503937007874015748031496062992125984251968503937007874015748031496b-3
8 , 255 , [[3, 1], [5, 1], [17, 1]] 3.921568627450980392156862745098039215686274509803921568627450980392156862745098039215686274509803922b-3
9 , 511 , [[7, 1], [73, 1]] 1.956947162426614481409001956947162426614481409001956947162426614481409001956947162426614481409001957b-3
10 , 1023 , [[3, 1], [11, 1], [31, 1]] 9.775171065493646138807429130009775171065493646138807429130009775171065493646138807429130009775171065b-4
11 , 2047 , [[23, 1], [89, 1]] 4.885197850512945774303859306301905227161700048851978505129457743038593063019052271617000488519785051b-4
12 , 4095 , [[3, 2], [5, 1], [7, 1], [13, 1]] 2.442002442002442002442002442002442002442002442002442002442002442002442002442002442002442002442002442b-4
13 , 8191 , [[8191, 1]] 1.220852154804053229153949456720791112196313026492491759247955072640703210841167134659992674887071176b-4
14 , 16383 , [[3, 1], [43, 1], [127, 1]] 6.103888176768601599218702313373618995300006103888176768601599218702313373618995300006103888176768602b-5
15 , 32767 , [[7, 1], [31, 1], [151, 1]] 3.051850947599719229712820825830866420484023560289315469832453382976775414288766136661885433515427107b-5
......
195 , 50216813883093446110686315385661331328818843555712276103167 , [[7, 1], [31, 1], [79, 1], [151, 1], [8191, 1], [121369, 1], [145295143558111, 1], [134304196845099262572814573351, 1]]
1.991364888915565346286100497209639756988880693990773482592396972073911683910436357812547195643147476b-59
196 , 100433627766186892221372630771322662657637687111424552206335 , [[3, 1], [5, 1], [29, 1], [43, 1], [113, 1], [127, 1], [197, 1], [19707683773, 1], [4363953127297, 1], [4432676798593, 1],
[4981857697937, 1]] 9.956824444577826731430502486048198784944403469953867412961885722016538277004397647478523847608439585b-60
197 , 200867255532373784442745261542645325315275374222849104412671 , [[7487, 1], [26828803997912886929710867041891989490486893845712448833, 1]]
4.978412222288913365715251243024099392472201734976933706480918076420014102865252788343208891152395343b-60
198 , 401734511064747568885490523085290650630550748445698208825343 , [[3, 3], [7, 1], [19, 1], [23, 1], [67, 1], [73, 1], [89, 1], [199, 1], [683, 1], [5347, 1], [20857, 1], [153649, 1], [599479, 1],
[33057806959, 1], [242099935645987, 1]] 2.489206111144456682857625621512049696236100867488466853240452842062943292523389885322591187413241559b-60
199 , 803469022129495137770981046170581301261101496891396417650687 , [[164504919713, 1], [4884164093883941177660049098586324302977543600799, 1]]
1.244603055572228341428812810756024848118050433744233426620224871994705706534385815449042279165881751b-60
200 , 1606938044258990275541962092341162602522202993782792835301375 , [[3, 1], [5, 3], [11, 1], [17, 1], [31, 1], [41, 1], [101, 1], [251, 1], [401, 1], [601, 1], [1801, 1], [4051, 1], [8101, 1],
[61681, 1], [268501, 1], [340801, 1], [2787601, 1], [3173389601, 1]] 6.223015277861141707144064053780124240590252168721167133101120487381613683353656259214578109477561187b-61


## How to find number type

Note that in numerical computations with finite precision ( on computer) :

• if number is represented as a ratio ( of integers) then it is a rational number
• if number has a floating point representation the it is also a rational number because of limited precision = finite expansion

/*

Maxima CAS batch file

*/

remvalue(all);
kill(all);

/*
input = ratio, which automaticaly changed to lowest terms by Maxima CAS
output = string describing a type of decimal expansion

---------------------------------------------------------------------------------
" The rules that determine whether a fraction has recurring decimals or
not are really quite simple.

1. First represent the fraction in its simplest form, by dividing both
numerator and denominator by common factors.

2. Now, look at the denominator.

3.
3.1 If the prime factorization of the denominator contains only the
factors 2 and 5, then the decimal fraction of that fraction will not
have recurring digits. In other words : Terminating decimals represent
rational numbers of the form k/(2^n*5^m)

3.2
A fraction in lowest terms with a prime denominator other than 2 or 5
(i.e. coprime to 10) always produces a repeating decimal.

3.2.1
If the prime factorization yields factors like 3, 7, 11 or other
primes (other than 2 and 5), then that fraction will have a decimal
representation that includes recurring digits.

3.2.2
Moreover, if the denominator's prime factors include 2 and/or 5 in
addition to other prime factors like 3, 7, etc., the decimal
decimals before the recurring part."

http://blogannath.blogspot.com/2010/04/vedic-mathematics-lesson-49-recurring.html

check :
http://www.knowledgedoor.com/2/calculators/convert_a_ratio_of_integers.html

https://en.wikipedia.org/wiki/Repeating_decimal
" A fraction in lowest terms with a prime denominator other than 2 or 5 (i.e. coprime to 10) always produces a repeating decimal.
The length of the repetend (period of the repeating decimal) of 1/p is equal to the order of 10 modulo p.
If 10 is a primitive root modulo p, the repetend length is equal to p − 1; if not,
the repetend length is a factor of p − 1.
This result can be deduced from Fermat's little theorem, which states that 10p−1 = 1 (mod p)."

---------------------------------------------------------------------------------------

*/

GiveRatioType(ratio):=
block
(
[denominator:denom(ratio),
FactorsList ,
Factor,
Has25:false,
HasAlsoOtherPrimes:false,
type ], /* type of decimal expansion of the ratio of integers */

/* compute list of prime factors ofd denominator */
FactorsList:ifactors(denominator),
FactorsList:map(first,FactorsList),
print(denominator, FactorsList),
/* check factors type :
only 2 or 5
also other primes then 2 or 5
*/
if (member(2,FactorsList) or member(5,FactorsList)) then Has25:true,

for Factor in FactorsList do
if (not member(Factor,[2,5])) then
HasAlsoOtherPrimes:true,
print(Has25, HasAlsoOtherPrimes),

/* find type of decimal expansion */
if (not Has25 and HasAlsoOtherPrimes)     then type:"periodic",
if (Has25 and HasAlsoOtherPrimes)     then type:"preperiodic",
if (Has25 and not HasAlsoOtherPrimes) then type:"finite",

return(type)
)$compile(all)$

/* input numbers*/
a:1 $b:3$

r:a/b$type : GiveRatioType(r);  ## Conversions Conversion between : • bases • using bc / dc • ratio and expansion [11] • expansion and rational form [16] • repeating decimals and ratio [17] • Recognizing Rational Numbers From Their Decimal Expansion[18]: "to compute the simple continued fraction of the approximation, and truncate it before a large partial quotient a_n, then compute the value of the truncated continued fraction." • converting decimal fraction to binary : "... we repeatedly multiply the decimal fraction by 2. If the result is greater than or equal to 1, we add a 1 to our answer. If the result is less than 1, we add a 0 to our answer." (from Virginia Tech Online CS module [19]) Algorithm [20]: • Multiply the input decimal fraction by two • from above result • take integer part as the binary digit • take the fractional part as the starting point for the next step • repeat until you either get to 0 or a periodic number • read the number starting from the top - the first binary digit is the first digit after the comma Example of conversion 0.1 decimal fraction to binary fraction :  0.1 * 2 = 0.2 -> 0 0.2 * 2 = 0.4 -> 0 0.4 * 2 = 0.8 -> 0 0.8 * 2 = 1.6 -> 1 0.6 * 2 = 1.2 -> 1 0.2 * 2 = 0.4 -> 0 0.4 * 2 = 0.8 -> 0 0.8 * 2 = 1.6 -> 1 0.6 * 2 = 1.2 -> 1 0.2 * 2 = 0.4 -> 0 0.4 * 2 = 0.8 -> 0 0.8 * 2 = 1.6 -> 1 0.6 * 2 = 1.2 -> 1 0.2 * 2 = 0.4 -> 0  Result: $0.1_{10} = 0.00011(0011)_2$  Graphical conversion ### C itoa function [21] /* itoa example http://www.cplusplus.com/reference/cstdlib/itoa/ */ #include <stdio.h> #include <stdlib.h> int main () { int i; char buffer [33]; printf ("Enter a number: "); scanf ("%d",&i); itoa (i,buffer,10); printf ("decimal: %s\n",buffer); itoa (i,buffer,16); printf ("hexadecimal: %s\n",buffer); itoa (i,buffer,2); printf ("binary: %s\n",buffer); return 0; }  snprintf[22] Binary integer constant[23] "Integer constants can be written as binary constants, consisting of a sequence of ‘0’ and ‘1’ digits, prefixed by ‘0b’ or ‘0B’. This is particularly useful in environments that operate a lot on the bit level (like microcontrollers). The following statements are identical:  i = 42; i = 0x2a; i = 052; i = 0b101010;  The type of these constants follows the same rules as for octal or hexadecimal integer constants, so suffixes like ‘L’ or ‘UL’ can be applied." Macro BOOST_BINARY ### Haskell  Int preperiod = length . dPreperiodic period :: Digits -> Int period = length . dPeriodic digitsAtBase :: Int -> Rational -> Digits digitsAtBase base rational = Digits { dNegative = rational < 0 , dInteger = int , dPreperiodic = pre , dPeriodic = per } where integer :: Integer fraction :: Rational (integer, fraction) = properFraction (abs rational) int | integer == 0 = [0] | otherwise = goInt integer [] goInt i ds | i == 0 = ds | otherwise = goInt i' (fromInteger d : ds) where (i', d) = i divMod baseZ factors :: [Integer] factors = map fromIntegral . nub . primeFactors$ base
isPreperiodic :: Rational -> Bool
isPreperiodic x = any (divides denominator x) factors
baseZ :: Integer
baseZ = fromIntegral base
baseQ :: Rational
baseQ = fromIntegral base
(pre, per) = goPre fraction
where
goPre :: Rational -> ([Int], [Int])
goPre x
| isPreperiodic x = first (d:) (goPre x')
| otherwise = ([], d : goPer x x')
where (d, x') = properFraction (baseQ * x)
goPer :: Rational -> Rational -> [Int]
goPer x0 x
| x0 == x = []
| otherwise = d : goPer x0 x'
where (d, x') = properFraction (baseQ * x)
first :: (a -> c) -> (a, b) -> (c, b)
first f (a, b) = (f a, b)
divides :: Integer -> Integer -> Bool
factor divides number = number mod factor == 0

digitsToString :: [String] -> Digits -> String
digitsToString digits Digits
{ dNegative = sign
, dInteger = int
, dPreperiodic = pre
, dPeriodic = per
}
= (if sign then "-" else "")
++ d int ++ "." ++ d pre ++ "(" ++ d per ++ ")"
where
d = concatMap (digits !!)

atBase :: Int -> Rational -> String
atBase base rational = digitsToString ds (digitsAtBase base rational)
where
ds | base <= 62 = map (:[]) $['0'..'9'] ++ ['A'..'Z'] ++ ['a'..'z'] | otherwise = [ "<" ++ show d ++ ">" | d <- [0 .. base - 1] ] main :: IO () main = do [sbase, sfraction] <- getArgs let (snum, _:sden) = break ('/' ==) sfraction base = read sbase num = read snum den = read sden rational = num % den putStrLn (atBase base rational)  ### Python # https://wiki.python.org/moin/BitManipulation # binary string to integer >>> int('00100001', 2) 33 # conversion from binary string to hex string >>> print "0x%x" % int('11111111', 2) 0xff >>> print "0x%x" % int('0110110110', 2) 0x1b6 >>> print "0x%x" % int('0010101110101100111010101101010111110101010101', 2) 0xaeb3ab57d55  Other methods [24] # How to use numbers in computer programs ? ## Rational "Any number with a finite decimal expansion is a rational number. " In other words : "any floating point number can be converted to a rational number." [25] So in numerical computations one can use only integer of floating points numbers ( rational ). ### Decimal ### Binary #### String Calculation of binary numbers with as a string with replicating parts in Haskell (ghci): -- by Claude Heiland-Allen -- http://mathr.co.uk/blog/haskell.html Prelude> let rep n s = concat (replicate n s) Prelude> putStrLn$ ".(" ++ rep 88 "001" ++ "010)"
.(001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001010)

putStrLn $".(" ++ rep 87 "001" ++ "010001)" .(001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001010001) Prelude> putStrLn$ ".(" ++ rep 88 "001" ++ "0001)"
.(0010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010001)

Prelude> putStrLn \$ ".(" ++ rep 88 "001" ++ "0010)"
.(0010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010)


In Python :

>>> bin(173)
'0b10101101'
>>> int('01010101111',2)
687


#### Literal

In python one can use binary literals :[26]

python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
>>> 0b101111
47


## Irrational

If one wants use irrational number then should check :

• symbolic computations :
• exact number can be used as a symbol, but "you cannot print the whole irrational number"
• infinite continued fraction
• numerical computations : close rational approximations to irrational numbers [27] (the Diophantine Approximation [28])
• ratio of integers
• floating point number
• finite continued fractions

### Inverse golden mean

Using Maxima CAS :

(%i10) print(float(%phi-1));
(%o10).6180339887498949
(%i11) rationalize(float(%phi-1));
(%o11) 347922205179541/562949953421312


and  :

(%i14) print(float(1/%phi));
(%o14) .6180339887498948
(%i15) rationalize(float(1/%phi));
(%o15) 5566755282872655/9007199254740992


where denominator :

$562949953421312 = 1 + 2^{49}$


# Cardinality

In mathematic ( theory) :

• "... the rational numbers are a countable set whereas the irrational numbers are an uncountable set. In other words, there are more irrational numbers than there are rational. " [29]
• "... in the set of real numbers there is continuum of irrational numbers and only aleph-zero rational numbers. Thus probability that any random number is irrational is 1;" ( Bartek Ogryczak) [30] "To be pedantically correct you should have said almost certainly is 1. " – David Hammen

# Random number

The probability that any random number :

• is irrational is almost 1 ( in theory because of cardinality )
• is rational is 1 ( in numerical computations because of limited precision )