Skip to content

Commit

Permalink
internal/core/convert: allow results of math to be integer
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mpvl committed Dec 4, 2020
1 parent 8b13752 commit ff3e32f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cue/format/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ func (p *printer) Print(v interface{}) {
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.
Expand Down
11 changes: 7 additions & 4 deletions internal/core/convert/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,13 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
// 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

Expand Down

0 comments on commit ff3e32f

Please sign in to comment.