-
-
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
RFC: More methods for Irrational #32117
Conversation
Another effect that should be mentioned is that |
...and for that reason it seemed to make sense to also make |
I think that's taking the documentation a bit overly literal in this case. I don't know of any other cases where |
|
I am running into issues with |
I only care about |
base/irrationals.jl
Outdated
Rational(x::AbstractIrrational) = rationalize(x) | ||
rationalize(x::Irrational{T}) where T = throw(ArgumentError("Type must be specified. Use rationalize(Int, $T) to obtain a Rational{Int} approximation to the irrational constant $T.")) | ||
Math.mod2pi(x::AbstractIrrational) = Float64(mod2pi(big(x))) | ||
Math.rem2pi(x::AbstractIrrational, m) = Float64(rem2pi(big(x), m)) |
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.
Shouldn't use BigFloat
arithmetic at runtime.
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.
Good point. I will turn these into generated functions.
I'm okay with |
Sounds good, will do. |
Done. |
Define `zero(Irrational)` and `one(Irrational)` to return `false` and `true` respectively. (This is breaking if someone is currently using `true * pi` or `pi + false` as a way of forcing conversion to `Float64`.) Make `false` act as the additive identity for `Irrational` by defining a method for `+`. (Not type stable) Make `true` act as the multiplicative identity for `Irrational` by defining a method for `*` (Not type stable) Make `widemul` of two `Irrational`s return a `BigFloat` Make `sin(pi)` and `tan(pi)` return exactly 0.0 Define a number of methods on `Irrational` to default to conversion to Float64.
I'm very sorry. I tried to rebase this on the latest master, and I obviously messed up. |
d3a83a7
to
020d8ee
Compare
@generated Math.mod2pi(x::AbstractIrrational) = Float64(mod2pi(big(x()))) | ||
@generated Math.rem2pi(x::AbstractIrrational, r::RoundingMode) = Float64(rem2pi(big(x()), r())) |
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.
@generated Math.mod2pi(x::AbstractIrrational) = Float64(mod2pi(big(x()))) | |
@generated Math.rem2pi(x::AbstractIrrational, r::RoundingMode) = Float64(rem2pi(big(x()), r())) | |
Math.mod2pi(x::AbstractIrrational) = Float64(mod2pi(big(x))) | |
Math.rem2pi(x::AbstractIrrational, r::RoundingMode) = Float64(rem2pi(big(x), r)) |
@eval $op(x::AbstractIrrational, y::AbstractIrrational) = $op(Float64(x),Float64(y)) | ||
end | ||
*(x::Bool, y::AbstractIrrational) = ifelse(x, Float64(y), 0.0) | ||
*(x::Bool, y::AbstractIrrational) = x ? y : zero(y) |
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.
Type unstable --- I'm not sure this is worth it.
@@ -45,6 +51,7 @@ promote_rule(::Type{S}, ::Type{T}) where {S<:AbstractIrrational,T<:Number} = pro | |||
AbstractFloat(x::AbstractIrrational) = Float64(x)::Float64 | |||
Float16(x::AbstractIrrational) = Float16(Float32(x)::Float32) | |||
Complex{T}(x::AbstractIrrational) where {T<:Real} = Complex{T}(T(x)) | |||
Complex(x::AbstractIrrational, y::AbstractIrrational) = Complex(Float64(x), Float64(y)) |
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.
Seems unnecessary?
julia> Complex(π, ℯ)
3.141592653589793 + 2.718281828459045im
Complex(x::AbstractIrrational, y::AbstractIrrational) = Complex(Float64(x), Float64(y)) |
|
||
widemul(x::AbstractIrrational, y::AbstractIrrational) = big(x)*big(y) | ||
|
||
(::Colon)(x::AbstractIrrational, y::AbstractIrrational) = Float64(x):Float64(y) |
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.
Seems to already work without this?
(::Colon)(x::AbstractIrrational, y::AbstractIrrational) = Float64(x):Float64(y) |
julia> ℯ:π
2.718281828459045:1.0:2.718281828459045
We discussed this on triage, and felt that it is better to be explicit about the transforms for irrational numbers into the appropriate computational types. Thanks for the PR however. I know it is sad to see one not get merged, but it happens to all of us. |
The purpose of this PR is to make
Irrational
work with many more functions, including those mentioned in #31949 #26701 and #22878.The main way that this is accomplished is by defining
The documentation says that these functions should return the additive/multiplicative identities for the type
T
- not of the type T.In order to make
false
andtrue
into additive/multiplicative identities, this PR also re-definesIrrational
-Bool
addition, subtraction and multiplication.This is technically breaking if someone is currently using
true * pi
orpi + false
as a way of forcing conversion toFloat64
. (The alternatives1 * pi
and0 + pi
continue to work as before.)The new +, - and * methods are not type-stable, which the old ones were. On the other hand, Julia is now much better at type inference than it was when the original methods were written.
With this, a whole range of functions start to work, as the only reason they were throwing before was a call to
one
orzero
. To give one example,sec(x::Number)
is defined asone(x)/cos(x)
which now works.This PR also extends the list of two-argument functions that convert to Float64 when given two identical
Irrational
arguments. The intent is to make all "math-like" functions inBase
work withIrrational
. (This should mitigate the effect oftrue*pi
now returningpi
rather than aFloat64
.)Finally, some minor fixes:
widemul
of twoIrrational
s now returns aBigFloat
same aswidemul
ofFloat64
s, andsin(pi)
andtan(pi)
now return exactly0.0
.