-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rename: MathConst => Irrational #11922
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -535,3 +535,6 @@ end | |
|
||
const UnionType = Union | ||
export UnionType | ||
|
||
const MathConst = Irrational | ||
export MathConst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,7 @@ export | |
LinSpace, | ||
LocalProcess, | ||
LowerTriangular, | ||
MathConst, | ||
Irrational, | ||
Matrix, | ||
MergeSort, | ||
Nullable, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# This file is a part of Julia. License is MIT: http://julialang.org/license | ||
|
||
## general machinery for irrational mathematical constants | ||
|
||
immutable Irrational{sym} <: Real end | ||
|
||
show{sym}(io::IO, x::Irrational{sym}) = print(io, "$sym = $(string(float(x))[1:15])...") | ||
|
||
promote_rule{s}(::Type{Irrational{s}}, ::Type{Float32}) = Float32 | ||
promote_rule{s,t}(::Type{Irrational{s}}, ::Type{Irrational{t}}) = Float64 | ||
promote_rule{s,T<:Number}(::Type{Irrational{s}}, ::Type{T}) = promote_type(Float64,T) | ||
|
||
convert(::Type{FloatingPoint}, x::Irrational) = Float64(x) | ||
convert(::Type{Float16}, x::Irrational) = Float16(Float32(x)) | ||
convert{T<:Real}(::Type{Complex{T}}, x::Irrational) = convert(Complex{T}, convert(T,x)) | ||
convert{T<:Integer}(::Type{Rational{T}}, x::Irrational) = convert(Rational{T}, Float64(x)) | ||
|
||
@generated function call{T<:Union{Float32,Float64},s}(t::Type{T},c::Irrational{s},r::RoundingMode) | ||
f = T(big(c()),r()) | ||
:($f) | ||
end | ||
|
||
=={s}(::Irrational{s}, ::Irrational{s}) = true | ||
==(::Irrational, ::Irrational) = false | ||
|
||
# Irationals are not rational, so unequal to everything else | ||
==(x::Irrational, y::Real) = false | ||
==(x::Real, y::Irrational) = false | ||
|
||
# Irrational vs FloatingPoint | ||
<(x::Irrational, y::Float64) = Float64(x,RoundUp) <= y | ||
<(x::Float64, y::Irrational) = x <= Float64(y,RoundDown) | ||
<(x::Irrational, y::Float32) = Float32(x,RoundUp) <= y | ||
<(x::Float32, y::Irrational) = x <= Float32(y,RoundDown) | ||
<(x::Irrational, y::Float16) = Float32(x,RoundUp) <= y | ||
<(x::Float16, y::Irrational) = x <= Float32(y,RoundDown) | ||
<(x::Irrational, y::BigFloat) = with_bigfloat_precision(precision(y)+32) do | ||
big(x) < y | ||
end | ||
<(x::BigFloat, y::Irrational) = with_bigfloat_precision(precision(x)+32) do | ||
x < big(y) | ||
end | ||
|
||
<=(x::Irrational,y::FloatingPoint) = x < y | ||
<=(x::FloatingPoint,y::Irrational) = x < y | ||
|
||
# Irrational vs Rational | ||
@generated function <{T}(x::Irrational, y::Rational{T}) | ||
bx = big(x()) | ||
bx < 0 && T <: Unsigned && return true | ||
rx = rationalize(T,bx,tol=0) | ||
rx < bx ? :($rx < y) : :($rx <= y) | ||
end | ||
@generated function <{T}(x::Rational{T}, y::Irrational) | ||
by = big(y()) | ||
by < 0 && T <: Unsigned && return false | ||
ry = rationalize(T,by,tol=0) | ||
ry < by ? :(x <= $ry) : :(x < $ry) | ||
end | ||
<(x::Irrational, y::Rational{BigInt}) = big(x) < y | ||
<(x::Rational{BigInt}, y::Irrational) = x < big(y) | ||
|
||
<=(x::Irrational,y::Rational) = x < y | ||
<=(x::Rational,y::Irrational) = x < y | ||
|
||
|
||
hash(x::Irrational, h::UInt) = 3*object_id(x) - h | ||
|
||
-(x::Irrational) = -Float64(x) | ||
for op in Symbol[:+, :-, :*, :/, :^] | ||
@eval $op(x::Irrational, y::Irrational) = $op(Float64(x),Float64(y)) | ||
end | ||
|
||
macro irrational(sym, val, def) | ||
esym = esc(sym) | ||
qsym = esc(Expr(:quote, sym)) | ||
bigconvert = isa(def,Symbol) ? quote | ||
function Base.convert(::Type{BigFloat}, ::Irrational{$qsym}) | ||
c = BigFloat() | ||
ccall(($(string("mpfr_const_", def)), :libmpfr), | ||
Cint, (Ptr{BigFloat}, Int32), | ||
&c, MPFR.ROUNDING_MODE[end]) | ||
return c | ||
end | ||
end : quote | ||
Base.convert(::Type{BigFloat}, ::Irrational{$qsym}) = $(esc(def)) | ||
end | ||
quote | ||
const $esym = Irrational{$qsym}() | ||
$bigconvert | ||
Base.convert(::Type{Float64}, ::Irrational{$qsym}) = $val | ||
Base.convert(::Type{Float32}, ::Irrational{$qsym}) = $(Float32(val)) | ||
@assert isa(big($esym), BigFloat) | ||
@assert Float64($esym) == Float64(big($esym)) | ||
@assert Float32($esym) == Float32(big($esym)) | ||
end | ||
end | ||
|
||
big(x::Irrational) = convert(BigFloat,x) | ||
|
||
## specific irriational mathematical constants | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo here: "irriational" -> "irrational". |
||
|
||
@irrational π 3.14159265358979323846 pi | ||
@irrational e 2.71828182845904523536 exp(big(1)) | ||
@irrational γ 0.57721566490153286061 euler | ||
@irrational catalan 0.91596559417721901505 catalan | ||
@irrational φ 1.61803398874989484820 (1+sqrt(big(5)))/2 | ||
|
||
# aliases | ||
const pi = π | ||
const eu = e | ||
const eulergamma = γ | ||
const golden = φ | ||
|
||
# special behaviors | ||
|
||
# use exp for e^x or e.^x, as in | ||
# ^(::Irrational{:e}, x::Number) = exp(x) | ||
# .^(::Irrational{:e}, x) = exp(x) | ||
# but need to loop over types to prevent ambiguity with generic rules for ^(::Number, x) etc. | ||
for T in (Irrational, Rational, Integer, Number) | ||
^(::Irrational{:e}, x::T) = exp(x) | ||
end | ||
for T in (Range, BitArray, SparseMatrixCSC, StridedArray, AbstractArray) | ||
.^(::Irrational{:e}, x::T) = exp(x) | ||
end | ||
^(::Irrational{:e}, x::AbstractMatrix) = expm(x) | ||
|
||
log(::Irrational{:e}) = 1 # use 1 to correctly promote expressions like log(x)/log(e) | ||
log(::Irrational{:e}, x) = log(x) | ||
|
||
# align along = for nice Array printing | ||
function alignment(x::Irrational) | ||
m = match(r"^(.*?)(=.*)$", sprint(showcompact_lim, x)) | ||
m == nothing ? (length(sprint(showcompact_lim, x)), 0) : | ||
(length(m.captures[1]), length(m.captures[2])) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: Irationals is missing an "r". Also, I'm not sure I fully understand this comment. The beginning is redundant and the ending is kinda vague (especially since technically irrationals are reals).