Skip to content
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

Fix type instability when h is a float #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
10 changes: 8 additions & 2 deletions src/Richardson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,15 @@ function extrapolate(f, h_::Number; contract::Number=oftype(float(real(h_)), 0.1
atol::Real=0, rtol::Real = atol > zero(atol) ? zero(one(float(real(x0+h_)))) : sqrt(eps(typeof(one(float(real(x0+h_)))))),
maxeval::Integer=typemax(Int), breaktol::Real=2)
if isinf(x0)
# use a change of variables x = 1/u
return extrapolate(u -> f(inv(u)), inv(h_); rtol=rtol, atol=atol, maxeval=maxeval, contract = abs(contract) > 1 ? inv(contract) : contract, x0=inv(x0), power=power)
# use a change of variables x = 1/u
contract = abs(contract) > 1 ? inv(contract) : contract
_extrapolate(u -> f(inv(u)), inv(h_), contract, inv(x0), power, atol, rtol, maxeval, breaktol)
else
_extrapolate(f, h_, contract, x0, power, atol, rtol, maxeval, breaktol)
end
end

function _extrapolate(f, h_::Number, contract, x0, power, atol, rtol, maxeval, breaktol)
(rtol ≥ 0 && atol ≥ zero(atol)) || throw(ArgumentError("rtol and atol must be nonnegative"))
breaktol > 0 || throw(ArgumentError("breaktol must be positive"))
0 < abs(contract) < 1 || throw(ArgumentError("contract must be in (0,1)"))
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,9 @@ end
@test_throws ArgumentError extrapolate!([(sin(h)/h,h) for h in [1, 0.8, 1.3]])
@test_throws ArgumentError extrapolate!([(sin(h)/h,h) for h in [1, 0.8, 0.8]])
end


@testset "type stability" begin
@inferred extrapolate(x -> sin(x)/x, 1)
@inferred extrapolate(x -> sin(x)/x, 1.0)
end
Loading