mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
transformer: fix precalculations of simple expressions involving floating literals, with -prod (fix VSL tests with -prod)
This commit is contained in:
parent
d857e97d8c
commit
3d2e251bf2
@ -0,0 +1,30 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
|
struct Sample {
|
||||||
|
value f64
|
||||||
|
literal f64
|
||||||
|
}
|
||||||
|
|
||||||
|
fn s(literal f64, value f64) Sample {
|
||||||
|
return Sample{value, literal}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
mut samples := [
|
||||||
|
s(1.0000000000000000, 1_000_000.0 * 10.0 / 10_000_000.0),
|
||||||
|
s(0.3333333333333300, 1.0 / 3.0),
|
||||||
|
s(2.5000000000000000, 10 / 4.0),
|
||||||
|
s(0.6666666666666600, 2.0 / 3.0),
|
||||||
|
s(0.1000000000000000, 1 / 10.0),
|
||||||
|
s(3.1415926535897932, math.pi),
|
||||||
|
]
|
||||||
|
mut fails := 0
|
||||||
|
for sample in samples {
|
||||||
|
equal := math.abs(sample.value - sample.literal) < 0.00000000001
|
||||||
|
eprintln('> sample.value: ${sample.value:20.16f} | sample.literal: ${sample.literal:20.16f} | equal: $equal')
|
||||||
|
if !equal {
|
||||||
|
fails++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
eprintln('> FAILS: $fails .')
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
> sample.value: 1.0000000000000000 | sample.literal: 1.0000000000000000 | equal: true
|
||||||
|
> sample.value: 0.3333333333333333 | sample.literal: 0.3333333333333300 | equal: true
|
||||||
|
> sample.value: 2.5000000000000000 | sample.literal: 2.5000000000000000 | equal: true
|
||||||
|
> sample.value: 0.6666666666666666 | sample.literal: 0.6666666666666600 | equal: true
|
||||||
|
> sample.value: 0.1000000000000000 | sample.literal: 0.1000000000000000 | equal: true
|
||||||
|
> sample.value: 3.1415926535897930 | sample.literal: 3.1415926535897930 | equal: true
|
||||||
|
> FAILS: 0 .
|
@ -866,8 +866,8 @@ pub fn (mut t Transformer) infix_expr(mut node ast.InfixExpr) ast.Expr {
|
|||||||
ast.FloatLiteral {
|
ast.FloatLiteral {
|
||||||
match mut node.right {
|
match mut node.right {
|
||||||
ast.FloatLiteral {
|
ast.FloatLiteral {
|
||||||
left_val := node.left.val.f32()
|
left_val := node.left.val.f64()
|
||||||
right_val := node.right.val.f32()
|
right_val := node.right.val.f64()
|
||||||
match node.op {
|
match node.op {
|
||||||
.eq {
|
.eq {
|
||||||
return ast.BoolLiteral{
|
return ast.BoolLiteral{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user