Course 2 : Numbers

Clear["Global`*"]

Accurate or precise Numbers

(* integers, rationals, irrationals *)

k=1+(1/9)+(5/9)(Sqrt[2])

Simplify[k]

N[k]

N[k,10]

known constants

Pi
E

N[Pi,10]
N[E,10]

arithmetic operations

1/9+2/5

(Sqrt[2])+(Sqrt[16])

(((Sqrt[2])-(Sqrt[5]))/(Sqrt[2])) //Simplify

Sin[Pi/4]+Sin[Pi/8]

N[%]

Integer Number functions

Mod[11,3]
Quotient[23,3]
Divisible[23,3]
CoprimeQ[8,11]
GCD[16,12,32]
LCM[16,12,32]
KroneckerDelta[4,5]

Floating-point numbers  (finite precision)

1.234566789
.2345
1.0

Arithmetic operations   (floats & accurate = floats)

x=1.2
y=x*Pi
N[y,20]

number of digits

InputForm[y]

x=100. 10/3
InputForm[%]

Accuracy[x]
Precision[x]

x=1. 10/3000
InputForm[%]
Accuracy[x]
Precision[x]

$MachinePrecision

10^(-Accuracy[x])

Arbitrary precision numbers

x=5.12345678901234567890
Accuracy[x]

y=5.0`20

z=x-y
w=x*y
Print["z: Acc=",Accuracy[z],"   Prec=",   Precision[z]]
Print["w: Acc=",Accuracy[w],"   Prec=",   Precision[w]]

set number of decimal digits

a=SetAccuracy[1/3,50]
b=SetAccuracy[1.0/3,50]

zz=SetAccuracy[z,50]

Complex numbers

k=(1/2)+3I    (* Accurate *)
l=3.5-I     (* floating point - finite precision *)
m=k*k
n=k/l

Re[n]
Im[n]
Conjugate[n]

z=ComplexExpand[(-1)^(1/3)]
z^3 //Simplify

Random numbers

RandomReal[]
RandomReal[{1,2}]
RandomInteger[{0,100}]
RandomComplex[{1+I,2+5 I}]
RandomChoice[{"a","b","c","d","e","f"}]

RandomReal[1,10]

SeedRandom[1980]
RandomReal[]
RandomReal[{1,2}]
RandomInteger[{0,100}]
RandomComplex[{1+I,2+5 I}]