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

max/min logic is incorrect #71

Closed
zev-zakaryan opened this issue Oct 14, 2023 · 2 comments · Fixed by #72
Closed

max/min logic is incorrect #71

zev-zakaryan opened this issue Oct 14, 2023 · 2 comments · Fixed by #72

Comments

@zev-zakaryan
Copy link

zev-zakaryan commented Oct 14, 2023

Max starts with "SmallestNonzeroFloat64" which is the smallest positive, non-zero value. So max:[0,0], max:[-1,0], max:[-3,-2] and etc. will all get incorrect answer. Also, min is correctly start with maximum value but min:[] return very large number make no sense. It should return null or error *spec in jsonlogic website return null for empty or if it contains not-numeric-castable.
So I think you should start with first found number. Then compare following number with the first one, not some made-up value. Return null for not-numeric-castable/empty input if want to follow jsonlogic.

@diegoholiveira
Copy link
Owner

Makes sense. Do you want to provide a PR fixing it?

@zev-zakaryan
Copy link
Author

zev-zakaryan commented Oct 15, 2023

I can provide my current implemented code, though it's not direct PR as I use my "conv.To".
conv.To[float64] is kind of this lib toNumber with return error.

`
func jlMax(values, _ any) any {

v := values.([]any)
if len(v) < 1 {
	return nil
}
out, err := conv.To[float64](v[0])
if err != nil {
	return nil
}
for i := 1; i < len(v); i++ {
	if f, err := conv.To[float64](v[i]); err != nil {
		return nil
	} else if f > out { //Min is < here
		out = f
	}
}
return out

}
`

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 a pull request may close this issue.

2 participants