Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Quadmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ end

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)


import Base.MPFR

Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,18 @@ end
@test parse(Float128,"3.0") == Float128(3.0)
end

struct TwoPi <: AbstractIrrational
end
Base.BigFloat(::TwoPi; precision=precision(BigFloat)) =
setprecision(() -> 2 * big(π), BigFloat, precision)
Base.Float64(x::TwoPi) = Float64(big(x))

@testset "irrationals" begin
tiny = 2eps(Float128(1))
@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

end

@testset "rationals" begin
Expand Down
Loading