internal/core/convert: allow results of math to be integer
Change-Id: I1f8c4c2ce597335e8f8809c38ce2be790ba7d592
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7882
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/format/printer.go b/cue/format/printer.go
index 79edc1a..b39e016 100644
--- a/cue/format/printer.go
+++ b/cue/format/printer.go
@@ -137,6 +137,11 @@
if p := strings.IndexByte(data, '.'); p >= 0 && data[p+1] > '9' {
data = data[:p+1] + "0" + data[p+1:]
}
+ // Lowercase E, but only if it is not the last character: in the
+ // future we may use E for Exa.
+ if p := strings.IndexByte(data, 'E'); p != -1 && p < len(data)-1 {
+ data = strings.ToLower(data)
+ }
case token.FLOAT:
// Pad leading or trailing dots.
diff --git a/internal/core/convert/go.go b/internal/core/convert/go.go
index c31a469..e585ad9 100644
--- a/internal/core/convert/go.go
+++ b/internal/core/convert/go.go
@@ -284,10 +284,13 @@
// a float or an int after all.
// The code to autodetect whether something is an integer can be done
// with this:
- // var d apd.Decimal
- // res, _ := apd.BaseContext.RoundToIntegralExact(&d, v)
- // integer := !res.Inexact()
- n := &adt.Num{Src: ctx.Source(), K: adt.FloatKind}
+ kind := adt.FloatKind
+ var d apd.Decimal
+ res, _ := apd.BaseContext.RoundToIntegralExact(&d, v)
+ if !res.Inexact() {
+ kind = adt.IntKind
+ }
+ n := &adt.Num{Src: ctx.Source(), K: kind}
n.X = *v
return n