.mean() on ArrayView1<u64>: panicked at 'attempt to add with overflow'

Open
#880 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
data

Research direction

Start by running the provided Rust example and inspect the ndarray implementations of mean() and sum(), since the report identifies their likely interaction. Determine how mean should behave when summing u64 values beyond that type's range, then verify that the reproduction no longer panics and still returns the expected mean.

Written by the indexing model from the issue text.

Description

enhancement

I wanted to calculate a mean over an ArrayView1<u64> with 1000 values and encounter a panic:
thread 'main' panicked at 'attempt to add with overflow', /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/core/src/ops/arith.rs:107:1

Is this intended? Or should the u64 values internally be casted to u128 when calculating the mean?
The following code reproduces the error:

use ndarray::*;

fn main() {
    let mut data_vec: Vec<u64> = Vec::new();
    for _ in 1..1000 {
        data_vec.push(132542363533234561)
    }
    let data = data_vec.as_slice();
    let view: ArrayView1<u64> = ArrayView1::from_shape(data.len(), data).unwrap();
    println!("{:?}", view.mean());
}

I think the reason for this error is that mean() uses the public sum() function, which panics if the result does not fit into the element type. However, the mean result should always fit into the element type, so mean() maybe shouldn't use the public sum() function.

Dominant language
Rust
Stars
4.3k
Forks
391
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-ndarray/ndarray

All issues in rust-ndarray/ndarray

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.