Comments (11)
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.
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.
@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.
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.
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.
Yes, type in inherited from left.
from expr.
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.
In your example number is float32
from expr.
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.
In first example expr does this
F32 == float64(12.34)
which is false.
from expr.
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)
- EOL for pmezard/go-difflib dependency HOT 2
- Logical operation against array functions not working as expected in v1.16 HOT 2
- Estimated expression cost
- Comparison with named integer type fails HOT 1
- OSS-Fuzz issue 68580
- Checker infinit looping when using patcher/value and operator overloading HOT 5
- Extra type hints for maps content? HOT 3
- Double escaping pair node key
- OSS-Fuzz issue 68797
- OSS-Fuzz issue 68824 HOT 1
- fromPairs does not work HOT 1
- Implement `int*` and `uint*` builtin functinos HOT 2
- pygments lexer support for mkdocs highlighting HOT 1
- Add uniq() builtin HOT 2
- Feature Request: Types implementing "fmt.Stringer" should be assignable/comparable to strings HOT 4
- interface conversion: interface {} is nil, not string (1:9) HOT 5
- Overflow errors when building for `386` HOT 2
- Can expr support custom function? HOT 2
- Print brackets `(` `)` from AST HOT 1
- Constant 1 overflows int64 when compiling expressions with int64 type parameters HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from expr.