Skip to content

Commit

Permalink
Add exp, log, trigonometric functions and @simonbyrne's test suite
Browse files Browse the repository at this point in the history
Some sign issues were found and commented in test file, and no
errors are thrown yet.
  • Loading branch information
andrioni committed May 3, 2013
1 parent 8b33832 commit 28328d0
Show file tree
Hide file tree
Showing 2 changed files with 765 additions and 5 deletions.
36 changes: 31 additions & 5 deletions base/mpc.jl
Expand Up @@ -10,8 +10,11 @@ export

import
Base: (*), +, -, /, <, <<, >>, <=, ==, >, >=, ^, (~), (&), (|), ($), cmp,
complex, convert, div, imag, integer_valued, isfinite, isinf, isnan,
promote_rule, real, show, showcompact, sqrt, string, get_precision
complex, convert, div, exp, imag, integer_valued, isfinite, isinf, log,
promote_rule, real, show, showcompact, sqrt, string, get_precision,

# trigonometric functions
sin, cos, tan, acos, asin, atan, cosh, sinh, tanh, acosh, asinh, atanh

const ROUNDING_MODE = [0]
const DEFAULT_PRECISION = [256, 256]
Expand Down Expand Up @@ -195,9 +198,6 @@ end
function sqrt(x::MPCComplex)
z = MPCComplex()
ccall((:mpc_sqrt, :libmpc), Int32, (Ptr{MPCComplex}, Ptr{MPCComplex}, Int32), &z, &x, ROUNDING_MODE[end])
if isnan(z)
throw(DomainError())
end
return z
end

Expand All @@ -219,6 +219,32 @@ function ^(x::MPCComplex, y::BigInt)
return z
end

function exp(x::MPCComplex)
z = MPCComplex()
ccall((:mpc_exp, :libmpc), Int32, (Ptr{MPCComplex}, Ptr{MPCComplex}, Int32), &z, &x, ROUNDING_MODE[end])
return z
end

function log(x::MPCComplex)
z = MPCComplex()
ccall((:mpc_log, :libmpc), Int32, (Ptr{MPCComplex}, Ptr{MPCComplex}, Int32), &z, &x, ROUNDING_MODE[end])
return z
end

# Trigonometric functions
# No error handling is done, and NaN are returned directly
# the Base functions behavior
for f in (:sin,:cos,:tan,:acos,:asin,:atan,
:cosh,:sinh,:tanh,:acosh,:asinh,:atanh)
@eval begin
function ($f)(x::MPCComplex)
z = MPCComplex()
ccall(($(string(:mpc_,f)), :libmpc), Int32, (Ptr{MPCComplex}, Ptr{MPCComplex}, Int32), &z, &x, ROUNDING_MODE[end])
return z
end
end
end

# Utility functions
==(x::MPCComplex, y::MPCComplex) = ccall((:mpc_cmp, :libmpc), Int32, (Ptr{MPCComplex}, Ptr{MPCComplex}), &x, &y) == 0

Expand Down

0 comments on commit 28328d0

Please sign in to comment.