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

Evaluating does not work as expected if the resulting is floating-point and given parameters are un/signed-integers #270

Closed
Dentrax opened this issue Nov 4, 2022 · 2 comments · Fixed by #272

Comments

@Dentrax
Copy link
Contributor

Dentrax commented Nov 4, 2022

Hey there, I was playing with this awesome expression library awhile! But I hit a bug that ended-up do me to debugging my app for a few hours. And noticed it was due to my expression which blocking my progress on my project currently. I also search for whether this is a known issue or not, but couldn't find much. Here is a minimal reproducible snippet:

package main

import (
	"fmt"
	"github.com/antonmedv/expr"
)

func main() {
	env := map[string]uint64{
		"foo":  3,
		"bar":  5,
	}

	code := `(foo / bar) < 0.5`

	program, err := expr.Compile(code, expr.Env(env))
	if err != nil {
		panic(err)
	}

	output, err := expr.Run(program, env)
	if err != nil {
		panic(err)
	}

	fmt.Println(output)
}

Bug

Given foo is 3 and bar is 5, resulting 0 if we evaluate with (foo / bar) expression. But actually it should return 0.6 instead. So eventually my (foo / bar) < 0.5 expression also return false instead of true. It's evaluating as true because of 0 < 0.5 is "true".

I'm not so sure if it's correct by design, since int/int resulting int in Go. So that expr resulting 0 as expected. But from the end-user perspective, I was expecting it should have floating-point value. (at least this is what most modern calculators' do, given 3/5 is just 0.6.)

  • Tested with the following primitive types: byte, int8, int16, int32, int64, uint8, uint16, uint32, uint64
  • Works only if all the given parameters are: float32 and float64
@antonmedv
Copy link
Member

This is nice point. I think we can change resulting type for / op to float64.

@Dentrax
Copy link
Contributor Author

Dentrax commented Nov 4, 2022

This is nice point. I think we can change resulting type for / op to float64.

Cool! What about other operators? I think all operators should result float64. 🤔 (i.e. 1.5 + 9.3333, 9 - 8.9, 0.0003 * 2.33, didn't test, just for an example.)

Dentrax added a commit to Dentrax/expr that referenced this issue Nov 4, 2022
Dentrax added a commit to Dentrax/expr that referenced this issue Nov 4, 2022
Dentrax added a commit to Dentrax/expr that referenced this issue Nov 4, 2022
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