1
0
mirror of https://github.com/golang/go synced 2024-11-26 08:27:56 -07:00
go/usr/austin/eval/util.go
Austin Clements da4a22993e Fix declared and not used errors and unused import errors in
the interpreter and update code to use ast.BasicDecl and
multi-type switch.  There are still a lot of "switch _ :=
x.(type)" that should make use of the new type switch syntax,
but those will be a different CL.

R=rsc
APPROVED=rsc
DELTA=58  (16 added, 23 deleted, 19 changed)
OCL=34853
CL=34963
2009-09-24 08:25:14 -07:00

40 lines
811 B
Go

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package eval
import (
"bignum";
)
// TODO(austin): Maybe add to bignum in more general form
func ratToString(rat *bignum.Rational) string {
n, dnat := rat.Value();
d := bignum.MakeInt(false, dnat);
w, frac := n.QuoRem(d);
out := w.String();
if frac.IsZero() {
return out;
}
r := frac.Abs();
r = r.Mul(bignum.Nat(1e6));
dec, tail := r.DivMod(dnat);
// Round last digit
if tail.Cmp(dnat.Div(bignum.Nat(2))) >= 0 {
dec = dec.Add(bignum.Nat(1));
}
// Strip zeros
ten := bignum.Nat(10);
for !dec.IsZero() {
dec2, r2 := dec.DivMod(ten);
if !r2.IsZero() {
break;
}
dec = dec2;
}
out += "." + dec.String();
return out;
}