Skip to content

Commit

Permalink
pkg/math: fix Log2E and Log10E constant expressions
Browse files Browse the repository at this point in the history
They were a division of integer constants, which got truncated,
meaning that Log2E rounded down to 1 and Log10E to 0.

To force them to be floating-point constants, like the CUE ones,
turn the dividend into a floating-point constant.

I found this problem since staticcheck correctly pointed out
a constant integer division which resulted in zero:

    math.go:156:11: the integer division '100… / 230…' results in zero (SA4025)

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I36ee54a7c8aff06368016b855536a9614dbbf229
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/557322
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
mvdan committed Aug 17, 2023
1 parent 63a4256 commit a1038f7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ const (
SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // https://oeis.org/A139339

Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 // https://oeis.org/A002162
Log2E = 1000000000000000000000000000000000000000000000000000000000000000 / 693147180559945309417232121458176568075500134360255254120680009
Log2E = 1000000000000000000000000000000000000000000000000000000000000000.0 / 693147180559945309417232121458176568075500134360255254120680009
Ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 // https://oeis.org/A002392
Log10E = 10000000000000000000000000000000000000000000000000000000000000 / 23025850929940456840179914546843642076011014886287729760333279
Log10E = 10000000000000000000000000000000000000000000000000000000000000.0 / 23025850929940456840179914546843642076011014886287729760333279
)

// Copysign returns a value with the magnitude
Expand Down
4 changes: 2 additions & 2 deletions pkg/math/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Example_constants() {
// SqrtPi: 1.772453850905516
// SqrtPhi: 1.272019649514069
// Ln2: 0.6931471805599453
// Log2E: 1
// Log2E: 1.4426950408889634
// Ln10: 2.302585092994046
// Log10E: 0
// Log10E: 0.4342944819032518
}

0 comments on commit a1038f7

Please sign in to comment.