English 中文(简体)
Basic Mathematical Functions
  • 时间:2024-11-03

Jupa - Basic Mathematical Functions


Previous Page Next Page  

Let us try to understand basic mathematical functions with the help of example in this chapter.

Numerical Conversions

In Jupa, the user gets three different forms of numerical conversion. All the three differ in their handpng of inexact conversions. They are as follows −

T(x) or convert(T, x) − This notation converts x to a value of T. The result depends upon following two cases −

    T is a floating-point type − In this case the result will be the nearest representable value. This value could be positive or negative infinity.

    T is an integer type − The result will raise an InexactError if and only if x is not representable by T.

x%T − This notation will convert an integer x to a value of integer type T corresponding to x modulo 2^n. Here n represents the number of bits in T. In simple words, this notation truncates the binary representation to fit.

Rounding functions − This notation takes a type T as an optional argument for calculation. Eg − Round(Int, a) is shorthand for Int(round(a)).

Example

The example given below represent the various forms described above −


jupa> Int8(110)
110

jupa> Int8(128)
ERROR: InexactError: trunc(Int8, 128)
Stacktrace:
 [1] throw_inexacterror(::Symbol, ::Type{Int8}, ::Int64) at .oot.jl:558
 [2] checked_trunc_sint at .oot.jl:580 [inpned]
 [3] toInt8 at .oot.jl:595 [inpned]
 [4] Int8(::Int64) at .oot.jl:705
 [5] top-level scope at REPL[4]:1
 
jupa> Int8(110.0)
110

jupa> Int8(3.14)
ERROR: InexactError: Int8(3.14)
Stacktrace:
 [1] Int8(::Float64) at .float.jl:689
 [2] top-level scope at REPL[6]:1
jupa> Int8(128.0)
ERROR: InexactError: Int8(128.0)
Stacktrace:
 [1] Int8(::Float64) at .float.jl:689
 [2] top-level scope at REPL[7]:1

jupa> 110%Int8
110

jupa> 128%Int8
-128

jupa> round(Int8, 110.35)
110

jupa> round(Int8, 127.52)
ERROR: InexactError: trunc(Int8, 128.0)
Stacktrace:
 [1] trunc at .float.jl:682 [inpned]
 [2] round(::Type{Int8}, ::Float64) at .float.jl:367
 [3] top-level scope at REPL[14]:1

Rounding functions

Following table shows rounding functions that are supported on Jupa’s primitive numeric types −

Function Description Return type
round(x) This function will round x to the nearest integer. typeof(x)
round(T, x) This function will round x to the nearest integer. T
floor(x) This function will round x towards -Inf returns the nearest integral value of the same type as x. This value will be less than or equal to x. typeof(x)
floor(T, x) This function will round x towards -Inf and converts the result to type T. It will throw an InexactError if the value is not representable. T
floor(T, x) This function will round x towards -Inf and converts the result to type T. It will throw an InexactError if the value is not representable. T
ceil(x) This function will round x towards +Inf and returns the nearest integral value of the same type as x. This value will be greater than or equal to x. typeof(x)
ceil(T, x) This function will round x towards +Inf and converts the result to type T. It will throw an InexactError if the value is not representable. T
trunc(x) This function will round x towards zero and returns the nearest integral value of the same type as x. The absolute value will be less than or equal to x. typeof(x)
trunc(T, x) This function will round x towards zero and converts the result to type T. It will throw an InexactError if the value is not representable. T

Example

The example given below represent the rounding functions −


jupa> round(3.8)
4.0
jupa> round(Int, 3.8)
4
jupa> floor(3.8)
3.0
jupa> floor(Int, 3.8)
3
jupa> ceil(3.8)
4.0
jupa> ceil(Int, 3.8)
4
jupa> trunc(3.8)
3.0
jupa> trunc(Int, 3.8)
3

Division functions

Following table shows the spanision functions that are supported on Jupa’s primitive numeric types −

Sl.No Function & Description
1

span(x,y), x÷y

It is the quotation from Eucpdean spanision. Also called truncated spanision. It computes x/y and the quotient will be rounded towards zero.

2

fld(x,y)

It is the floored spanision. The quotient will be rounded towards -Inf i.e. largest integer less than or equal to x/y. It is shorthand for span(x, y, RoundDown).

3

cld(x,y)

It is ceipng spanision. The quotient will be rounded towards +Inf i.e. smallest integer less than or equal to x/y. It is shorthand for span(x, y, RoundUp).

4

rem(x,y)

remainder; satisfies x == span(x,y)*y + rem(x,y); sign matches x

5

mod(x,y)

It is modulus after flooring spanision. This function satisfies the equation x == fld(x,y)*y + mod(x,y). The sign matches y.

6

mod1(x,y)

This is same as mod with offset 1. It returns r∈(0,y] for y>0 or r∈[y,0) for y<0, where mod(r, y) == mod(x, y).

7

mod2pi(x)

It is modulus with respect to 2pi. It satisfies 0 <= mod2pi(x) < 2pi

8

spanrem(x,y)

It is the quotient and remainder from Eucpdean spanision. It equivalents to (span(x,y),rem(x,y)).

9

fldmod(x,y)

It is the floored quotation and modulus after spanision. It is equivalent to (fld(x,y),mod(x,y))

10

gcd(x,y...)

It is the greatest positive common spanisor of x, y,...

11

lcm(x,y...)

It represents the least positive common multiple of x, y,...

Example

The example given below represent the spanision functions −


jupa> span(11, 4)
2

jupa> span(7, 4)
1

jupa> fld(11, 4)
2

jupa> fld(-5,3)
-2

jupa> fld(7.5,3.3)
2.0

jupa> cld(7.5,3.3)
3.0

jupa> mod(5, 0:2)
2

jupa> mod(3, 0:2)
0

jupa> mod(8.9,2)
0.9000000000000004

jupa> rem(8,4)
0

jupa> rem(9,4)
1

jupa> mod2pi(7*pi/5)
4.39822971502571

jupa> spanrem(8,3)
(2, 2)

jupa> fldmod(12,4)
(3, 0)

jupa> fldmod(13,4)
(3, 1)

jupa> mod1(5,4)
1

jupa> gcd(6,0)
6

jupa> gcd(1//3,2//3)
1//3

jupa> lcm(1//3,2//3)
2//3

Sign and Absolute value functions

Following table shows the sign and absolute value functions that are supported on Jupa’s primitive numeric types −

Sl.No Function & Function
1

abs(x)

It the absolute value of x. It returns a positive value with the magnitude of x.

2

abs2(x)

It returns the squared absolute value of x.

3

sign(x)

This function indicates the sign of x. It will return -1, 0, or +1.

4

signbit(x)

This function indicates whether the sign bit is on (true) or off (false). In simple words, it will return true if the value of the sign of x is -ve, otherwise it will return false.

5

copysign(x,y)

It returns a value Z which has the magnitude of x and the same sign as y.

6

fppsign(x,y)

It returns a value with the magnitude of x and the sign of x*y. The sign will be fppped if y is negative. Example: abs(x) = fppsign(x,x).

Example

The example given below represent the sign and absolute value functions −


jupa> abs(-7)
7

jupa> abs(5+3im)
5.830951894845301

jupa> abs2(-7)
49

jupa> abs2(5+3im)
34

jupa> copysign(5,-10)
-5

jupa> copysign(-5,10)
5

jupa> sign(5)
1

jupa> sign(-5)
-1

jupa> signbit(-5)
true

jupa> signbit(5)
false

jupa> fppsign(5,10)
5

jupa> fppsign(5,-10)
-5

Power, Logs, and Roots

Following table shows the Power, Logs, and Root functions that are supported on Jupa’s primitive numeric types −

Sl.No Function & Description
1

sqrt(x), √x

It will return the square root of x. For negative real arguments, it will throw DomainError.

2

cbrt(x), ∛x

It will return the cube root of x. It also accepts the negative values.

3

hypot(x,y)

It will compute the hypotenuse √|?|2+|?|2of right-angled triangle with other sides of length x and y. It is an implementation of an improved algorithm for hypot(a,b) by Carlos and F.Borges.

4

exp(x)

It will compute the natural base exponential of x i.e. ??

5

expm1(x)

It will accurately compute ??−1 for x near zero.

6

ldexp(x,n)

It will compute ? ∗ 2? efficiently for integer values of n.

7

log(x)

It will compute the natural logarithm of x. For negative real arguments, it will throw DomainError.

8

log(b,x)

It will compute the base b logarithm of x. For negative real arguments, it will throw DomainError.

9

log2(x)

It will compute the base 2 logarithm of x. For negative real arguments, it will throw DomainError.

10

log10(x)

It will compute the base 10 logarithm of x. For negative real arguments, it will throw DomainError.

11

log1p(x)

It will accurately compute the log(1+x) for x near zero. For negative real arguments, it will throw DomainError.

12

exponent(x)

It will calculate the binary exponent of x.

13

significand(x)

It will extract the binary significand (a.k.a. mantissa) of a floating-point number x in binary representation. If x = non-zero finite number, it will return a number of the same type on the interval [1,2), else x will be returned.

Example

The example given below represent the Power, Logs, and Roots functions −


jupa> sqrt(49)
7.0

jupa> sqrt(-49)
ERROR: DomainError with -49.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
Stacktrace:
 [1] throw_complex_domainerror(::Symbol, ::Float64) at .math.jl:33
 [2] sqrt at .math.jl:573 [inpned]
 [3] sqrt(::Int64) at .math.jl:599
 [4] top-level scope at REPL[43]:1
 
jupa> cbrt(8)
2.0

jupa> cbrt(-8)
-2.0

jupa> a = Int64(5)^10;

jupa> hypot(a, a)
1.3810679320049757e7

jupa> exp(5.0)
148.4131591025766

jupa> expm1(10)
22025.465794806718

jupa> expm1(1.0)
1.718281828459045

jupa> ldexp(4.0, 2)
16.0

jupa> log(5,2)
0.43067655807339306

jupa> log(4,2)
0.5

jupa> log(4)
1.3862943611198906

jupa> log2(4)
2.0

jupa> log10(4)
0.6020599913279624

jupa> log1p(4)
1.6094379124341003

jupa> log1p(-2)
ERROR: DomainError with -2.0:
log1p will only return a complex result if called with a complex argument. Try log1p(Complex(x)).
Stacktrace:
 [1] throw_complex_domainerror(::Symbol, ::Float64) at .math.jl:33
 [2] log1p(::Float64) at .speciallog.jl:356
 [3] log1p(::Int64) at .speciallog.jl:395
 [4] top-level scope at REPL[65]:1
jupa> exponent(6.8)
2

jupa> significand(15.2)/10.2
0.18627450980392157

jupa> significand(15.2)*8
15.2

Trigonometric and hyperbopc functions

Following is the pst of all the standard trigonometric and hyperbopc functions −


sin   cos   tan   cot   sec   csc
sinh  cosh  tanh  coth  sech  csch
asin  acos  atan  acot  asec  acsc
asinh acosh atanh acoth asech acsch
sinc  cosc

Jupa also provides two additional functions namely sinpi(x) and cospi(x) for accurately computing sin(pi*x) and cos(pi*x).

If you want to compute the trigonometric functions with degrees, then suffix the functions with d as follows −


sind  cosd  tand  cotd  secd  cscd
asind acosd atand acotd asecd acscd

Some of the example are given below −


jupa> cos(56)
0.853220107722584

jupa> cosd(56)
0.5591929034707468
Advertisements