Skip to content

Commit

Permalink
:+100: MathConst
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolewski committed Apr 1, 2015
1 parent e1c0242 commit 3e28e64
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions base/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

immutable MathConst{sym} <: Real end

show{sym}(io::IO, x::MathConst{sym}) = print(io, "$sym = $(string(float(x))[1:15])...")
function show{sym}(io::IO, x::MathConst{sym})
strx = string(float(x))
subs = strx[1:min(15,length(strx))]
dots = length(strx) >= length(subs) ? "..." : ""
print(io, "$sym = $subs", dots)
end

promote_rule{s}(::Type{MathConst{s}}, ::Type{Float32}) = Float32
promote_rule{s,t}(::Type{MathConst{s}}, ::Type{MathConst{t}}) = Float64
Expand Down Expand Up @@ -86,7 +91,7 @@ macro math_const(sym, val, def)
quote
const $esym = MathConst{$qsym}()
$bigconvert
Base.convert(::Type{Float64}, ::MathConst{$qsym}) = $val
Base.convert(::Type{Float64}, ::MathConst{$qsym}) = $(Float64(val))
Base.convert(::Type{Float32}, ::MathConst{$qsym}) = $(Float32(val))
@assert isa(big($esym), BigFloat)
@assert Float64($esym) == Float64(big($esym))
Expand All @@ -98,6 +103,7 @@ big(x::MathConst) = convert(BigFloat,x)

## specific mathematical constants

@math_const 💯 100 BigFloat(100)
@math_const π 3.14159265358979323846 pi
@math_const e 2.71828182845904523536 exp(big(1))
@math_const γ 0.57721566490153286061 euler
Expand Down

0 comments on commit 3e28e64

Please sign in to comment.