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

Error when calculating stats.rms on integers (math library) #23245

Closed
rodabt opened this issue Dec 23, 2024 · 4 comments
Closed

Error when calculating stats.rms on integers (math library) #23245

rodabt opened this issue Dec 23, 2024 · 4 comments
Assignees
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@rodabt
Copy link
Contributor

rodabt commented Dec 23, 2024

Describe the bug

When passing an array of ints to stats.rms it returns an error

Reproduction Steps

Start REPL:

import math.stats
>>> x := [1,3,5,7]
>>> stats.rms(x)

Expected Behavior

>>> stats.rms(x)
9.16515138991168

Current Behavior

vlib/math/stats/stats.v:111:15: error: cannot assign to `sum`: expected `int`, not `f64`
  109 |     mut sum := T(0)
  110 |     for v in data {
  111 |         sum += math.pow(v, 2)
      |                     ~~~~~~~~~
  112 |     }
  113 |     return math.sqrt(sum / T(data.len))

Possible Solution

pub fn rms[T](data []T) T {
	if data.len == 0 {
		return T(0)
	}
	mut sum := T(0)
	for v in data {
		sum += T(math.pow(v, 2))
	}
	return T(math.sqrt(sum / T(data.len)))
}

Additional Information/Context

No response

V version

V 0.4.9 98f13ef

Environment details (OS name and version, etc.)

V full version: V 0.4.9 3939737.98f13ef
OS: linux, Debian GNU/Linux 11 (bullseye) (WSL 2)
Processor: 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz

getwd: /tmp
vexe: /home/rabt/devel/new_v/v
vexe mtime: 2024-12-23 04:07:42

vroot: OK, value: /home/rabt/devel/new_v
VMODULES: OK, value: /home/rabt/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.30.2
Git vroot status: weekly.2024.50-100-g98f13ef4
.git/config present: true

CC version: cc (Debian 10.2.1-6) 10.2.1 20210110
emcc version: N/A
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21679

@rodabt rodabt added the Bug This tag is applied to issues which reports bugs. label Dec 23, 2024
@kbkpbot kbkpbot self-assigned this Dec 23, 2024
@kbkpbot
Copy link
Contributor

kbkpbot commented Dec 23, 2024

What do you think about mean of [1,2] should return 1.5 or 1?

@rodabt
Copy link
Contributor Author

rodabt commented Dec 23, 2024

What do you think about mean of [1,2] should return 1.5 or 1?

Mathematically speaking 1.5 should be the answer, regardless of the original data types

@jorgeluismireles
Copy link

What do you think about mean of [1,2] should return 1.5 or 1?

Is not mean but root mean square aprox 1.581138... but if you set integers you'll get an integer as response (I assume).

@rodabt
Copy link
Contributor Author

rodabt commented Dec 23, 2024

What do you think about mean of [1,2] should return 1.5 or 1?

Is not mean but root mean square aprox 1.581138... but if you set integers you'll get an integer as response (I assume).

I read the question about the mean :) Anyway, I would let the user decide to round or trim to an int if needed, otherwise it's not very meaningful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

3 participants