Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use zero() for type-stable calculation of _momentN() #969

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

Draft
abhro wants to merge 3 commits into JuliaStats:master
base: master
Choose a base branch
Loading
from abhro:moment-types

Conversation

@abhro
Copy link
Contributor

@abhro abhro commented Oct 21, 2025
edited
Loading

Solves #968

src/moments.jl Outdated
function _moment2(v::AbstractArray{<:Real}, m::Real; corrected=false)
n = length(v)
s = 0.0
s = zero(m * m)
Copy link
Member

@devmotion devmotion Oct 21, 2025

Choose a reason for hiding this comment

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

In general, s does not have the correct/desired type here as this initialization purely depends on m and neglects v.

Copy link
Contributor Author

@abhro abhro Oct 21, 2025
edited
Loading

Choose a reason for hiding this comment

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

I was making the assumption that m and v would have compatible/identical types, since m is calculated from v and the function is only called internally. Would an implementation involving v be something like zero(v[begin] * v[begin])?

Copy link
Member

@devmotion devmotion Oct 21, 2025

Choose a reason for hiding this comment

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

IMO we shouldn't make that assumption. For instance, in var(...; mean=...) users can easily provide a mean that is of different type.

Would an implementation involving v be something like zero(v[begin] * v[begin])

No, that would error if the array is empty and would neglect the type of m.

abhro reacted with thumbs up emoji
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@devmotion devmotion devmotion left review comments

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

AltStyle によって変換されたページ (->オリジナル) /