From 3d2e251bf253fc54d140d3a19416383457e54caa Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 16 Oct 2022 20:23:41 +0300 Subject: [PATCH] transformer: fix precalculations of simple expressions involving floating literals, with -prod (fix VSL tests with -prod) --- ...expressions_should_not_round_as_f32.prod.v | 30 +++++++++++++++++++ ...hould_not_round_as_f32.prod.v.expected.txt | 7 +++++ vlib/v/transformer/transformer.v | 4 +-- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/prod/f64_literals_and_simple_expressions_should_not_round_as_f32.prod.v create mode 100644 vlib/v/tests/prod/f64_literals_and_simple_expressions_should_not_round_as_f32.prod.v.expected.txt diff --git a/vlib/v/tests/prod/f64_literals_and_simple_expressions_should_not_round_as_f32.prod.v b/vlib/v/tests/prod/f64_literals_and_simple_expressions_should_not_round_as_f32.prod.v new file mode 100644 index 0000000000..d185f933f9 --- /dev/null +++ b/vlib/v/tests/prod/f64_literals_and_simple_expressions_should_not_round_as_f32.prod.v @@ -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 .') +} diff --git a/vlib/v/tests/prod/f64_literals_and_simple_expressions_should_not_round_as_f32.prod.v.expected.txt b/vlib/v/tests/prod/f64_literals_and_simple_expressions_should_not_round_as_f32.prod.v.expected.txt new file mode 100644 index 0000000000..ff8570c885 --- /dev/null +++ b/vlib/v/tests/prod/f64_literals_and_simple_expressions_should_not_round_as_f32.prod.v.expected.txt @@ -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 . diff --git a/vlib/v/transformer/transformer.v b/vlib/v/transformer/transformer.v index 78b6fe9dc9..9a465473cc 100644 --- a/vlib/v/transformer/transformer.v +++ b/vlib/v/transformer/transformer.v @@ -866,8 +866,8 @@ pub fn (mut t Transformer) infix_expr(mut node ast.InfixExpr) ast.Expr { ast.FloatLiteral { match mut node.right { ast.FloatLiteral { - left_val := node.left.val.f32() - right_val := node.right.val.f32() + left_val := node.left.val.f64() + right_val := node.right.val.f64() match node.op { .eq { return ast.BoolLiteral{