Difference between f(1.0e-7) & f(1.0e-8)

you can also take the fact of the existence of expm1 and expanding each term:

f1(x)= 2*(1-x*exp(-x)-exp(-x))/(1-exp(-x))^2 #for comparison
function f2(x)
    denom= -expm1(-x)
    return 2/denom+ 2*x/denom- 2*x/(denom*denom)
end

Testing for equality on other number

julia> f1(4)
1.885271061051406

julia> f2(4)
1.8852710610514052

near zero:

julia> f1(1e-7)
0.9992008230547272

julia> f2(1e-7)
1.0000000335276127

julia> f2(1e-8)
1.0

julia> f1(1e-8)
0.0

to be fair it also fails, but around 1e-11

2 Likes