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 subnormal sqrt and cbrt #167

Merged
merged 4 commits into from
Mar 19, 2024
Merged

fix subnormal sqrt and cbrt #167

merged 4 commits into from
Mar 19, 2024

Conversation

t-bltg
Copy link
Contributor

@t-bltg t-bltg commented Mar 8, 2023

Fix #166 (tests added).

Also remove .DS_Store files and add them to .gitignore.

  • benchmark performance

PR

julia> using BenchmarkTools, DoubleFloats
julia> x = Double64(1e-10);
julia> @btime sqrt($x)
  230.792 ns (0 allocations: 0 bytes)

master

julia> using BenchmarkTools, DoubleFloats
julia> x = Double64(1e-10)
julia> @btime sqrt($x)
  215.868 ns (0 allocations: 0 bytes)

@codecov
Copy link

codecov bot commented Mar 8, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.07 🎉

Comparison is base (ef689cc) 49.12% compared to head (5b99418) 49.19%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #167      +/-   ##
==========================================
+ Coverage   49.12%   49.19%   +0.07%     
==========================================
  Files          63       63              
  Lines        3422     3425       +3     
==========================================
+ Hits         1681     1685       +4     
+ Misses       1741     1740       -1     
Impacted Files Coverage Δ
src/math/ops/op_dd_dd.jl 91.01% <100.00%> (+0.31%) ⬆️
src/type/predicates.jl 65.51% <0.00%> (+3.44%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@t-bltg t-bltg marked this pull request as draft March 8, 2023 12:30
@t-bltg t-bltg marked this pull request as ready for review March 8, 2023 12:33
@JeffreySarnoff
Copy link
Member

for sqrt using

HI(x) < 5.562684646268013e-309 && return DoubleFloat{T}(sqrt(HI(x)), zero(T))

for cbrt using

HI(x) < 7.458340731200208e-155 && return DoubleFloat{T}(cbrt(HI(x)), zero(T))

will be faster (and shrinks the special case space).

@t-bltg
Copy link
Contributor Author

t-bltg commented Mar 8, 2023

How are these subnormal numbers determined ?

@JeffreySarnoff
Copy link
Member

JeffreySarnoff commented Mar 8, 2023

By hand. For example, at the end of the cbrt search

julia> x=prevfloat(y,3_545_284); cbrt(Double64(x))
NaN

julia> x=prevfloat(y,3_545_283); cbrt(Double64(x))
4.209340649576657e-52

julia> x
7.458340731200208e-155

For cbrt, the value is not subnormal.

@t-bltg
Copy link
Contributor Author

t-bltg commented Mar 8, 2023

I see, thanks.
Are we sure that we want to hardcode these numbers in the algorithm itself ?

Especially since these numbers are determined from the output of algorithm itself, any change to the aforementioned algorithm is likely to change these values and thus breaking the code again: I find it error prone and brittle.

@JeffreySarnoff
Copy link
Member

JeffreySarnoff commented Mar 8, 2023 via email

@t-bltg
Copy link
Contributor Author

t-bltg commented Mar 8, 2023

I've pushed an alternative in 5b99418, although I must admit that I find it less readable.

@t-bltg
Copy link
Contributor Author

t-bltg commented Mar 8, 2023

By hand. For example, at the end of the cbrt search

What is y in your example ?

@JeffreySarnoff
Copy link
Member

y is an approximation to the final value x, 3.351952e153 was found by informed search.

julia> yy = (floatmin(Float64) * 3.351952e153)
7.458340770170931e-155

julia> xx=prevfloat(yy, 3_545_283); cbrt(Double64(xx))
4.209340655803547e-52

@JeffreySarnoff JeffreySarnoff merged commit beb563c into JuliaMath:main Mar 19, 2024
@t-bltg t-bltg deleted the rounding branch March 19, 2024 22:35
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.

invalid sqrt and cbrt result on subnormal numbers
2 participants