Skip to content

Conversation

@Ickaser
Copy link
Contributor

@Ickaser Ickaser commented Nov 25, 2025

The current version of NaNMath.jl doesn't work on Unitful quantities; e.g.

NaNMath.sqrt(-4u"m^2")

should return NaN*u"m" but currently errors. (The point of NaNMath is that it should return NaN rather than error, which is helpful when e.g. adaptive solvers get outside of feasible space.)

This was discussed on the NaNMath side at JuliaMath/NaNMath.jl#86 but that seems to have stalled out. The extension to Unitful of the sqrt and pow methods is easy enough; NaNMath has other functions (e.g. for functions like sin that take dimensionless input, or aggregators like sum) but I think those are not as necessary.

@codecov
Copy link

codecov bot commented Nov 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (master@946f7ae). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff            @@
##             master     #824   +/-   ##
=========================================
  Coverage          ?   89.53%           
=========================================
  Files             ?       22           
  Lines             ?     1701           
  Branches          ?        0           
=========================================
  Hits              ?     1523           
  Misses            ?      178           
  Partials          ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Ickaser
Copy link
Contributor Author

Ickaser commented Dec 5, 2025

@sostock, @giordano , @gustaphe : any thoughts on this?

Comment on lines +5 to +6
NaNMath.sqrt(q::Quantity) = NaNMath.sqrt(ustrip(q))*sqrt(unit(q))
NaNMath.pow(q::Quantity, r) = NaNMath.pow(ustrip(q), r)*unit(q)^r
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
NaNMath.sqrt(q::Quantity) = NaNMath.sqrt(ustrip(q))*sqrt(unit(q))
NaNMath.pow(q::Quantity, r) = NaNMath.pow(ustrip(q), r)*unit(q)^r
NaNMath.sqrt(q::Unitful.AbstractQuantity) = NaNMath.sqrt(ustrip(q))*sqrt(unit(q))
NaNMath.pow(q::Unitful.AbstractQuantity, r) = NaNMath.pow(ustrip(q), r)*unit(q)^r

@sostock
Copy link
Collaborator

sostock commented Dec 5, 2025

I wonder whether it makes sense to define these functions on dimensions and units as well, not just quantities? Of course, they are not different from the regular sqrt and ^ functions in that case, but it might help with writing generic code.

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.

2 participants