Skip to content

Support arbitrary AbstractIrrationals - #116

Open
ararslan wants to merge 1 commit into
masterfrom
aa/abstractirrational
Open

ararslan wants to merge 1 commit into
masterfrom
aa/abstractirrational

Conversation

@ararslan

Copy link
Copy Markdown
Member

The fallback method had been using Irrational{T}, but irrational values needn't be Irrationals, so we can just use AbstractIrrational for that method instead.

Fixes #100

The fallback method had been using `Irrational{T}`, but irrational
values needn't be `Irrational`s, so we can just use `AbstractIrrational`
for that method instead.

Fixes issue 100
Comment thread src/Quadmath.jl
Float128(::Irrational{:π}) = reinterpret(Float128, 0x4000921fb54442d18469898cc51701b8)
Float128(::Irrational{:ℯ}) = reinterpret(Float128, 0x40005bf0a8b1457695355fb8ac404e7a)
Float128(x::Irrational{T}) where {T} = Float128(BigFloat(x))
Float128(x::AbstractIrrational) = Float128(BigFloat(x))

@stevengj stevengj Sep 26, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we specify the precision here? (The default BigFloat precision of 256 is more than what is required, but the user might have called setprecision to a lower precision.)

Suggested change
Float128(x::AbstractIrrational) = Float128(BigFloat(x))
Float128(x::AbstractIrrational) = Float128(BigFloat(x, precision=128))

(technically I guess you could use precision=113, but it seems sensible to have a few guard bits and I doubt there is much of a performance price to round up to the next power of 2)

Comment thread test/runtests.jl
@test abs(cos(Float128(pi)) + 1) < tiny
@test abs(log(Float128(ℯ)) - 1) < tiny
@test abs((2*Float128(MathConstants.golden) - 1)^2 - 5) < 5 * tiny
@test abs(cos(Float128(TwoPi())) - 1) < tiny

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to test the above issue by surrounding the tests with setprecision(16) do ... end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle AbstractIrrational conversion

2 participants