Giter Club home page Giter Club logo

Comments (11)

antonmedv avatar antonmedv commented on June 8, 2024 1

Yes! I think we can work on a proper solution for this. The solution in #610 is not correct.

What we need is to cast type to float32 only in case of lhs is float32. This should be done in patcher.

from expr.

antonmedv avatar antonmedv commented on June 8, 2024

This is how float comparison works:

package main

func main() {
	var f32 float32 = 12.34
	var f64 float64 = 12.34
	println(float64(f32) == f64) // false
}

https://go.dev/play/p/XsvrQ66pfrX

from expr.

daisy1754 avatar daisy1754 commented on June 8, 2024

@antonmedv I think example I gave is more like below. It is true in golang but false in expr

package main

func main() {
	var f32 float32 = 12.34
	println(f32 == 12.34) // true
}

https://go.dev/play/p/Z9SwZHGSi_J

from expr.

antonmedv avatar antonmedv commented on June 8, 2024

In golang f32 == 12.34, right hand side 12.34 will be interpreted as float32, i.e. float32(12.34). IN Expr 12.34 is a float64.

from expr.

daisy1754 avatar daisy1754 commented on June 8, 2024

well in golang literals are automatically casted to proper type right? in below example both float32 and float64 comparison returns true.

package main

func main() {
	var f32 float32 = 12.34
	var f64 float64 = 12.34
	println(f32 == 12.34) // true
	println(f64 == 12.34) // true
}

from expr.

antonmedv avatar antonmedv commented on June 8, 2024

Yes, type in inherited from left.

from expr.

daisy1754 avatar daisy1754 commented on June 8, 2024

and that is not the case for expr (please refer to my initial example) and that's why I opened issue. I believe Expr internally always cast number to flat64

from expr.

antonmedv avatar antonmedv commented on June 8, 2024

In your example number is float32

from expr.

daisy1754 avatar daisy1754 commented on June 8, 2024

Is this example more clear? I expect all to be true

package main

import (
	"fmt"

	"github.com/expr-lang/expr"
)

type Env struct {
	F32 float32
	F64 float64
}

var env = Env{
	F32: 12.34,
	F64: 12.34,
}

func main() {
	fmt.Printf("f32 expr: %t, go: %t\n", eval(`F32 == 12.34`), env.F32 == 12.34)
	fmt.Printf("f64 expr: %t, go: %t\n", eval(`F64 == 12.34`), env.F64 == 12.34)
}

func eval(code string) bool {
	program, err := expr.Compile(code, expr.Env(Env{}))
	if err != nil {
		panic(err)
	}
	output, err := expr.Run(program, env)
	if err != nil {
		panic(err)
	}
	return output.(bool)
}

result is

f32 expr: false, go: true
f64 expr: true, go: true

https://go.dev/play/p/GH9DSp3YFHF

from expr.

antonmedv avatar antonmedv commented on June 8, 2024

In first example expr does this

F32 == float64(12.34)

which is false.

from expr.

daisy1754 avatar daisy1754 commented on June 8, 2024

yeah I understand internal - that's why I sent out #610

Don't you think it's confusing though? As shown above

fmt.Printf("f32 expr: %t, go: %t\n", eval(F32 == 12.34), env.F32 == 12.34)

expr expression is false and golang expression is true in this case

from expr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.