Skip to content

Commit 87b92d9

Browse files
shaneHowearthanzdaddy
authored andcommitted
Clean up error messages and shadowed types/vars (#63)
Also add a comment to explain the use of a struct to define `Decimal64`.
1 parent d47b92a commit 87b92d9

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

decimal64.go

+21-20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const (
3131

3232
// Decimal64 represents an IEEE 754 64-bit floating point decimal number.
3333
// It uses the binary representation method.
34+
// Decimal64 is intentionally a struct to ensure users don't accidentally cast it to uint64
3435
type Decimal64 struct {
3536
bits uint64
3637
}
@@ -241,8 +242,8 @@ func (d Decimal64) parts() (fl flavor, sign int, exp int, significand uint64) {
241242

242243
// getParts gets the parts and returns in decParts stuct, doesn't get the magnitude due to performance issues\
243244
func (d Decimal64) getParts() decParts {
244-
flavor, sign, exp, significand := d.parts()
245-
return decParts{flavor, sign, exp, uint128T{significand, 0}, &d}
245+
flav, sign, exp, significand := d.parts()
246+
return decParts{flav, sign, exp, uint128T{significand, 0}, &d}
246247
}
247248

248249
func expWholeFrac(exp int, significand uint64) (exp2 int, whole uint64, frac uint64) {
@@ -279,8 +280,8 @@ func expWholeFrac(exp int, significand uint64) (exp2 int, whole uint64, frac uin
279280

280281
// Float64 returns a float64 representation of d.
281282
func (d Decimal64) Float64() float64 {
282-
flavor, sign, exp, significand := d.parts()
283-
switch flavor {
283+
flav, sign, exp, significand := d.parts()
284+
switch flav {
284285
case flNormal:
285286
if significand == 0 {
286287
return 0.0 * float64(1-2*sign)
@@ -301,8 +302,8 @@ func (d Decimal64) Float64() float64 {
301302

302303
// Int64 converts d to an int64.
303304
func (d Decimal64) Int64() int64 {
304-
flavor, sign, exp, significand := d.parts()
305-
switch flavor {
305+
flav, sign, exp, significand := d.parts()
306+
switch flav {
306307
case flInf:
307308
if sign == 0 {
308309
return math.MaxInt64
@@ -327,38 +328,38 @@ func (d Decimal64) Int64() int64 {
327328

328329
// IsZero returns true if the Decimal encodes a zero value.
329330
func (d Decimal64) IsZero() bool {
330-
fl, _, _, significand := d.parts()
331-
return significand == 0 && fl == flNormal
331+
flav, _, _, significand := d.parts()
332+
return significand == 0 && flav == flNormal
332333
}
333334

334335
// IsInf returns true iff d = ±∞.
335336
func (d Decimal64) IsInf() bool {
336-
flavor, _, _, _ := d.parts()
337-
return flavor == flInf
337+
flav, _, _, _ := d.parts()
338+
return flav == flInf
338339
}
339340

340341
// IsNaN returns true iff d is not a number.
341342
func (d Decimal64) IsNaN() bool {
342-
flavor, _, _, _ := d.parts()
343-
return flavor == flQNaN || flavor == flSNaN
343+
flav, _, _, _ := d.parts()
344+
return flav == flQNaN || flav == flSNaN
344345
}
345346

346347
// IsQNaN returns true iff d is a quiet NaN.
347348
func (d Decimal64) IsQNaN() bool {
348-
flavor, _, _, _ := d.parts()
349-
return flavor == flQNaN
349+
flav, _, _, _ := d.parts()
350+
return flav == flQNaN
350351
}
351352

352353
// IsSNaN returns true iff d is a signalling NaN.
353354
func (d Decimal64) IsSNaN() bool {
354-
flavor, _, _, _ := d.parts()
355-
return flavor == flSNaN
355+
flav, _, _, _ := d.parts()
356+
return flav == flSNaN
356357
}
357358

358359
// IsInt returns true iff d is an integer.
359360
func (d Decimal64) IsInt() bool {
360-
fl, _, exp, significand := d.parts()
361-
switch fl {
361+
flav, _, exp, significand := d.parts()
362+
switch flav {
362363
case flNormal:
363364
_, _, frac := expWholeFrac(exp, significand)
364365
return frac == 0
@@ -369,8 +370,8 @@ func (d Decimal64) IsInt() bool {
369370

370371
// IsSubnormal returns true iff d is a subnormal.
371372
func (d Decimal64) IsSubnormal() bool {
372-
fl, _, _, significand := d.parts()
373-
return significand != 0 && significand < decimal64Base && fl == flNormal
373+
flav, _, _, significand := d.parts()
374+
return significand != 0 && significand < decimal64Base && flav == flNormal
374375
}
375376

376377
// Sign returns -1/0/1 depending on whether d is </=/> 0.

decimal64fmt.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ func appendUint64(buf []byte, n, limit uint64) []byte {
3030

3131
// Append appends the text representation of d to buf.
3232
func (d Decimal64) Append(buf []byte, format byte, prec int) []byte {
33-
flavor, sign, exp, significand := d.parts()
33+
flav, sign, exp, significand := d.parts()
3434
if sign == 1 {
3535
buf = append(buf, '-')
3636
}
37-
switch flavor {
37+
switch flav {
3838
case flQNaN, flSNaN:
3939
return appendUint64(append(buf, []byte("NaN")...), significand, 10000)
4040
case flInf:
@@ -64,10 +64,10 @@ formatBlock:
6464
}
6565
return buf
6666
case 'f', 'F':
67-
exp, whole, frac := expWholeFrac(exp, significand)
67+
exponent, whole, frac := expWholeFrac(exp, significand)
6868
if whole > 0 {
6969
buf = appendUint64(buf, whole, decimal64Base)
70-
for ; exp > 0; exp-- {
70+
for ; exponent > 0; exponent-- {
7171
buf = append(buf, '0')
7272
}
7373
} else {

decimal64math.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ func (ctx Context64) Quo(d, e Decimal64) Decimal64 {
129129

130130
// Sqrt computes √d.
131131
func (d Decimal64) Sqrt() Decimal64 {
132-
flavor, sign, exp, significand := d.parts()
133-
switch flavor {
132+
flav, sign, exp, significand := d.parts()
133+
switch flav {
134134
case flInf:
135135
if sign == 1 {
136136
return QNaN64

decimal64scan.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (d *Decimal64) Scan(state fmt.ScanState, verb rune) error {
7979
return err
8080
}
8181
if len(dot) > 1 {
82-
return fmt.Errorf("Too many dots")
82+
return fmt.Errorf("too many dots")
8383
}
8484

8585
frac, err := tokenString(state, unicode.IsDigit)
@@ -92,7 +92,7 @@ func (d *Decimal64) Scan(state fmt.ScanState, verb rune) error {
9292
return err
9393
}
9494
if len(e) > 1 {
95-
return fmt.Errorf("Too many 'e's")
95+
return fmt.Errorf("too many 'e's")
9696
}
9797

9898
var expSign int
@@ -107,13 +107,13 @@ func (d *Decimal64) Scan(state fmt.ScanState, verb rune) error {
107107
return err
108108
}
109109
if exp == "" {
110-
return fmt.Errorf("Exponent value missing")
110+
return fmt.Errorf("exponent value missing")
111111
}
112112
}
113113

114114
mantissa := whole + frac
115115
if mantissa == "" {
116-
return fmt.Errorf("Mantissa missing")
116+
return fmt.Errorf("mantissa missing")
117117
}
118118
mantissa = strings.TrimLeft(mantissa, "0")
119119
if mantissa == "" {
@@ -143,7 +143,7 @@ func (d *Decimal64) Scan(state fmt.ScanState, verb rune) error {
143143
}
144144

145145
func notDecimal64() error {
146-
return fmt.Errorf("Not a valid Decimal64")
146+
return fmt.Errorf("not a valid Decimal64")
147147
}
148148

149149
func parseUint(s string) (int64, int) {
@@ -180,7 +180,7 @@ func scanSign(state fmt.ScanState) (int, error) {
180180
return 1, nil
181181
}
182182
default:
183-
return 0, fmt.Errorf("Too many +/- characters: %s", string(s))
183+
return 0, fmt.Errorf("too many +/- characters: %s", string(s))
184184
}
185185
return 0, nil
186186
}

0 commit comments

Comments
 (0)