this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

pkg/math: add tests which show the API constants

consts.txtar shows the constant values exposed via the CUE std API,
and Example_constants shows the same ones exposed via the Go API.
Note that we're not particular about the precision used by the Go test;
we mainly aim to see whether the values look roughly correct or not.

This is in preparation for https://cuelang.org/cl/557322,
which will fix one of the constants to not truncate to an integer.

Note that Log2E and Log10E are truncated to the integers 1 and 0,
a bug which will be fixed in the next commit.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ie737f7a7ff7a609cdb9fc74df64bddc2e423f00f
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/557321
Reviewed-by: Paul Jolly <paul@myitcv.io>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+68 -6
+35
pkg/math/math_test.go
··· 15 15 package math_test 16 16 17 17 import ( 18 + "fmt" 18 19 "testing" 19 20 20 21 "cuelang.org/go/pkg/internal/builtintest" 22 + "cuelang.org/go/pkg/math" 21 23 ) 22 24 23 25 func TestBuiltin(t *testing.T) { 24 26 builtintest.Run("math", t) 25 27 } 28 + 29 + func Example_constants() { 30 + show := func(name string, value any) { 31 + fmt.Printf("% 7s: %v\n", name, value) 32 + } 33 + 34 + show("E", math.E) 35 + show("Pi", math.Pi) 36 + show("Phi", math.Phi) 37 + 38 + show("Sqrt2", math.Sqrt2) 39 + show("SqrtE", math.SqrtE) 40 + show("SqrtPi", math.SqrtPi) 41 + show("SqrtPhi", math.SqrtPhi) 42 + 43 + show("Ln2", math.Ln2) 44 + show("Log2E", math.Log2E) 45 + show("Ln10", math.Ln10) 46 + show("Log10E", math.Log10E) 47 + 48 + // Output: 49 + // E: 2.718281828459045 50 + // Pi: 3.141592653589793 51 + // Phi: 1.618033988749895 52 + // Sqrt2: 1.4142135623730951 53 + // SqrtE: 1.6487212707001282 54 + // SqrtPi: 1.772453850905516 55 + // SqrtPhi: 1.272019649514069 56 + // Ln2: 0.6931471805599453 57 + // Log2E: 1 58 + // Ln10: 2.302585092994046 59 + // Log10E: 0 60 + }
+29
pkg/math/testdata/consts.txtar
··· 1 + -- in.cue -- 2 + import "math" 3 + 4 + E: math.E 5 + Pi: math.Pi 6 + Phi: math.Phi 7 + 8 + Sqrt2: math.Sqrt2 9 + SqrtE: math.SqrtE 10 + SqrtPi: math.SqrtPi 11 + SqrtPhi: math.SqrtPhi 12 + 13 + Ln2: math.Ln2 14 + Log2E: math.Log2E 15 + Ln10: math.Ln10 16 + Log10E: math.Log10E 17 + -- out/math -- 18 + E: 2.71828182845904523536028747135266249775724709369995957496696763 19 + Pi: 3.14159265358979323846264338327950288419716939937510582097494459 20 + Phi: 1.61803398874989484820458683436563811772030917980576286213544861 21 + Sqrt2: 1.41421356237309504880168872420969807856967187537694807317667974 22 + SqrtE: 1.64872127070012814684865078781416357165377610071014801157507931 23 + SqrtPi: 1.77245385090551602729816748334114518279754945612238712821380779 24 + SqrtPhi: 1.27201964951406896425242246173749149171560804184009624861664038 25 + Ln2: 0.693147180559945309417232121458176568075500134360255254120680009 26 + Log2E: 1.442695040888963407359924681001892137426645954152985934135449408 27 + Ln10: 2.3025850929940456840179914546843642076011014886287729760333278 28 + Log10E: 0.43429448190325182765112891891660508229439700580366656611445378 29 +
+4 -6
pkg/math/testdata/gen.txtar
··· 3 3 -- in.cue -- 4 4 import "math" 5 5 6 - t1: math.Pi 7 6 t2: math.Floor(math.Pi) 8 7 t3: math.Pi(3) 9 8 t6: math.Jacobi(1000, 2000) ··· 26 25 -- out/math -- 27 26 Errors: 28 27 t3: cannot call non-function math.Pi (type float): 28 + ./in.cue:4:6 29 + t6: error in call to math.Jacobi: big: invalid 2nd argument to Int.Jacobi: need odd integer but got 2000: 29 30 ./in.cue:5:6 30 - t6: error in call to math.Jacobi: big: invalid 2nd argument to Int.Jacobi: need odd integer but got 2000: 31 - ./in.cue:6:6 32 31 t8: cannot use 2.0E+400 (type float) as float64 in argument 0 to math.Asin: value was rounded up: 33 - ./in.cue:8:6 34 - ./in.cue:8:16 32 + ./in.cue:7:6 33 + ./in.cue:7:16 35 34 36 35 Result: 37 - t1: 3.14159265358979323846264338327950288419716939937510582097494459 38 36 t2: 3 39 37 t3: _|_ // t3: cannot call non-function math.Pi (type float) 40 38 t6: _|_ // t6: error in call to math.Jacobi: big: invalid 2nd argument to Int.Jacobi: need odd integer but got 2000