Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
8a7cbadb
Commit
8a7cbadb
authored
Jan 15, 2009
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
convert strconv
R=r DELTA=568 (0 added, 9 deleted, 559 changed) OCL=22898 CL=22901
parent
74a60ed0
Changes
22
Hide whitespace changes
Inline
Side-by-side
Showing
22 changed files
with
552 additions
and
561 deletions
+552
-561
src/lib/fmt/format.go
src/lib/fmt/format.go
+8
-8
src/lib/http/server.go
src/lib/http/server.go
+1
-1
src/lib/json/generic.go
src/lib/json/generic.go
+1
-1
src/lib/json/parse.go
src/lib/json/parse.go
+3
-3
src/lib/net/net.go
src/lib/net/net.go
+1
-1
src/lib/reflect/tostring.go
src/lib/reflect/tostring.go
+6
-6
src/lib/strconv/atof.go
src/lib/strconv/atof.go
+28
-28
src/lib/strconv/atof_test.go
src/lib/strconv/atof_test.go
+67
-67
src/lib/strconv/atoi.go
src/lib/strconv/atoi.go
+9
-9
src/lib/strconv/atoi_test.go
src/lib/strconv/atoi_test.go
+79
-87
src/lib/strconv/decimal.go
src/lib/strconv/decimal.go
+86
-86
src/lib/strconv/decimal_test.go
src/lib/strconv/decimal_test.go
+42
-42
src/lib/strconv/fp_test.go
src/lib/strconv/fp_test.go
+10
-10
src/lib/strconv/ftoa.go
src/lib/strconv/ftoa.go
+34
-34
src/lib/strconv/ftoa_test.go
src/lib/strconv/ftoa_test.go
+78
-78
src/lib/strconv/itoa.go
src/lib/strconv/itoa.go
+3
-4
src/lib/strconv/itoa_test.go
src/lib/strconv/itoa_test.go
+34
-34
src/lib/strconv/quote_test.go
src/lib/strconv/quote_test.go
+48
-48
test/chan/goroutines.go
test/chan/goroutines.go
+1
-1
test/fixedbugs/bug120.go
test/fixedbugs/bug120.go
+5
-5
test/mallocrep1.go
test/mallocrep1.go
+1
-1
test/map.go
test/map.go
+7
-7
No files found.
src/lib/fmt/format.go
View file @
8a7cbadb
...
@@ -428,38 +428,38 @@ func fmtString(f *Fmt, s string) *Fmt {
...
@@ -428,38 +428,38 @@ func fmtString(f *Fmt, s string) *Fmt {
// float64
// float64
func
(
f
*
Fmt
)
Fmt_e64
(
a
float64
)
*
Fmt
{
func
(
f
*
Fmt
)
Fmt_e64
(
a
float64
)
*
Fmt
{
return
fmtString
(
f
,
strconv
.
f
toa64
(
a
,
'e'
,
Prec
(
f
,
6
)));
return
fmtString
(
f
,
strconv
.
F
toa64
(
a
,
'e'
,
Prec
(
f
,
6
)));
}
}
func
(
f
*
Fmt
)
Fmt_f64
(
a
float64
)
*
Fmt
{
func
(
f
*
Fmt
)
Fmt_f64
(
a
float64
)
*
Fmt
{
return
fmtString
(
f
,
strconv
.
f
toa64
(
a
,
'f'
,
Prec
(
f
,
6
)));
return
fmtString
(
f
,
strconv
.
F
toa64
(
a
,
'f'
,
Prec
(
f
,
6
)));
}
}
func
(
f
*
Fmt
)
Fmt_g64
(
a
float64
)
*
Fmt
{
func
(
f
*
Fmt
)
Fmt_g64
(
a
float64
)
*
Fmt
{
return
fmtString
(
f
,
strconv
.
f
toa64
(
a
,
'g'
,
Prec
(
f
,
-
1
)));
return
fmtString
(
f
,
strconv
.
F
toa64
(
a
,
'g'
,
Prec
(
f
,
-
1
)));
}
}
func
(
f
*
Fmt
)
Fmt_fb64
(
a
float64
)
*
Fmt
{
func
(
f
*
Fmt
)
Fmt_fb64
(
a
float64
)
*
Fmt
{
return
fmtString
(
f
,
strconv
.
f
toa64
(
a
,
'b'
,
0
));
return
fmtString
(
f
,
strconv
.
F
toa64
(
a
,
'b'
,
0
));
}
}
// float32
// float32
// cannot defer to float64 versions
// cannot defer to float64 versions
// because it will get rounding wrong in corner cases.
// because it will get rounding wrong in corner cases.
func
(
f
*
Fmt
)
Fmt_e32
(
a
float32
)
*
Fmt
{
func
(
f
*
Fmt
)
Fmt_e32
(
a
float32
)
*
Fmt
{
return
fmtString
(
f
,
strconv
.
f
toa32
(
a
,
'e'
,
Prec
(
f
,
6
)));
return
fmtString
(
f
,
strconv
.
F
toa32
(
a
,
'e'
,
Prec
(
f
,
6
)));
}
}
func
(
f
*
Fmt
)
Fmt_f32
(
a
float32
)
*
Fmt
{
func
(
f
*
Fmt
)
Fmt_f32
(
a
float32
)
*
Fmt
{
return
fmtString
(
f
,
strconv
.
f
toa32
(
a
,
'f'
,
Prec
(
f
,
6
)));
return
fmtString
(
f
,
strconv
.
F
toa32
(
a
,
'f'
,
Prec
(
f
,
6
)));
}
}
func
(
f
*
Fmt
)
Fmt_g32
(
a
float32
)
*
Fmt
{
func
(
f
*
Fmt
)
Fmt_g32
(
a
float32
)
*
Fmt
{
return
fmtString
(
f
,
strconv
.
f
toa32
(
a
,
'g'
,
Prec
(
f
,
-
1
)));
return
fmtString
(
f
,
strconv
.
F
toa32
(
a
,
'g'
,
Prec
(
f
,
-
1
)));
}
}
func
(
f
*
Fmt
)
Fmt_fb32
(
a
float32
)
*
Fmt
{
func
(
f
*
Fmt
)
Fmt_fb32
(
a
float32
)
*
Fmt
{
return
fmtString
(
f
,
strconv
.
f
toa32
(
a
,
'b'
,
0
));
return
fmtString
(
f
,
strconv
.
F
toa32
(
a
,
'b'
,
0
));
}
}
// float
// float
...
...
src/lib/http/server.go
View file @
8a7cbadb
...
@@ -39,7 +39,7 @@ func ServeConnection(fd net.Conn, raddr string, f *(*Conn, *Request)) {
...
@@ -39,7 +39,7 @@ func ServeConnection(fd net.Conn, raddr string, f *(*Conn, *Request)) {
export
func
Serve
(
l
net
.
Listener
,
f
*
(
*
Conn
,
*
Request
))
*
os
.
Error
{
export
func
Serve
(
l
net
.
Listener
,
f
*
(
*
Conn
,
*
Request
))
*
os
.
Error
{
// TODO: Make this unnecessary
// TODO: Make this unnecessary
s
,
e
:=
os
.
Getenv
(
"GOMAXPROCS"
);
s
,
e
:=
os
.
Getenv
(
"GOMAXPROCS"
);
if
n
,
ok
:=
strconv
.
a
toi
(
s
);
n
<
3
{
if
n
,
ok
:=
strconv
.
A
toi
(
s
);
n
<
3
{
print
(
"Warning: $GOMAXPROCS needs to be at least 3.
\n
"
);
print
(
"Warning: $GOMAXPROCS needs to be at least 3.
\n
"
);
}
}
...
...
src/lib/json/generic.go
View file @
8a7cbadb
...
@@ -140,7 +140,7 @@ export func Walk(j Json, path string) Json {
...
@@ -140,7 +140,7 @@ export func Walk(j Json, path string) Json {
}
}
switch
j
.
Kind
()
{
switch
j
.
Kind
()
{
case
ArrayKind
:
case
ArrayKind
:
indx
,
err
:=
strconv
.
a
toi
(
elem
);
indx
,
err
:=
strconv
.
A
toi
(
elem
);
if
err
!=
nil
{
if
err
!=
nil
{
return
null
return
null
}
}
...
...
src/lib/json/parse.go
View file @
8a7cbadb
...
@@ -296,16 +296,16 @@ Switch:
...
@@ -296,16 +296,16 @@ Switch:
break
;
break
;
case
'1'
:
case
'1'
:
// If the number is exactly an integer, use that.
// If the number is exactly an integer, use that.
if
i
,
err
:=
strconv
.
a
toi64
(
lex
.
token
);
err
==
nil
{
if
i
,
err
:=
strconv
.
A
toi64
(
lex
.
token
);
err
==
nil
{
build
.
Int64
(
i
);
build
.
Int64
(
i
);
ok
=
true
;
ok
=
true
;
}
}
else
if
i
,
err
:=
strconv
.
a
toui64
(
lex
.
token
);
err
==
nil
{
else
if
i
,
err
:=
strconv
.
A
toui64
(
lex
.
token
);
err
==
nil
{
build
.
Uint64
(
i
);
build
.
Uint64
(
i
);
ok
=
true
;
ok
=
true
;
}
}
// Fall back to floating point.
// Fall back to floating point.
else
if
f
,
err
:=
strconv
.
a
tof64
(
lex
.
token
);
err
==
nil
{
else
if
f
,
err
:=
strconv
.
A
tof64
(
lex
.
token
);
err
==
nil
{
build
.
Float64
(
f
);
build
.
Float64
(
f
);
ok
=
true
;
ok
=
true
;
}
}
...
...
src/lib/net/net.go
View file @
8a7cbadb
...
@@ -124,7 +124,7 @@ func SockaddrToHostPort(sa *syscall.Sockaddr) (hostport string, err *os.Error) {
...
@@ -124,7 +124,7 @@ func SockaddrToHostPort(sa *syscall.Sockaddr) (hostport string, err *os.Error) {
return
""
,
e
return
""
,
e
}
}
host
:=
IPToString
(
addr
);
host
:=
IPToString
(
addr
);
return
JoinHostPort
(
host
,
strconv
.
i
toa
(
port
)),
nil
;
return
JoinHostPort
(
host
,
strconv
.
I
toa
(
port
)),
nil
;
default
:
default
:
return
""
,
UnknownSocketFamily
return
""
,
UnknownSocketFamily
}
}
...
...
src/lib/reflect/tostring.go
View file @
8a7cbadb
...
@@ -84,7 +84,7 @@ func TypeToString(typ Type, expand bool) string {
...
@@ -84,7 +84,7 @@ func TypeToString(typ Type, expand bool) string {
if
a
.
Open
()
{
if
a
.
Open
()
{
str
=
"[]"
str
=
"[]"
}
else
{
}
else
{
str
=
"["
+
strconv
.
i
toa64
(
int64
(
a
.
Len
()))
+
"]"
str
=
"["
+
strconv
.
I
toa64
(
int64
(
a
.
Len
()))
+
"]"
}
}
return
str
+
TypeToString
(
a
.
Elem
(),
false
);
return
str
+
TypeToString
(
a
.
Elem
(),
false
);
case
MapKind
:
case
MapKind
:
...
@@ -123,7 +123,7 @@ func TypeToString(typ Type, expand bool) string {
...
@@ -123,7 +123,7 @@ func TypeToString(typ Type, expand bool) string {
// TODO: want an unsigned one too
// TODO: want an unsigned one too
func
integer
(
v
int64
)
string
{
func
integer
(
v
int64
)
string
{
return
strconv
.
i
toa64
(
v
);
return
strconv
.
I
toa64
(
v
);
}
}
func
ValueToString
(
val
Value
)
string
{
func
ValueToString
(
val
Value
)
string
{
...
@@ -154,14 +154,14 @@ func ValueToString(val Value) string {
...
@@ -154,14 +154,14 @@ func ValueToString(val Value) string {
return
integer
(
int64
(
val
.
(
Uint64Value
)
.
Get
()));
return
integer
(
int64
(
val
.
(
Uint64Value
)
.
Get
()));
case
FloatKind
:
case
FloatKind
:
if
strconv
.
FloatSize
==
32
{
if
strconv
.
FloatSize
==
32
{
return
strconv
.
f
toa32
(
float32
(
val
.
(
FloatValue
)
.
Get
()),
'g'
,
-
1
);
return
strconv
.
F
toa32
(
float32
(
val
.
(
FloatValue
)
.
Get
()),
'g'
,
-
1
);
}
else
{
}
else
{
return
strconv
.
f
toa64
(
float64
(
val
.
(
FloatValue
)
.
Get
()),
'g'
,
-
1
);
return
strconv
.
F
toa64
(
float64
(
val
.
(
FloatValue
)
.
Get
()),
'g'
,
-
1
);
}
}
case
Float32Kind
:
case
Float32Kind
:
return
strconv
.
f
toa32
(
val
.
(
Float32Value
)
.
Get
(),
'g'
,
-
1
);
return
strconv
.
F
toa32
(
val
.
(
Float32Value
)
.
Get
(),
'g'
,
-
1
);
case
Float64Kind
:
case
Float64Kind
:
return
strconv
.
f
toa64
(
val
.
(
Float64Value
)
.
Get
(),
'g'
,
-
1
);
return
strconv
.
F
toa64
(
val
.
(
Float64Value
)
.
Get
(),
'g'
,
-
1
);
case
Float80Kind
:
case
Float80Kind
:
return
"float80"
;
return
"float80"
;
case
StringKind
:
case
StringKind
:
...
...
src/lib/strconv/atof.go
View file @
8a7cbadb
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// license that can be found in the LICENSE file.
//
D
ecimal to binary floating point conversion.
//
d
ecimal to binary floating point conversion.
// Algorithm:
// Algorithm:
// 1) Store input in multiprecision decimal.
// 1) Store input in multiprecision decimal.
// 2) Multiply/divide decimal by powers of two until in range [0.5, 1)
// 2) Multiply/divide decimal by powers of two until in range [0.5, 1)
...
@@ -15,10 +15,10 @@ import (
...
@@ -15,10 +15,10 @@ import (
"strconv"
;
"strconv"
;
)
)
package
var
optimize
=
true
// can change for testing
var
optimize
=
true
// can change for testing
// TODO(rsc): Better truncation handling.
// TODO(rsc): Better truncation handling.
func
StringToDecimal
(
s
string
)
(
neg
bool
,
d
*
D
ecimal
,
trunc
bool
,
ok
bool
)
{
func
stringToDecimal
(
s
string
)
(
neg
bool
,
d
*
d
ecimal
,
trunc
bool
,
ok
bool
)
{
i
:=
0
;
i
:=
0
;
// optional sign
// optional sign
...
@@ -34,7 +34,7 @@ func StringToDecimal(s string) (neg bool, d *Decimal, trunc bool, ok bool) {
...
@@ -34,7 +34,7 @@ func StringToDecimal(s string) (neg bool, d *Decimal, trunc bool, ok bool) {
}
}
// digits
// digits
b
:=
new
(
D
ecimal
);
b
:=
new
(
d
ecimal
);
sawdot
:=
false
;
sawdot
:=
false
;
sawdigits
:=
false
;
sawdigits
:=
false
;
for
;
i
<
len
(
s
);
i
++
{
for
;
i
<
len
(
s
);
i
++
{
...
@@ -104,12 +104,12 @@ func StringToDecimal(s string) (neg bool, d *Decimal, trunc bool, ok bool) {
...
@@ -104,12 +104,12 @@ func StringToDecimal(s string) (neg bool, d *Decimal, trunc bool, ok bool) {
return
;
return
;
}
}
//
D
ecimal power of ten to binary power of two.
//
d
ecimal power of ten to binary power of two.
var
powtab
=
[]
int
{
var
powtab
=
[]
int
{
1
,
3
,
6
,
9
,
13
,
16
,
19
,
23
,
26
1
,
3
,
6
,
9
,
13
,
16
,
19
,
23
,
26
}
}
func
DecimalToFloatBits
(
neg
bool
,
d
*
Decimal
,
trunc
bool
,
flt
*
F
loatInfo
)
(
b
uint64
,
overflow
bool
)
{
func
decimalToFloatBits
(
neg
bool
,
d
*
decimal
,
trunc
bool
,
flt
*
f
loatInfo
)
(
b
uint64
,
overflow
bool
)
{
var
exp
int
;
var
exp
int
;
var
mant
uint64
;
var
mant
uint64
;
...
@@ -208,7 +208,7 @@ out:
...
@@ -208,7 +208,7 @@ out:
// Compute exact floating-point integer from d's digits.
// Compute exact floating-point integer from d's digits.
// Caller is responsible for avoiding overflow.
// Caller is responsible for avoiding overflow.
func
DecimalToFloat64Int
(
neg
bool
,
d
*
D
ecimal
)
float64
{
func
decimalAtof64Int
(
neg
bool
,
d
*
d
ecimal
)
float64
{
f
:=
float64
(
0
);
f
:=
float64
(
0
);
for
i
:=
0
;
i
<
d
.
nd
;
i
++
{
for
i
:=
0
;
i
<
d
.
nd
;
i
++
{
f
=
f
*
10
+
float64
(
d
.
d
[
i
]
-
'0'
);
f
=
f
*
10
+
float64
(
d
.
d
[
i
]
-
'0'
);
...
@@ -219,7 +219,7 @@ func DecimalToFloat64Int(neg bool, d *Decimal) float64 {
...
@@ -219,7 +219,7 @@ func DecimalToFloat64Int(neg bool, d *Decimal) float64 {
return
f
;
return
f
;
}
}
func
DecimalToFloat32Int
(
neg
bool
,
d
*
D
ecimal
)
float32
{
func
decimalAtof32Int
(
neg
bool
,
d
*
d
ecimal
)
float32
{
f
:=
float32
(
0
);
f
:=
float32
(
0
);
for
i
:=
0
;
i
<
d
.
nd
;
i
++
{
for
i
:=
0
;
i
<
d
.
nd
;
i
++
{
f
=
f
*
10
+
float32
(
d
.
d
[
i
]
-
'0'
);
f
=
f
*
10
+
float32
(
d
.
d
[
i
]
-
'0'
);
...
@@ -241,13 +241,13 @@ var float32pow10 = []float32 {
...
@@ -241,13 +241,13 @@ var float32pow10 = []float32 {
}
}
// If possible to convert decimal d to 64-bit float f exactly,
// If possible to convert decimal d to 64-bit float f exactly,
// entirely in floating-point math, do so, avoiding the expense of
D
ecimalToFloatBits.
// entirely in floating-point math, do so, avoiding the expense of
d
ecimalToFloatBits.
// Three common cases:
// Three common cases:
// value is exact integer
// value is exact integer
// value is exact integer * exact power of ten
// value is exact integer * exact power of ten
// value is exact integer / exact power of ten
// value is exact integer / exact power of ten
// These all produce potentially inexact but correctly rounded answers.
// These all produce potentially inexact but correctly rounded answers.
func
DecimalToFloat64
(
neg
bool
,
d
*
D
ecimal
,
trunc
bool
)
(
f
float64
,
ok
bool
)
{
func
decimalAtof64
(
neg
bool
,
d
*
d
ecimal
,
trunc
bool
)
(
f
float64
,
ok
bool
)
{
// Exact integers are <= 10^15.
// Exact integers are <= 10^15.
// Exact powers of ten are <= 10^22.
// Exact powers of ten are <= 10^22.
if
d
.
nd
>
15
{
if
d
.
nd
>
15
{
...
@@ -255,11 +255,11 @@ func DecimalToFloat64(neg bool, d *Decimal, trunc bool) (f float64, ok bool) {
...
@@ -255,11 +255,11 @@ func DecimalToFloat64(neg bool, d *Decimal, trunc bool) (f float64, ok bool) {
}
}
switch
{
switch
{
case
d
.
dp
==
d
.
nd
:
// int
case
d
.
dp
==
d
.
nd
:
// int
f
:=
DecimalToFloat
64Int
(
neg
,
d
);
f
:=
decimalAtof
64Int
(
neg
,
d
);
return
f
,
true
;
return
f
,
true
;
case
d
.
dp
>
d
.
nd
&&
d
.
dp
<=
15
+
22
:
// int * 10^k
case
d
.
dp
>
d
.
nd
&&
d
.
dp
<=
15
+
22
:
// int * 10^k
f
:=
DecimalToFloat
64Int
(
neg
,
d
);
f
:=
decimalAtof
64Int
(
neg
,
d
);
k
:=
d
.
dp
-
d
.
nd
;
k
:=
d
.
dp
-
d
.
nd
;
// If exponent is big but number of digits is not,
// If exponent is big but number of digits is not,
// can move a few zeros into the integer part.
// can move a few zeros into the integer part.
...
@@ -270,7 +270,7 @@ func DecimalToFloat64(neg bool, d *Decimal, trunc bool) (f float64, ok bool) {
...
@@ -270,7 +270,7 @@ func DecimalToFloat64(neg bool, d *Decimal, trunc bool) (f float64, ok bool) {
return
f
*
float64pow10
[
k
],
true
;
return
f
*
float64pow10
[
k
],
true
;
case
d
.
dp
<
d
.
nd
&&
d
.
nd
-
d
.
dp
<=
22
:
// int / 10^k
case
d
.
dp
<
d
.
nd
&&
d
.
nd
-
d
.
dp
<=
22
:
// int / 10^k
f
:=
DecimalToFloat
64Int
(
neg
,
d
);
f
:=
decimalAtof
64Int
(
neg
,
d
);
return
f
/
float64pow10
[
d
.
nd
-
d
.
dp
],
true
;
return
f
/
float64pow10
[
d
.
nd
-
d
.
dp
],
true
;
}
}
return
;
return
;
...
@@ -278,7 +278,7 @@ func DecimalToFloat64(neg bool, d *Decimal, trunc bool) (f float64, ok bool) {
...
@@ -278,7 +278,7 @@ func DecimalToFloat64(neg bool, d *Decimal, trunc bool) (f float64, ok bool) {
// If possible to convert decimal d to 32-bit float f exactly,
// If possible to convert decimal d to 32-bit float f exactly,
// entirely in floating-point math, do so, avoiding the machinery above.
// entirely in floating-point math, do so, avoiding the machinery above.
func
DecimalToFloat32
(
neg
bool
,
d
*
D
ecimal
,
trunc
bool
)
(
f
float32
,
ok
bool
)
{
func
decimalAtof32
(
neg
bool
,
d
*
d
ecimal
,
trunc
bool
)
(
f
float32
,
ok
bool
)
{
// Exact integers are <= 10^7.
// Exact integers are <= 10^7.
// Exact powers of ten are <= 10^10.
// Exact powers of ten are <= 10^10.
if
d
.
nd
>
7
{
if
d
.
nd
>
7
{
...
@@ -286,11 +286,11 @@ func DecimalToFloat32(neg bool, d *Decimal, trunc bool) (f float32, ok bool) {
...
@@ -286,11 +286,11 @@ func DecimalToFloat32(neg bool, d *Decimal, trunc bool) (f float32, ok bool) {
}
}
switch
{
switch
{
case
d
.
dp
==
d
.
nd
:
// int
case
d
.
dp
==
d
.
nd
:
// int
f
:=
DecimalToFloat
32Int
(
neg
,
d
);
f
:=
decimalAtof
32Int
(
neg
,
d
);
return
f
,
true
;
return
f
,
true
;
case
d
.
dp
>
d
.
nd
&&
d
.
dp
<=
7
+
10
:
// int * 10^k
case
d
.
dp
>
d
.
nd
&&
d
.
dp
<=
7
+
10
:
// int * 10^k
f
:=
DecimalToFloat
32Int
(
neg
,
d
);
f
:=
decimalAtof
32Int
(
neg
,
d
);
k
:=
d
.
dp
-
d
.
nd
;
k
:=
d
.
dp
-
d
.
nd
;
// If exponent is big but number of digits is not,
// If exponent is big but number of digits is not,
// can move a few zeros into the integer part.
// can move a few zeros into the integer part.
...
@@ -301,7 +301,7 @@ func DecimalToFloat32(neg bool, d *Decimal, trunc bool) (f float32, ok bool) {
...
@@ -301,7 +301,7 @@ func DecimalToFloat32(neg bool, d *Decimal, trunc bool) (f float32, ok bool) {
return
f
*
float32pow10
[
k
],
true
;
return
f
*
float32pow10
[
k
],
true
;
case
d
.
dp
<
d
.
nd
&&
d
.
nd
-
d
.
dp
<=
10
:
// int / 10^k
case
d
.
dp
<
d
.
nd
&&
d
.
nd
-
d
.
dp
<=
10
:
// int / 10^k
f
:=
DecimalToFloat
32Int
(
neg
,
d
);
f
:=
decimalAtof
32Int
(
neg
,
d
);
return
f
/
float32pow10
[
d
.
nd
-
d
.
dp
],
true
;
return
f
/
float32pow10
[
d
.
nd
-
d
.
dp
],
true
;
}
}
return
;
return
;
...
@@ -318,17 +318,17 @@ func DecimalToFloat32(neg bool, d *Decimal, trunc bool) (f float32, ok bool) {
...
@@ -318,17 +318,17 @@ func DecimalToFloat32(neg bool, d *Decimal, trunc bool) (f float32, ok bool) {
// If s is syntactically well-formed but is more than 1/2 ULP
// If s is syntactically well-formed but is more than 1/2 ULP
// away from the largest floating point number of the given size,
// away from the largest floating point number of the given size,
// returns f = ±Inf, err = os.ERANGE.
// returns f = ±Inf, err = os.ERANGE.
export
func
a
tof64
(
s
string
)
(
f
float64
,
err
*
os
.
Error
)
{
export
func
A
tof64
(
s
string
)
(
f
float64
,
err
*
os
.
Error
)
{
neg
,
d
,
trunc
,
ok
:=
S
tringToDecimal
(
s
);
neg
,
d
,
trunc
,
ok
:=
s
tringToDecimal
(
s
);
if
!
ok
{
if
!
ok
{
return
0
,
os
.
EINVAL
;
return
0
,
os
.
EINVAL
;
}
}
if
optimize
{
if
optimize
{
if
f
,
ok
:=
DecimalToFloat
64
(
neg
,
d
,
trunc
);
ok
{
if
f
,
ok
:=
decimalAtof
64
(
neg
,
d
,
trunc
);
ok
{
return
f
,
nil
;
return
f
,
nil
;
}
}
}
}
b
,
ovf
:=
D
ecimalToFloatBits
(
neg
,
d
,
trunc
,
&
float64info
);
b
,
ovf
:=
d
ecimalToFloatBits
(
neg
,
d
,
trunc
,
&
float64info
);
f
=
sys
.
float64frombits
(
b
);
f
=
sys
.
float64frombits
(
b
);
if
ovf
{
if
ovf
{
err
=
os
.
ERANGE
;
err
=
os
.
ERANGE
;
...
@@ -336,17 +336,17 @@ export func atof64(s string) (f float64, err *os.Error) {
...
@@ -336,17 +336,17 @@ export func atof64(s string) (f float64, err *os.Error) {
return
f
,
err
return
f
,
err
}
}
export
func
a
tof32
(
s
string
)
(
f
float32
,
err
*
os
.
Error
)
{
export
func
A
tof32
(
s
string
)
(
f
float32
,
err
*
os
.
Error
)
{
neg
,
d
,
trunc
,
ok
:=
S
tringToDecimal
(
s
);
neg
,
d
,
trunc
,
ok
:=
s
tringToDecimal
(
s
);
if
!
ok
{
if
!
ok
{
return
0
,
os
.
EINVAL
;
return
0
,
os
.
EINVAL
;
}
}
if
optimize
{
if
optimize
{
if
f
,
ok
:=
DecimalToFloat
32
(
neg
,
d
,
trunc
);
ok
{
if
f
,
ok
:=
decimalAtof
32
(
neg
,
d
,
trunc
);
ok
{
return
f
,
nil
;
return
f
,
nil
;
}
}
}
}
b
,
ovf
:=
D
ecimalToFloatBits
(
neg
,
d
,
trunc
,
&
float32info
);
b
,
ovf
:=
d
ecimalToFloatBits
(
neg
,
d
,
trunc
,
&
float32info
);
f
=
sys
.
float32frombits
(
uint32
(
b
));
f
=
sys
.
float32frombits
(
uint32
(
b
));
if
ovf
{
if
ovf
{
err
=
os
.
ERANGE
;
err
=
os
.
ERANGE
;
...
@@ -354,12 +354,12 @@ export func atof32(s string) (f float32, err *os.Error) {
...
@@ -354,12 +354,12 @@ export func atof32(s string) (f float32, err *os.Error) {
return
f
,
err
return
f
,
err
}
}
export
func
a
tof
(
s
string
)
(
f
float
,
err
*
os
.
Error
)
{
export
func
A
tof
(
s
string
)
(
f
float
,
err
*
os
.
Error
)
{
if
FloatSize
==
32
{
if
FloatSize
==
32
{
f1
,
err1
:=
a
tof32
(
s
);
f1
,
err1
:=
A
tof32
(
s
);
return
float
(
f1
),
err1
;
return
float
(
f1
),
err1
;
}
}
f1
,
err1
:=
a
tof64
(
s
);
f1
,
err1
:=
A
tof64
(
s
);
return
float
(
f1
),
err1
;
return
float
(
f1
),
err1
;
}
}
src/lib/strconv/atof_test.go
View file @
8a7cbadb
...
@@ -10,113 +10,113 @@ import (
...
@@ -10,113 +10,113 @@ import (
"testing"
"testing"
)
)
type
A
tofTest
struct
{
type
a
tofTest
struct
{
in
string
;
in
string
;
out
string
;
out
string
;
err
*
os
.
Error
;
err
*
os
.
Error
;
}
}
var
atoftests
=
[]
A
tofTest
{
var
atoftests
=
[]
a
tofTest
{
A
tofTest
{
""
,
"0"
,
os
.
EINVAL
},
a
tofTest
{
""
,
"0"
,
os
.
EINVAL
},
A
tofTest
{
"1"
,
"1"
,
nil
},
a
tofTest
{
"1"
,
"1"
,
nil
},
A
tofTest
{
"+1"
,
"1"
,
nil
},
a
tofTest
{
"+1"
,
"1"
,
nil
},
A
tofTest
{
"1x"
,
"0"
,
os
.
EINVAL
},
a
tofTest
{
"1x"
,
"0"
,
os
.
EINVAL
},
A
tofTest
{
"1.1."
,
"0"
,
os
.
EINVAL
},
a
tofTest
{
"1.1."
,
"0"
,
os
.
EINVAL
},
A
tofTest
{
"1e23"
,
"1e+23"
,
nil
},
a
tofTest
{
"1e23"
,
"1e+23"
,
nil
},
A
tofTest
{
"100000000000000000000000"
,
"1e+23"
,
nil
},
a
tofTest
{
"100000000000000000000000"
,
"1e+23"
,
nil
},
A
tofTest
{
"1e-100"
,
"1e-100"
,
nil
},
a
tofTest
{
"1e-100"
,
"1e-100"
,
nil
},
A
tofTest
{
"123456700"
,
"1.234567e+08"
,
nil
},
a
tofTest
{
"123456700"
,
"1.234567e+08"
,
nil
},
A
tofTest
{
"99999999999999974834176"
,
"9.999999999999997e+22"
,
nil
},
a
tofTest
{
"99999999999999974834176"
,
"9.999999999999997e+22"
,
nil
},
A
tofTest
{
"100000000000000000000001"
,
"1.0000000000000001e+23"
,
nil
},
a
tofTest
{
"100000000000000000000001"
,
"1.0000000000000001e+23"
,
nil
},
A
tofTest
{
"100000000000000008388608"
,
"1.0000000000000001e+23"
,
nil
},
a
tofTest
{
"100000000000000008388608"
,
"1.0000000000000001e+23"
,
nil
},
A
tofTest
{
"100000000000000016777215"
,
"1.0000000000000001e+23"
,
nil
},
a
tofTest
{
"100000000000000016777215"
,
"1.0000000000000001e+23"
,
nil
},
A
tofTest
{
"100000000000000016777216"
,
"1.0000000000000003e+23"
,
nil
},
a
tofTest
{
"100000000000000016777216"
,
"1.0000000000000003e+23"
,
nil
},
A
tofTest
{
"-1"
,
"-1"
,
nil
},
a
tofTest
{
"-1"
,
"-1"
,
nil
},
A
tofTest
{
"-0"
,
"-0"
,
nil
},
a
tofTest
{
"-0"
,
"-0"
,
nil
},
A
tofTest
{
"1e-20"
,
"1e-20"
,
nil
},
a
tofTest
{
"1e-20"
,
"1e-20"
,
nil
},
A
tofTest
{
"625e-3"
,
"0.625"
,
nil
},
a
tofTest
{
"625e-3"
,
"0.625"
,
nil
},
// largest float64
// largest float64
A
tofTest
{
"1.7976931348623157e308"
,
"1.7976931348623157e+308"
,
nil
},
a
tofTest
{
"1.7976931348623157e308"
,
"1.7976931348623157e+308"
,
nil
},
A
tofTest
{
"-1.7976931348623157e308"
,
"-1.7976931348623157e+308"
,
nil
},
a
tofTest
{
"-1.7976931348623157e308"
,
"-1.7976931348623157e+308"
,
nil
},
// next float64 - too large
// next float64 - too large
A
tofTest
{
"1.7976931348623159e308"
,
"+Inf"
,
os
.
ERANGE
},
a
tofTest
{
"1.7976931348623159e308"
,
"+Inf"
,
os
.
ERANGE
},
A
tofTest
{
"-1.7976931348623159e308"
,
"-Inf"
,
os
.
ERANGE
},
a
tofTest
{
"-1.7976931348623159e308"
,
"-Inf"
,
os
.
ERANGE
},
// the border is ...158079
// the border is ...158079
// borderline - okay
// borderline - okay
A
tofTest
{
"1.7976931348623158e308"
,
"1.7976931348623157e+308"
,
nil
},
a
tofTest
{
"1.7976931348623158e308"
,
"1.7976931348623157e+308"
,
nil
},
A
tofTest
{
"-1.7976931348623158e308"
,
"-1.7976931348623157e+308"
,
nil
},
a
tofTest
{
"-1.7976931348623158e308"
,
"-1.7976931348623157e+308"
,
nil
},
// borderline - too large
// borderline - too large
A
tofTest
{
"1.797693134862315808e308"
,
"+Inf"
,
os
.
ERANGE
},
a
tofTest
{
"1.797693134862315808e308"
,
"+Inf"
,
os
.
ERANGE
},
A
tofTest
{
"-1.797693134862315808e308"
,
"-Inf"
,
os
.
ERANGE
},
a
tofTest
{
"-1.797693134862315808e308"
,
"-Inf"
,
os
.
ERANGE
},
// a little too large
// a little too large
A
tofTest
{
"1e308"
,
"1e+308"
,
nil
},
a
tofTest
{
"1e308"
,
"1e+308"
,
nil
},
A
tofTest
{
"2e308"
,
"+Inf"
,
os
.
ERANGE
},
a
tofTest
{
"2e308"
,
"+Inf"
,
os
.
ERANGE
},
A
tofTest
{
"1e309"
,
"+Inf"
,
os
.
ERANGE
},
a
tofTest
{
"1e309"
,
"+Inf"
,
os
.
ERANGE
},
// way too large
// way too large
A
tofTest
{
"1e310"
,
"+Inf"
,
os
.
ERANGE
},
a
tofTest
{
"1e310"
,
"+Inf"
,
os
.
ERANGE
},
A
tofTest
{
"-1e310"
,
"-Inf"
,
os
.
ERANGE
},
a
tofTest
{
"-1e310"
,
"-Inf"
,
os
.
ERANGE
},
A
tofTest
{
"1e400"
,
"+Inf"
,
os
.
ERANGE
},
a
tofTest
{
"1e400"
,
"+Inf"
,
os
.
ERANGE
},
A
tofTest
{
"-1e400"
,
"-Inf"
,
os
.
ERANGE
},
a
tofTest
{
"-1e400"
,
"-Inf"
,
os
.
ERANGE
},
A
tofTest
{
"1e400000"
,
"+Inf"
,
os
.
ERANGE
},
a
tofTest
{
"1e400000"
,
"+Inf"
,
os
.
ERANGE
},
A
tofTest
{
"-1e400000"
,
"-Inf"
,
os
.
ERANGE
},
a
tofTest
{
"-1e400000"
,
"-Inf"
,
os
.
ERANGE
},
// denormalized
// denormalized
A
tofTest
{
"1e-305"
,
"1e-305"
,
nil
},
a
tofTest
{
"1e-305"
,
"1e-305"
,
nil
},
A
tofTest
{
"1e-306"
,
"1e-306"
,
nil
},
a
tofTest
{
"1e-306"
,
"1e-306"
,
nil
},
A
tofTest
{
"1e-307"
,
"1e-307"
,
nil
},
a
tofTest
{
"1e-307"
,
"1e-307"
,
nil
},
A
tofTest
{
"1e-308"
,
"1e-308"
,
nil
},
a
tofTest
{
"1e-308"
,
"1e-308"
,
nil
},
A
tofTest
{
"1e-309"
,
"1e-309"
,
nil
},
a
tofTest
{
"1e-309"
,
"1e-309"
,
nil
},
A
tofTest
{
"1e-310"
,
"1e-310"
,
nil
},
a
tofTest
{
"1e-310"
,
"1e-310"
,
nil
},
A
tofTest
{
"1e-322"
,
"1e-322"
,
nil
},
a
tofTest
{
"1e-322"
,
"1e-322"
,
nil
},
// smallest denormal
// smallest denormal
A
tofTest
{
"5e-324"
,
"5e-324"
,
nil
},
a
tofTest
{
"5e-324"
,
"5e-324"
,
nil
},
// too small
// too small
A
tofTest
{
"4e-324"
,
"0"
,
nil
},
a
tofTest
{
"4e-324"
,
"0"
,
nil
},
// way too small
// way too small
A
tofTest
{
"1e-350"
,
"0"
,
nil
},
a
tofTest
{
"1e-350"
,
"0"
,
nil
},
A
tofTest
{
"1e-400000"
,
"0"
,
nil
},
a
tofTest
{
"1e-400000"
,
"0"
,
nil
},
// try to overflow exponent
// try to overflow exponent
A
tofTest
{
"1e-4294967296"
,
"0"
,
nil
},
a
tofTest
{
"1e-4294967296"
,
"0"
,
nil
},
A
tofTest
{
"1e+4294967296"
,
"+Inf"
,
os
.
ERANGE
},
a
tofTest
{
"1e+4294967296"
,
"+Inf"
,
os
.
ERANGE
},
A
tofTest
{
"1e-18446744073709551616"
,
"0"
,
nil
},
a
tofTest
{
"1e-18446744073709551616"
,
"0"
,
nil
},
A
tofTest
{
"1e+18446744073709551616"
,
"+Inf"
,
os
.
ERANGE
},
a
tofTest
{
"1e+18446744073709551616"
,
"+Inf"
,
os
.
ERANGE
},
// Parse errors
// Parse errors
A
tofTest
{
"1e"
,
"0"
,
os
.
EINVAL
},
a
tofTest
{
"1e"
,
"0"
,
os
.
EINVAL
},
A
tofTest
{
"1e-"
,
"0"
,
os
.
EINVAL
},
a
tofTest
{
"1e-"
,
"0"
,
os
.
EINVAL
},
A
tofTest
{
".e-1"
,
"0"
,
os
.
EINVAL
},
a
tofTest
{
".e-1"
,
"0"
,
os
.
EINVAL
},
}
}
func
XT
estAtof
(
t
*
testing
.
T
,
opt
bool
)
{
func
t
estAtof
(
t
*
testing
.
T
,
opt
bool
)
{
oldopt
:=
strconv
.
optimize
;
oldopt
:=
strconv
.
optimize
;
strconv
.
optimize
=
opt
;
strconv
.
optimize
=
opt
;
for
i
:=
0
;
i
<
len
(
atoftests
);
i
++
{
for
i
:=
0
;
i
<
len
(
atoftests
);
i
++
{
test
:=
&
atoftests
[
i
];
test
:=
&
atoftests
[
i
];
out
,
err
:=
strconv
.
a
tof64
(
test
.
in
);
out
,
err
:=
strconv
.
A
tof64
(
test
.
in
);
outs
:=
strconv
.
f
toa64
(
out
,
'g'
,
-
1
);
outs
:=
strconv
.
F
toa64
(
out
,
'g'
,
-
1
);
if
outs
!=
test
.
out
||
err
!=
test
.
err
{
if
outs
!=
test
.
out
||
err
!=
test
.
err
{
t
.
Errorf
(
"strconv.
a
tof64(%v) = %v, %v want %v, %v
\n
"
,
t
.
Errorf
(
"strconv.
A
tof64(%v) = %v, %v want %v, %v
\n
"
,
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
}
}
if
float64
(
float32
(
out
))
==
out
{
if
float64
(
float32
(
out
))
==
out
{
out32
,
err
:=
strconv
.
a
tof32
(
test
.
in
);
out32
,
err
:=
strconv
.
A
tof32
(
test
.
in
);
outs
:=
strconv
.
f
toa32
(
out32
,
'g'
,
-
1
);
outs
:=
strconv
.
F
toa32
(
out32
,
'g'
,
-
1
);
if
outs
!=
test
.
out
||
err
!=
test
.
err
{
if
outs
!=
test
.
out
||
err
!=
test
.
err
{
t
.
Errorf
(
"strconv.
a
tof32(%v) = %v, %v want %v, %v # %v
\n
"
,
t
.
Errorf
(
"strconv.
A
tof32(%v) = %v, %v want %v, %v # %v
\n
"
,
test
.
in
,
out32
,
err
,
test
.
out
,
test
.
err
,
out
);
test
.
in
,
out32
,
err
,
test
.
out
,
test
.
err
,
out
);
}
}
}
}
if
FloatSize
==
64
||
float64
(
float32
(
out
))
==
out
{
if
FloatSize
==
64
||
float64
(
float32
(
out
))
==
out
{
outf
,
err
:=
strconv
.
a
tof
(
test
.
in
);
outf
,
err
:=
strconv
.
A
tof
(
test
.
in
);
outs
:=
strconv
.
f
toa
(
outf
,
'g'
,
-
1
);
outs
:=
strconv
.
F
toa
(
outf
,
'g'
,
-
1
);
if
outs
!=
test
.
out
||
err
!=
test
.
err
{
if
outs
!=
test
.
out
||
err
!=
test
.
err
{
t
.
Errorf
(
"strconv.
f
toa(%v) = %v, %v want %v, %v # %v
\n
"
,
t
.
Errorf
(
"strconv.
F
toa(%v) = %v, %v want %v, %v # %v
\n
"
,
test
.
in
,
outf
,
err
,
test
.
out
,
test
.
err
,
out
);
test
.
in
,
outf
,
err
,
test
.
out
,
test
.
err
,
out
);
}
}
}
}
...
@@ -125,9 +125,9 @@ func XTestAtof(t *testing.T, opt bool) {
...
@@ -125,9 +125,9 @@ func XTestAtof(t *testing.T, opt bool) {
}
}
export
func
TestAtof
(
t
*
testing
.
T
)
{
export
func
TestAtof
(
t
*
testing
.
T
)
{
XT
estAtof
(
t
,
true
);
t
estAtof
(
t
,
true
);
}
}
export
func
TestAtofSlow
(
t
*
testing
.
T
)
{
export
func
TestAtofSlow
(
t
*
testing
.
T
)
{
XT
estAtof
(
t
,
false
);
t
estAtof
(
t
,
false
);
}
}
src/lib/strconv/atoi.go
View file @
8a7cbadb
...
@@ -5,17 +5,17 @@
...
@@ -5,17 +5,17 @@
package
strconv
package
strconv
import
"os"
import
"os"
func
IntS
ize
()
uint
{
func
computeInts
ize
()
uint
{
siz
:=
uint
(
8
);
siz
:=
uint
(
8
);
for
1
<<
siz
!=
0
{
for
1
<<
siz
!=
0
{
siz
*=
2
siz
*=
2
}
}
return
siz
return
siz
}
}
var
intsize
=
IntS
ize
();
var
intsize
=
computeInts
ize
();
// Convert decimal string to unsigned integer.
// Convert decimal string to unsigned integer.
export
func
a
toui64
(
s
string
)
(
i
uint64
,
err
*
os
.
Error
)
{
export
func
A
toui64
(
s
string
)
(
i
uint64
,
err
*
os
.
Error
)
{
// empty string bad
// empty string bad
if
len
(
s
)
==
0
{
if
len
(
s
)
==
0
{
return
0
,
os
.
EINVAL
return
0
,
os
.
EINVAL
...
@@ -52,7 +52,7 @@ export func atoui64(s string) (i uint64, err *os.Error) {
...
@@ -52,7 +52,7 @@ export func atoui64(s string) (i uint64, err *os.Error) {
}
}
// Convert decimal string to integer.
// Convert decimal string to integer.
export
func
a
toi64
(
s
string
)
(
i
int64
,
err
*
os
.
Error
)
{
export
func
A
toi64
(
s
string
)
(
i
int64
,
err
*
os
.
Error
)
{
// empty string bad
// empty string bad
if
len
(
s
)
==
0
{
if
len
(
s
)
==
0
{
return
0
,
os
.
EINVAL
return
0
,
os
.
EINVAL
...
@@ -68,7 +68,7 @@ export func atoi64(s string) (i int64, err *os.Error) {
...
@@ -68,7 +68,7 @@ export func atoi64(s string) (i int64, err *os.Error) {
}
}
var
un
uint64
;
var
un
uint64
;
un
,
err
=
a
toui64
(
s
);
un
,
err
=
A
toui64
(
s
);
if
err
!=
nil
&&
err
!=
os
.
ERANGE
{
if
err
!=
nil
&&
err
!=
os
.
ERANGE
{
return
0
,
err
return
0
,
err
}
}
...
@@ -85,8 +85,8 @@ export func atoi64(s string) (i int64, err *os.Error) {
...
@@ -85,8 +85,8 @@ export func atoi64(s string) (i int64, err *os.Error) {
return
n
,
nil
return
n
,
nil
}
}
export
func
a
toui
(
s
string
)
(
i
uint
,
err
*
os
.
Error
)
{
export
func
A
toui
(
s
string
)
(
i
uint
,
err
*
os
.
Error
)
{
i1
,
e1
:=
a
toui64
(
s
);
i1
,
e1
:=
A
toui64
(
s
);
if
e1
!=
nil
&&
e1
!=
os
.
ERANGE
{
if
e1
!=
nil
&&
e1
!=
os
.
ERANGE
{
return
0
,
e1
return
0
,
e1
}
}
...
@@ -99,8 +99,8 @@ export func atoui(s string) (i uint, err *os.Error) {
...
@@ -99,8 +99,8 @@ export func atoui(s string) (i uint, err *os.Error) {
return
i
,
nil
return
i
,
nil
}
}
export
func
a
toi
(
s
string
)
(
i
int
,
err
*
os
.
Error
)
{
export
func
A
toi
(
s
string
)
(
i
int
,
err
*
os
.
Error
)
{
i1
,
e1
:=
a
toi64
(
s
);
i1
,
e1
:=
A
toi64
(
s
);
if
e1
!=
nil
&&
e1
!=
os
.
ERANGE
{
if
e1
!=
nil
&&
e1
!=
os
.
ERANGE
{
return
0
,
e1
return
0
,
e1
}
}
...
...
src/lib/strconv/atoi_test.go
View file @
8a7cbadb
...
@@ -10,105 +10,105 @@ import (
...
@@ -10,105 +10,105 @@ import (
"testing"
"testing"
)
)
type
A
toui64Test
struct
{
type
a
toui64Test
struct
{
in
string
;
in
string
;
out
uint64
;
out
uint64
;
err
*
os
.
Error
;
err
*
os
.
Error
;
}
}
var
atoui64tests
=
[]
A
toui64Test
{
var
atoui64tests
=
[]
a
toui64Test
{
A
toui64Test
{
""
,
0
,
os
.
EINVAL
},
a
toui64Test
{
""
,
0
,
os
.
EINVAL
},
A
toui64Test
{
"0"
,
0
,
nil
},
a
toui64Test
{
"0"
,
0
,
nil
},
A
toui64Test
{
"1"
,
1
,
nil
},
a
toui64Test
{
"1"
,
1
,
nil
},
A
toui64Test
{
"12345"
,
12345
,
nil
},
a
toui64Test
{
"12345"
,
12345
,
nil
},
A
toui64Test
{
"012345"
,
0
,
os
.
EINVAL
},
a
toui64Test
{
"012345"
,
0
,
os
.
EINVAL
},
A
toui64Test
{
"12345x"
,
0
,
os
.
EINVAL
},
a
toui64Test
{
"12345x"
,
0
,
os
.
EINVAL
},
A
toui64Test
{
"98765432100"
,
98765432100
,
nil
},
a
toui64Test
{
"98765432100"
,
98765432100
,
nil
},
A
toui64Test
{
"18446744073709551615"
,
1
<<
64
-
1
,
nil
},
a
toui64Test
{
"18446744073709551615"
,
1
<<
64
-
1
,
nil
},
A
toui64Test
{
"18446744073709551616"
,
1
<<
64
-
1
,
os
.
ERANGE
},
a
toui64Test
{
"18446744073709551616"
,
1
<<
64
-
1
,
os
.
ERANGE
},
A
toui64Test
{
"18446744073709551620"
,
1
<<
64
-
1
,
os
.
ERANGE
},
a
toui64Test
{
"18446744073709551620"
,
1
<<
64
-
1
,
os
.
ERANGE
},
}
}
type
A
toi64Test
struct
{
type
a
toi64Test
struct
{
in
string
;
in
string
;
out
int64
;
out
int64
;
err
*
os
.
Error
;
err
*
os
.
Error
;
}
}
var
atoi64test
=
[]
A
toi64Test
{
var
atoi64test
=
[]
a
toi64Test
{
A
toi64Test
{
""
,
0
,
os
.
EINVAL
},
a
toi64Test
{
""
,
0
,
os
.
EINVAL
},
A
toi64Test
{
"0"
,
0
,
nil
},
a
toi64Test
{
"0"
,
0
,
nil
},
A
toi64Test
{
"-0"
,
0
,
nil
},
a
toi64Test
{
"-0"
,
0
,
nil
},
A
toi64Test
{
"1"
,
1
,
nil
},
a
toi64Test
{
"1"
,
1
,
nil
},
A
toi64Test
{
"-1"
,
-
1
,
nil
},
a
toi64Test
{
"-1"
,
-
1
,
nil
},
A
toi64Test
{
"12345"
,
12345
,
nil
},
a
toi64Test
{
"12345"
,
12345
,
nil
},
A
toi64Test
{
"-12345"
,
-
12345
,
nil
},
a
toi64Test
{
"-12345"
,
-
12345
,
nil
},
A
toi64Test
{
"012345"
,
0
,
os
.
EINVAL
},
a
toi64Test
{
"012345"
,
0
,
os
.
EINVAL
},
A
toi64Test
{
"-012345"
,
0
,
os
.
EINVAL
},
a
toi64Test
{
"-012345"
,
0
,
os
.
EINVAL
},
A
toi64Test
{
"12345x"
,
0
,
os
.
EINVAL
},
a
toi64Test
{
"12345x"
,
0
,
os
.
EINVAL
},
A
toi64Test
{
"-12345x"
,
0
,
os
.
EINVAL
},
a
toi64Test
{
"-12345x"
,
0
,
os
.
EINVAL
},
A
toi64Test
{
"98765432100"
,
98765432100
,
nil
},
a
toi64Test
{
"98765432100"
,
98765432100
,
nil
},
A
toi64Test
{
"-98765432100"
,
-
98765432100
,
nil
},
a
toi64Test
{
"-98765432100"
,
-
98765432100
,
nil
},
A
toi64Test
{
"9223372036854775807"
,
1
<<
63
-
1
,
nil
},
a
toi64Test
{
"9223372036854775807"
,
1
<<
63
-
1
,
nil
},
A
toi64Test
{
"-9223372036854775807"
,
-
(
1
<<
63
-
1
),
nil
},
a
toi64Test
{
"-9223372036854775807"
,
-
(
1
<<
63
-
1
),
nil
},
A
toi64Test
{
"9223372036854775808"
,
1
<<
63
-
1
,
os
.
ERANGE
},
a
toi64Test
{
"9223372036854775808"
,
1
<<
63
-
1
,
os
.
ERANGE
},
A
toi64Test
{
"-9223372036854775808"
,
-
1
<<
63
,
nil
},
a
toi64Test
{
"-9223372036854775808"
,
-
1
<<
63
,
nil
},
A
toi64Test
{
"9223372036854775809"
,
1
<<
63
-
1
,
os
.
ERANGE
},
a
toi64Test
{
"9223372036854775809"
,
1
<<
63
-
1
,
os
.
ERANGE
},
A
toi64Test
{
"-9223372036854775809"
,
-
1
<<
63
,
os
.
ERANGE
},
a
toi64Test
{
"-9223372036854775809"
,
-
1
<<
63
,
os
.
ERANGE
},
}
}
type
A
toui32Test
struct
{
type
a
toui32Test
struct
{
in
string
;
in
string
;
out
uint32
;
out
uint32
;
err
*
os
.
Error
;
err
*
os
.
Error
;
}
}
var
atoui32tests
=
[]
A
toui32Test
{
var
atoui32tests
=
[]
a
toui32Test
{
A
toui32Test
{
""
,
0
,
os
.
EINVAL
},
a
toui32Test
{
""
,
0
,
os
.
EINVAL
},
A
toui32Test
{
"0"
,
0
,
nil
},
a
toui32Test
{
"0"
,
0
,
nil
},
A
toui32Test
{
"1"
,
1
,
nil
},
a
toui32Test
{
"1"
,
1
,
nil
},
A
toui32Test
{
"12345"
,
12345
,
nil
},
a
toui32Test
{
"12345"
,
12345
,
nil
},
A
toui32Test
{
"012345"
,
0
,
os
.
EINVAL
},
a
toui32Test
{
"012345"
,
0
,
os
.
EINVAL
},
A
toui32Test
{
"12345x"
,
0
,
os
.
EINVAL
},
a
toui32Test
{
"12345x"
,
0
,
os
.
EINVAL
},
A
toui32Test
{
"987654321"
,
987654321
,
nil
},
a
toui32Test
{
"987654321"
,
987654321
,
nil
},
A
toui32Test
{
"4294967295"
,
1
<<
32
-
1
,
nil
},
a
toui32Test
{
"4294967295"
,
1
<<
32
-
1
,
nil
},
A
toui32Test
{
"4294967296"
,
1
<<
32
-
1
,
os
.
ERANGE
},
a
toui32Test
{
"4294967296"
,
1
<<
32
-
1
,
os
.
ERANGE
},
}
}
type
A
toi32Test
struct
{
type
a
toi32Test
struct
{
in
string
;
in
string
;
out
int32
;
out
int32
;
err
*
os
.
Error
;
err
*
os
.
Error
;
}
}
var
atoi32tests
=
[]
A
toi32Test
{
var
atoi32tests
=
[]
a
toi32Test
{
A
toi32Test
{
""
,
0
,
os
.
EINVAL
},
a
toi32Test
{
""
,
0
,
os
.
EINVAL
},
A
toi32Test
{
"0"
,
0
,
nil
},
a
toi32Test
{
"0"
,
0
,
nil
},
A
toi32Test
{
"-0"
,
0
,
nil
},
a
toi32Test
{
"-0"
,
0
,
nil
},
A
toi32Test
{
"1"
,
1
,
nil
},
a
toi32Test
{
"1"
,
1
,
nil
},
A
toi32Test
{
"-1"
,
-
1
,
nil
},
a
toi32Test
{
"-1"
,
-
1
,
nil
},
A
toi32Test
{
"12345"
,
12345
,
nil
},
a
toi32Test
{
"12345"
,
12345
,
nil
},
A
toi32Test
{
"-12345"
,
-
12345
,
nil
},
a
toi32Test
{
"-12345"
,
-
12345
,
nil
},
A
toi32Test
{
"012345"
,
0
,
os
.
EINVAL
},
a
toi32Test
{
"012345"
,
0
,
os
.
EINVAL
},
A
toi32Test
{
"-012345"
,
0
,
os
.
EINVAL
},
a
toi32Test
{
"-012345"
,
0
,
os
.
EINVAL
},
A
toi32Test
{
"12345x"
,
0
,
os
.
EINVAL
},
a
toi32Test
{
"12345x"
,
0
,
os
.
EINVAL
},
A
toi32Test
{
"-12345x"
,
0
,
os
.
EINVAL
},
a
toi32Test
{
"-12345x"
,
0
,
os
.
EINVAL
},
A
toi32Test
{
"987654321"
,
987654321
,
nil
},
a
toi32Test
{
"987654321"
,
987654321
,
nil
},
A
toi32Test
{
"-987654321"
,
-
987654321
,
nil
},
a
toi32Test
{
"-987654321"
,
-
987654321
,
nil
},
A
toi32Test
{
"2147483647"
,
1
<<
31
-
1
,
nil
},
a
toi32Test
{
"2147483647"
,
1
<<
31
-
1
,
nil
},
A
toi32Test
{
"-2147483647"
,
-
(
1
<<
31
-
1
),
nil
},
a
toi32Test
{
"-2147483647"
,
-
(
1
<<
31
-
1
),
nil
},
A
toi32Test
{
"2147483648"
,
1
<<
31
-
1
,
os
.
ERANGE
},
a
toi32Test
{
"2147483648"
,
1
<<
31
-
1
,
os
.
ERANGE
},
A
toi32Test
{
"-2147483648"
,
-
1
<<
31
,
nil
},
a
toi32Test
{
"-2147483648"
,
-
1
<<
31
,
nil
},
A
toi32Test
{
"2147483649"
,
1
<<
31
-
1
,
os
.
ERANGE
},
a
toi32Test
{
"2147483649"
,
1
<<
31
-
1
,
os
.
ERANGE
},
A
toi32Test
{
"-2147483649"
,
-
1
<<
31
,
os
.
ERANGE
},
a
toi32Test
{
"-2147483649"
,
-
1
<<
31
,
os
.
ERANGE
},
}
}
export
func
TestAtoui64
(
t
*
testing
.
T
)
{
export
func
TestAtoui64
(
t
*
testing
.
T
)
{
for
i
:=
0
;
i
<
len
(
atoui64tests
);
i
++
{
for
i
:=
0
;
i
<
len
(
atoui64tests
);
i
++
{
test
:=
&
atoui64tests
[
i
];
test
:=
&
atoui64tests
[
i
];
out
,
err
:=
strconv
.
a
toui64
(
test
.
in
);
out
,
err
:=
strconv
.
A
toui64
(
test
.
in
);
if
test
.
out
!=
out
||
test
.
err
!=
err
{
if
test
.
out
!=
out
||
test
.
err
!=
err
{
t
.
Errorf
(
"strconv.
a
toui64(%v) = %v, %v want %v, %v
\n
"
,
t
.
Errorf
(
"strconv.
A
toui64(%v) = %v, %v want %v, %v
\n
"
,
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
}
}
}
}
...
@@ -117,39 +117,31 @@ export func TestAtoui64(t *testing.T) {
...
@@ -117,39 +117,31 @@ export func TestAtoui64(t *testing.T) {
export
func
TestAtoi64
(
t
*
testing
.
T
)
{
export
func
TestAtoi64
(
t
*
testing
.
T
)
{
for
i
:=
0
;
i
<
len
(
atoi64test
);
i
++
{
for
i
:=
0
;
i
<
len
(
atoi64test
);
i
++
{
test
:=
&
atoi64test
[
i
];
test
:=
&
atoi64test
[
i
];
out
,
err
:=
strconv
.
a
toi64
(
test
.
in
);
out
,
err
:=
strconv
.
A
toi64
(
test
.
in
);
if
test
.
out
!=
out
||
test
.
err
!=
err
{
if
test
.
out
!=
out
||
test
.
err
!=
err
{
t
.
Errorf
(
"strconv.
a
toi64(%v) = %v, %v want %v, %v
\n
"
,
t
.
Errorf
(
"strconv.
A
toi64(%v) = %v, %v want %v, %v
\n
"
,
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
}
}
}
}
}
}
func
IntSize1
()
uint
{
tmp
:=
1
;
if
tmp
<<
16
<<
16
==
0
{
return
32
;
}
return
64
;
}
export
func
TestAtoui
(
t
*
testing
.
T
)
{
export
func
TestAtoui
(
t
*
testing
.
T
)
{
switch
IntSize1
()
{
switch
intsize
{
case
32
:
case
32
:
for
i
:=
0
;
i
<
len
(
atoui32tests
);
i
++
{
for
i
:=
0
;
i
<
len
(
atoui32tests
);
i
++
{
test
:=
&
atoui32tests
[
i
];
test
:=
&
atoui32tests
[
i
];
out
,
err
:=
strconv
.
a
toui
(
test
.
in
);
out
,
err
:=
strconv
.
A
toui
(
test
.
in
);
if
test
.
out
!=
uint32
(
out
)
||
test
.
err
!=
err
{
if
test
.
out
!=
uint32
(
out
)
||
test
.
err
!=
err
{
t
.
Errorf
(
"strconv.
a
toui(%v) = %v, %v want %v, %v
\n
"
,
t
.
Errorf
(
"strconv.
A
toui(%v) = %v, %v want %v, %v
\n
"
,
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
}
}
}
}
case
64
:
case
64
:
for
i
:=
0
;
i
<
len
(
atoui64tests
);
i
++
{
for
i
:=
0
;
i
<
len
(
atoui64tests
);
i
++
{
test
:=
&
atoui64tests
[
i
];
test
:=
&
atoui64tests
[
i
];
out
,
err
:=
strconv
.
a
toui
(
test
.
in
);
out
,
err
:=
strconv
.
A
toui
(
test
.
in
);
if
test
.
out
!=
uint64
(
out
)
||
test
.
err
!=
err
{
if
test
.
out
!=
uint64
(
out
)
||
test
.
err
!=
err
{
t
.
Errorf
(
"strconv.
a
toui(%v) = %v, %v want %v, %v
\n
"
,
t
.
Errorf
(
"strconv.
A
toui(%v) = %v, %v want %v, %v
\n
"
,
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
}
}
}
}
...
@@ -157,22 +149,22 @@ export func TestAtoui(t *testing.T) {
...
@@ -157,22 +149,22 @@ export func TestAtoui(t *testing.T) {
}
}
export
func
TestAtoi
(
t
*
testing
.
T
)
{
export
func
TestAtoi
(
t
*
testing
.
T
)
{
switch
IntSize1
()
{
switch
intsize
{
case
32
:
case
32
:
for
i
:=
0
;
i
<
len
(
atoi32tests
);
i
++
{
for
i
:=
0
;
i
<
len
(
atoi32tests
);
i
++
{
test
:=
&
atoi32tests
[
i
];
test
:=
&
atoi32tests
[
i
];
out
,
err
:=
strconv
.
a
toi
(
test
.
in
);
out
,
err
:=
strconv
.
A
toi
(
test
.
in
);
if
test
.
out
!=
int32
(
out
)
||
test
.
err
!=
err
{
if
test
.
out
!=
int32
(
out
)
||
test
.
err
!=
err
{
t
.
Errorf
(
"strconv.
a
toi(%v) = %v, %v want %v, %v
\n
"
,
t
.
Errorf
(
"strconv.
A
toi(%v) = %v, %v want %v, %v
\n
"
,
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
}
}
}
}
case
64
:
case
64
:
for
i
:=
0
;
i
<
len
(
atoi64test
);
i
++
{
for
i
:=
0
;
i
<
len
(
atoi64test
);
i
++
{
test
:=
&
atoi64test
[
i
];
test
:=
&
atoi64test
[
i
];
out
,
err
:=
strconv
.
a
toi
(
test
.
in
);
out
,
err
:=
strconv
.
A
toi
(
test
.
in
);
if
test
.
out
!=
int64
(
out
)
||
test
.
err
!=
err
{
if
test
.
out
!=
int64
(
out
)
||
test
.
err
!=
err
{
t
.
Errorf
(
"strconv.
a
toi(%v) = %v, %v want %v, %v
\n
"
,
t
.
Errorf
(
"strconv.
A
toi(%v) = %v, %v want %v, %v
\n
"
,
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
);
}
}
}
}
...
...
src/lib/strconv/decimal.go
View file @
8a7cbadb
...
@@ -11,26 +11,26 @@
...
@@ -11,26 +11,26 @@
package
strconv
package
strconv
package
type
D
ecimal
struct
{
type
d
ecimal
struct
{
// TODO(rsc): Can make d[] a bit smaller and add
// TODO(rsc): Can make d[] a bit smaller and add
// truncated bool;
// truncated bool;
d
[
2000
]
byte
;
// digits
d
[
2000
]
byte
;
// digits
nd
int
;
// number of digits used
nd
int
;
// number of digits used
dp
int
;
// decimal point
dp
int
;
// decimal point
};
};
func
(
a
*
D
ecimal
)
String
()
string
;
func
(
a
*
d
ecimal
)
String
()
string
;
func
(
a
*
D
ecimal
)
Assign
(
v
uint64
);
func
(
a
*
d
ecimal
)
Assign
(
v
uint64
);
func
(
a
*
Decimal
)
Shift
(
k
int
)
*
D
ecimal
;
func
(
a
*
decimal
)
Shift
(
k
int
)
*
d
ecimal
;
func
(
a
*
Decimal
)
Round
(
nd
int
)
*
D
ecimal
;
func
(
a
*
decimal
)
Round
(
nd
int
)
*
d
ecimal
;
func
(
a
*
Decimal
)
RoundUp
(
nd
int
)
*
D
ecimal
;
func
(
a
*
decimal
)
RoundUp
(
nd
int
)
*
d
ecimal
;
func
(
a
*
Decimal
)
RoundDown
(
nd
int
)
*
D
ecimal
;
func
(
a
*
decimal
)
RoundDown
(
nd
int
)
*
d
ecimal
;
func
(
a
*
D
ecimal
)
RoundedInteger
()
uint64
;
func
(
a
*
d
ecimal
)
RoundedInteger
()
uint64
;
func
C
opy
(
dst
[]
byte
,
src
[]
byte
)
int
;
func
c
opy
(
dst
[]
byte
,
src
[]
byte
)
int
;
func
D
igitZero
(
dst
[]
byte
)
int
;
func
d
igitZero
(
dst
[]
byte
)
int
;
func
(
a
*
D
ecimal
)
String
()
string
{
func
(
a
*
d
ecimal
)
String
()
string
{
n
:=
10
+
a
.
nd
;
n
:=
10
+
a
.
nd
;
if
a
.
dp
>
0
{
if
a
.
dp
>
0
{
n
+=
a
.
dp
;
n
+=
a
.
dp
;
...
@@ -51,42 +51,42 @@ func (a *Decimal) String() string {
...
@@ -51,42 +51,42 @@ func (a *Decimal) String() string {
w
++
;
w
++
;
buf
[
w
]
=
'.'
;
buf
[
w
]
=
'.'
;
w
++
;
w
++
;
w
+=
D
igitZero
(
buf
[
w
:
w
+-
a
.
dp
]);
w
+=
d
igitZero
(
buf
[
w
:
w
+-
a
.
dp
]);
w
+=
C
opy
(
buf
[
w
:
w
+
a
.
nd
],
a
.
d
[
0
:
a
.
nd
]);
w
+=
c
opy
(
buf
[
w
:
w
+
a
.
nd
],
a
.
d
[
0
:
a
.
nd
]);
case
a
.
dp
<
a
.
nd
:
case
a
.
dp
<
a
.
nd
:
// decimal point in middle of digits
// decimal point in middle of digits
w
+=
C
opy
(
buf
[
w
:
w
+
a
.
dp
],
a
.
d
[
0
:
a
.
dp
]);
w
+=
c
opy
(
buf
[
w
:
w
+
a
.
dp
],
a
.
d
[
0
:
a
.
dp
]);
buf
[
w
]
=
'.'
;
buf
[
w
]
=
'.'
;
w
++
;
w
++
;
w
+=
C
opy
(
buf
[
w
:
w
+
a
.
nd
-
a
.
dp
],
a
.
d
[
a
.
dp
:
a
.
nd
]);
w
+=
c
opy
(
buf
[
w
:
w
+
a
.
nd
-
a
.
dp
],
a
.
d
[
a
.
dp
:
a
.
nd
]);
default
:
default
:
// zeros fill space between digits and decimal point
// zeros fill space between digits and decimal point
w
+=
C
opy
(
buf
[
w
:
w
+
a
.
nd
],
a
.
d
[
0
:
a
.
nd
]);
w
+=
c
opy
(
buf
[
w
:
w
+
a
.
nd
],
a
.
d
[
0
:
a
.
nd
]);
w
+=
D
igitZero
(
buf
[
w
:
w
+
a
.
dp
-
a
.
nd
]);
w
+=
d
igitZero
(
buf
[
w
:
w
+
a
.
dp
-
a
.
nd
]);
}
}
return
string
(
buf
[
0
:
w
]);
return
string
(
buf
[
0
:
w
]);
}
}
func
C
opy
(
dst
[]
byte
,
src
[]
byte
)
int
{
func
c
opy
(
dst
[]
byte
,
src
[]
byte
)
int
{
for
i
:=
0
;
i
<
len
(
dst
);
i
++
{
for
i
:=
0
;
i
<
len
(
dst
);
i
++
{
dst
[
i
]
=
src
[
i
];
dst
[
i
]
=
src
[
i
];
}
}
return
len
(
dst
);
return
len
(
dst
);
}
}
func
D
igitZero
(
dst
[]
byte
)
int
{
func
d
igitZero
(
dst
[]
byte
)
int
{
for
i
:=
0
;
i
<
len
(
dst
);
i
++
{
for
i
:=
0
;
i
<
len
(
dst
);
i
++
{
dst
[
i
]
=
'0'
;
dst
[
i
]
=
'0'
;
}
}
return
len
(
dst
);
return
len
(
dst
);
}
}
//
T
rim trailing zeros from number.
//
t
rim trailing zeros from number.
// (They are meaningless; the decimal point is tracked
// (They are meaningless; the decimal point is tracked
// independent of the number of digits.)
// independent of the number of digits.)
func
Trim
(
a
*
D
ecimal
)
{
func
trim
(
a
*
d
ecimal
)
{
for
a
.
nd
>
0
&&
a
.
d
[
a
.
nd
-
1
]
==
'0'
{
for
a
.
nd
>
0
&&
a
.
d
[
a
.
nd
-
1
]
==
'0'
{
a
.
nd
--
;
a
.
nd
--
;
}
}
...
@@ -96,7 +96,7 @@ func Trim(a *Decimal) {
...
@@ -96,7 +96,7 @@ func Trim(a *Decimal) {
}
}
// Assign v to a.
// Assign v to a.
func
(
a
*
D
ecimal
)
Assign
(
v
uint64
)
{
func
(
a
*
d
ecimal
)
Assign
(
v
uint64
)
{
var
buf
[
50
]
byte
;
var
buf
[
50
]
byte
;
// Write reversed decimal in buf.
// Write reversed decimal in buf.
...
@@ -116,21 +116,21 @@ func (a *Decimal) Assign(v uint64) {
...
@@ -116,21 +116,21 @@ func (a *Decimal) Assign(v uint64) {
a
.
nd
++
;
a
.
nd
++
;
}
}
a
.
dp
=
a
.
nd
;
a
.
dp
=
a
.
nd
;
T
rim
(
a
);
t
rim
(
a
);
}
}
package
func
NewDecimal
(
i
uint64
)
*
D
ecimal
{
func
newDecimal
(
i
uint64
)
*
d
ecimal
{
a
:=
new
(
D
ecimal
);
a
:=
new
(
d
ecimal
);
a
.
Assign
(
i
);
a
.
Assign
(
i
);
return
a
;
return
a
;
}
}
// Maximum shift that we can do in one pass without overflow.
// Maximum shift that we can do in one pass without overflow.
// Signed int has 31 bits, and we have to be able to accomodate 9<<k.
// Signed int has 31 bits, and we have to be able to accomodate 9<<k.
const
M
axShift
=
27
const
m
axShift
=
27
// Binary shift right (* 2) by k bits. k <=
M
axShift to avoid overflow.
// Binary shift right (* 2) by k bits. k <=
m
axShift to avoid overflow.
func
RightShift
(
a
*
D
ecimal
,
k
uint
)
{
func
rightShift
(
a
*
d
ecimal
,
k
uint
)
{
r
:=
0
;
// read pointer
r
:=
0
;
// read pointer
w
:=
0
;
// write pointer
w
:=
0
;
// write pointer
...
@@ -174,69 +174,69 @@ func RightShift(a *Decimal, k uint) {
...
@@ -174,69 +174,69 @@ func RightShift(a *Decimal, k uint) {
}
}
a
.
nd
=
w
;
a
.
nd
=
w
;
T
rim
(
a
);
t
rim
(
a
);
}
}
// Cheat sheet for left shift: table indexed by shift count giving
// Cheat sheet for left shift: table indexed by shift count giving
// number of new digits that will be introduced by that shift.
// number of new digits that will be introduced by that shift.
//
//
// For example, leftcheat[4] = {2, "625"}. That means that
// For example, leftcheat
s
[4] = {2, "625"}. That means that
// if we are shifting by 4 (multiplying by 16), it will add 2 digits
// if we are shifting by 4 (multiplying by 16), it will add 2 digits
// when the string prefix is "625" through "999", and one fewer digit
// when the string prefix is "625" through "999", and one fewer digit
// if the string prefix is "000" through "624".
// if the string prefix is "000" through "624".
//
//
// Credit for this trick goes to Ken.
// Credit for this trick goes to Ken.
type
L
eftCheat
struct
{
type
l
eftCheat
struct
{
delta
int
;
// number of new digits
delta
int
;
// number of new digits
cutoff
string
;
// minus one digit if original < a.
cutoff
string
;
// minus one digit if original < a.
}
}
var
leftcheat
=
[]
L
eftCheat
{
var
leftcheat
s
=
[]
l
eftCheat
{
// Leading digits of 1/2^i = 5^i.
// Leading digits of 1/2^i = 5^i.
// 5^23 is not an exact 64-bit floating point number,
// 5^23 is not an exact 64-bit floating point number,
// so have to use bc for the math.
// so have to use bc for the math.
/*
/*
seq 27 | sed 's/^/5^/' | bc |
seq 27 | sed 's/^/5^/' | bc |
awk 'BEGIN{ print "\t
L
eftCheat{ 0, \"\" }," }
awk 'BEGIN{ print "\t
l
eftCheat{ 0, \"\" }," }
{
{
log2 = log(2)/log(10)
log2 = log(2)/log(10)
printf("\t
L
eftCheat{ %d, \"%s\" },\t// * %d\n",
printf("\t
l
eftCheat{ %d, \"%s\" },\t// * %d\n",
int(log2*NR+1), $0, 2**NR)
int(log2*NR+1), $0, 2**NR)
}'
}'
*/
*/
L
eftCheat
{
0
,
""
},
l
eftCheat
{
0
,
""
},
L
eftCheat
{
1
,
"5"
},
// * 2
l
eftCheat
{
1
,
"5"
},
// * 2
L
eftCheat
{
1
,
"25"
},
// * 4
l
eftCheat
{
1
,
"25"
},
// * 4
L
eftCheat
{
1
,
"125"
},
// * 8
l
eftCheat
{
1
,
"125"
},
// * 8
L
eftCheat
{
2
,
"625"
},
// * 16
l
eftCheat
{
2
,
"625"
},
// * 16
L
eftCheat
{
2
,
"3125"
},
// * 32
l
eftCheat
{
2
,
"3125"
},
// * 32
L
eftCheat
{
2
,
"15625"
},
// * 64
l
eftCheat
{
2
,
"15625"
},
// * 64
L
eftCheat
{
3
,
"78125"
},
// * 128
l
eftCheat
{
3
,
"78125"
},
// * 128
L
eftCheat
{
3
,
"390625"
},
// * 256
l
eftCheat
{
3
,
"390625"
},
// * 256
L
eftCheat
{
3
,
"1953125"
},
// * 512
l
eftCheat
{
3
,
"1953125"
},
// * 512
L
eftCheat
{
4
,
"9765625"
},
// * 1024
l
eftCheat
{
4
,
"9765625"
},
// * 1024
L
eftCheat
{
4
,
"48828125"
},
// * 2048
l
eftCheat
{
4
,
"48828125"
},
// * 2048
L
eftCheat
{
4
,
"244140625"
},
// * 4096
l
eftCheat
{
4
,
"244140625"
},
// * 4096
L
eftCheat
{
4
,
"1220703125"
},
// * 8192
l
eftCheat
{
4
,
"1220703125"
},
// * 8192
L
eftCheat
{
5
,
"6103515625"
},
// * 16384
l
eftCheat
{
5
,
"6103515625"
},
// * 16384
L
eftCheat
{
5
,
"30517578125"
},
// * 32768
l
eftCheat
{
5
,
"30517578125"
},
// * 32768
L
eftCheat
{
5
,
"152587890625"
},
// * 65536
l
eftCheat
{
5
,
"152587890625"
},
// * 65536
L
eftCheat
{
6
,
"762939453125"
},
// * 131072
l
eftCheat
{
6
,
"762939453125"
},
// * 131072
L
eftCheat
{
6
,
"3814697265625"
},
// * 262144
l
eftCheat
{
6
,
"3814697265625"
},
// * 262144
L
eftCheat
{
6
,
"19073486328125"
},
// * 524288
l
eftCheat
{
6
,
"19073486328125"
},
// * 524288
L
eftCheat
{
7
,
"95367431640625"
},
// * 1048576
l
eftCheat
{
7
,
"95367431640625"
},
// * 1048576
L
eftCheat
{
7
,
"476837158203125"
},
// * 2097152
l
eftCheat
{
7
,
"476837158203125"
},
// * 2097152
L
eftCheat
{
7
,
"2384185791015625"
},
// * 4194304
l
eftCheat
{
7
,
"2384185791015625"
},
// * 4194304
L
eftCheat
{
7
,
"11920928955078125"
},
// * 8388608
l
eftCheat
{
7
,
"11920928955078125"
},
// * 8388608
L
eftCheat
{
8
,
"59604644775390625"
},
// * 16777216
l
eftCheat
{
8
,
"59604644775390625"
},
// * 16777216
L
eftCheat
{
8
,
"298023223876953125"
},
// * 33554432
l
eftCheat
{
8
,
"298023223876953125"
},
// * 33554432
L
eftCheat
{
8
,
"1490116119384765625"
},
// * 67108864
l
eftCheat
{
8
,
"1490116119384765625"
},
// * 67108864
L
eftCheat
{
9
,
"7450580596923828125"
},
// * 134217728
l
eftCheat
{
9
,
"7450580596923828125"
},
// * 134217728
}
}
// Is the leading prefix of b lexicographically less than s?
// Is the leading prefix of b lexicographically less than s?
func
P
refixIsLessThan
(
b
[]
byte
,
s
string
)
bool
{
func
p
refixIsLessThan
(
b
[]
byte
,
s
string
)
bool
{
for
i
:=
0
;
i
<
len
(
s
);
i
++
{
for
i
:=
0
;
i
<
len
(
s
);
i
++
{
if
i
>=
len
(
b
)
{
if
i
>=
len
(
b
)
{
return
true
;
return
true
;
...
@@ -248,10 +248,10 @@ func PrefixIsLessThan(b []byte, s string) bool {
...
@@ -248,10 +248,10 @@ func PrefixIsLessThan(b []byte, s string) bool {
return
false
;
return
false
;
}
}
// Binary shift left (/ 2) by k bits. k <=
M
axShift to avoid overflow.
// Binary shift left (/ 2) by k bits. k <=
m
axShift to avoid overflow.
func
LeftShift
(
a
*
D
ecimal
,
k
uint
)
{
func
leftShift
(
a
*
d
ecimal
,
k
uint
)
{
delta
:=
leftcheat
[
k
]
.
delta
;
delta
:=
leftcheat
s
[
k
]
.
delta
;
if
PrefixIsLessThan
(
a
.
d
[
0
:
a
.
nd
],
leftcheat
[
k
]
.
cutoff
)
{
if
prefixIsLessThan
(
a
.
d
[
0
:
a
.
nd
],
leftcheats
[
k
]
.
cutoff
)
{
delta
--
;
delta
--
;
}
}
...
@@ -280,37 +280,37 @@ func LeftShift(a *Decimal, k uint) {
...
@@ -280,37 +280,37 @@ func LeftShift(a *Decimal, k uint) {
if
w
!=
0
{
if
w
!=
0
{
// TODO: Remove - has no business panicking.
// TODO: Remove - has no business panicking.
panicln
(
"strconv: bad
L
eftShift"
,
w
);
panicln
(
"strconv: bad
l
eftShift"
,
w
);
}
}
a
.
nd
+=
delta
;
a
.
nd
+=
delta
;
a
.
dp
+=
delta
;
a
.
dp
+=
delta
;
T
rim
(
a
);
t
rim
(
a
);
}
}
// Binary shift left (k > 0) or right (k < 0).
// Binary shift left (k > 0) or right (k < 0).
// Returns receiver for convenience.
// Returns receiver for convenience.
func
(
a
*
Decimal
)
Shift
(
k
int
)
*
D
ecimal
{
func
(
a
*
decimal
)
Shift
(
k
int
)
*
d
ecimal
{
switch
{
switch
{
case
a
.
nd
==
0
:
case
a
.
nd
==
0
:
// nothing to do: a == 0
// nothing to do: a == 0
case
k
>
0
:
case
k
>
0
:
for
k
>
M
axShift
{
for
k
>
m
axShift
{
LeftShift
(
a
,
M
axShift
);
leftShift
(
a
,
m
axShift
);
k
-=
M
axShift
;
k
-=
m
axShift
;
}
}
L
eftShift
(
a
,
uint
(
k
));
l
eftShift
(
a
,
uint
(
k
));
case
k
<
0
:
case
k
<
0
:
for
k
<
-
M
axShift
{
for
k
<
-
m
axShift
{
RightShift
(
a
,
M
axShift
);
rightShift
(
a
,
m
axShift
);
k
+=
M
axShift
;
k
+=
m
axShift
;
}
}
R
ightShift
(
a
,
uint
(
-
k
));
r
ightShift
(
a
,
uint
(
-
k
));
}
}
return
a
;
return
a
;
}
}
// If we chop a at nd digits, should we round up?
// If we chop a at nd digits, should we round up?
func
ShouldRoundUp
(
a
*
D
ecimal
,
nd
int
)
bool
{
func
shouldRoundUp
(
a
*
d
ecimal
,
nd
int
)
bool
{
if
nd
<=
0
||
nd
>=
a
.
nd
{
if
nd
<=
0
||
nd
>=
a
.
nd
{
return
false
;
return
false
;
}
}
...
@@ -323,11 +323,11 @@ func ShouldRoundUp(a *Decimal, nd int) bool {
...
@@ -323,11 +323,11 @@ func ShouldRoundUp(a *Decimal, nd int) bool {
// Round a to nd digits (or fewer).
// Round a to nd digits (or fewer).
// Returns receiver for convenience.
// Returns receiver for convenience.
func
(
a
*
Decimal
)
Round
(
nd
int
)
*
D
ecimal
{
func
(
a
*
decimal
)
Round
(
nd
int
)
*
d
ecimal
{
if
nd
<=
0
||
nd
>=
a
.
nd
{
if
nd
<=
0
||
nd
>=
a
.
nd
{
return
a
;
return
a
;
}
}
if
(
S
houldRoundUp
(
a
,
nd
))
{
if
(
s
houldRoundUp
(
a
,
nd
))
{
return
a
.
RoundUp
(
nd
);
return
a
.
RoundUp
(
nd
);
}
}
return
a
.
RoundDown
(
nd
);
return
a
.
RoundDown
(
nd
);
...
@@ -335,18 +335,18 @@ func (a *Decimal) Round(nd int) *Decimal {
...
@@ -335,18 +335,18 @@ func (a *Decimal) Round(nd int) *Decimal {
// Round a down to nd digits (or fewer).
// Round a down to nd digits (or fewer).
// Returns receiver for convenience.
// Returns receiver for convenience.
func
(
a
*
Decimal
)
RoundDown
(
nd
int
)
*
D
ecimal
{
func
(
a
*
decimal
)
RoundDown
(
nd
int
)
*
d
ecimal
{
if
nd
<=
0
||
nd
>=
a
.
nd
{
if
nd
<=
0
||
nd
>=
a
.
nd
{
return
a
;
return
a
;
}
}
a
.
nd
=
nd
;
a
.
nd
=
nd
;
T
rim
(
a
);
t
rim
(
a
);
return
a
;
return
a
;
}
}
// Round a up to nd digits (or fewer).
// Round a up to nd digits (or fewer).
// Returns receiver for convenience.
// Returns receiver for convenience.
func
(
a
*
Decimal
)
RoundUp
(
nd
int
)
*
D
ecimal
{
func
(
a
*
decimal
)
RoundUp
(
nd
int
)
*
d
ecimal
{
if
nd
<=
0
||
nd
>=
a
.
nd
{
if
nd
<=
0
||
nd
>=
a
.
nd
{
return
a
;
return
a
;
}
}
...
@@ -371,7 +371,7 @@ func (a *Decimal) RoundUp(nd int) *Decimal {
...
@@ -371,7 +371,7 @@ func (a *Decimal) RoundUp(nd int) *Decimal {
// Extract integer part, rounded appropriately.
// Extract integer part, rounded appropriately.
// No guarantees about overflow.
// No guarantees about overflow.
func
(
a
*
D
ecimal
)
RoundedInteger
()
uint64
{
func
(
a
*
d
ecimal
)
RoundedInteger
()
uint64
{
if
a
.
dp
>
20
{
if
a
.
dp
>
20
{
return
0xFFFFFFFFFFFFFFFF
;
return
0xFFFFFFFFFFFFFFFF
;
}
}
...
@@ -383,7 +383,7 @@ func (a *Decimal) RoundedInteger() uint64 {
...
@@ -383,7 +383,7 @@ func (a *Decimal) RoundedInteger() uint64 {
for
;
i
<
a
.
dp
;
i
++
{
for
;
i
<
a
.
dp
;
i
++
{
n
*=
10
;
n
*=
10
;
}
}
if
S
houldRoundUp
(
a
,
a
.
dp
)
{
if
s
houldRoundUp
(
a
,
a
.
dp
)
{
n
++
;
n
++
;
}
}
return
n
;
return
n
;
...
...
src/lib/strconv/decimal_test.go
View file @
8a7cbadb
...
@@ -10,30 +10,30 @@ import (
...
@@ -10,30 +10,30 @@ import (
"testing"
;
"testing"
;
)
)
type
S
hiftTest
struct
{
type
s
hiftTest
struct
{
i
uint64
;
i
uint64
;
shift
int
;
shift
int
;
out
string
;
out
string
;
}
}
var
shifttests
=
[]
S
hiftTest
{
var
shifttests
=
[]
s
hiftTest
{
S
hiftTest
{
0
,
-
100
,
"0"
},
s
hiftTest
{
0
,
-
100
,
"0"
},
S
hiftTest
{
0
,
100
,
"0"
},
s
hiftTest
{
0
,
100
,
"0"
},
S
hiftTest
{
1
,
100
,
"1267650600228229401496703205376"
},
s
hiftTest
{
1
,
100
,
"1267650600228229401496703205376"
},
S
hiftTest
{
1
,
-
100
,
s
hiftTest
{
1
,
-
100
,
"0.00000000000000000000000000000078886090522101180541"
"0.00000000000000000000000000000078886090522101180541"
"17285652827862296732064351090230047702789306640625"
},
"17285652827862296732064351090230047702789306640625"
},
S
hiftTest
{
12345678
,
8
,
"3160493568"
},
s
hiftTest
{
12345678
,
8
,
"3160493568"
},
S
hiftTest
{
12345678
,
-
8
,
"48225.3046875"
},
s
hiftTest
{
12345678
,
-
8
,
"48225.3046875"
},
S
hiftTest
{
195312
,
9
,
"99999744"
},
s
hiftTest
{
195312
,
9
,
"99999744"
},
S
hiftTest
{
1953125
,
9
,
"1000000000"
},
s
hiftTest
{
1953125
,
9
,
"1000000000"
},
}
}
export
func
TestDecimalShift
(
t
*
testing
.
T
)
{
export
func
TestDecimalShift
(
t
*
testing
.
T
)
{
ok
:=
true
;
ok
:=
true
;
for
i
:=
0
;
i
<
len
(
shifttests
);
i
++
{
for
i
:=
0
;
i
<
len
(
shifttests
);
i
++
{
test
:=
&
shifttests
[
i
];
test
:=
&
shifttests
[
i
];
s
:=
strconv
.
N
ewDecimal
(
test
.
i
)
.
Shift
(
test
.
shift
)
.
String
();
s
:=
strconv
.
n
ewDecimal
(
test
.
i
)
.
Shift
(
test
.
shift
)
.
String
();
if
s
!=
test
.
out
{
if
s
!=
test
.
out
{
t
.
Errorf
(
"Decimal %v << %v = %v, want %v
\n
"
,
t
.
Errorf
(
"Decimal %v << %v = %v, want %v
\n
"
,
test
.
i
,
test
.
shift
,
s
,
test
.
out
);
test
.
i
,
test
.
shift
,
s
,
test
.
out
);
...
@@ -41,45 +41,45 @@ export func TestDecimalShift(t *testing.T) {
...
@@ -41,45 +41,45 @@ export func TestDecimalShift(t *testing.T) {
}
}
}
}
type
R
oundTest
struct
{
type
r
oundTest
struct
{
i
uint64
;
i
uint64
;
nd
int
;
nd
int
;
down
,
round
,
up
string
;
down
,
round
,
up
string
;
int
uint64
;
int
uint64
;
}
}
var
roundtests
=
[]
R
oundTest
{
var
roundtests
=
[]
r
oundTest
{
R
oundTest
{
0
,
4
,
"0"
,
"0"
,
"0"
,
0
},
r
oundTest
{
0
,
4
,
"0"
,
"0"
,
"0"
,
0
},
R
oundTest
{
12344999
,
4
,
"12340000"
,
"12340000"
,
"12350000"
,
12340000
},
r
oundTest
{
12344999
,
4
,
"12340000"
,
"12340000"
,
"12350000"
,
12340000
},
R
oundTest
{
12345000
,
4
,
"12340000"
,
"12340000"
,
"12350000"
,
12340000
},
r
oundTest
{
12345000
,
4
,
"12340000"
,
"12340000"
,
"12350000"
,
12340000
},
R
oundTest
{
12345001
,
4
,
"12340000"
,
"12350000"
,
"12350000"
,
12350000
},
r
oundTest
{
12345001
,
4
,
"12340000"
,
"12350000"
,
"12350000"
,
12350000
},
R
oundTest
{
23454999
,
4
,
"23450000"
,
"23450000"
,
"23460000"
,
23450000
},
r
oundTest
{
23454999
,
4
,
"23450000"
,
"23450000"
,
"23460000"
,
23450000
},
R
oundTest
{
23455000
,
4
,
"23450000"
,
"23460000"
,
"23460000"
,
23460000
},
r
oundTest
{
23455000
,
4
,
"23450000"
,
"23460000"
,
"23460000"
,
23460000
},
R
oundTest
{
23455001
,
4
,
"23450000"
,
"23460000"
,
"23460000"
,
23460000
},
r
oundTest
{
23455001
,
4
,
"23450000"
,
"23460000"
,
"23460000"
,
23460000
},
R
oundTest
{
99994999
,
4
,
"99990000"
,
"99990000"
,
"100000000"
,
99990000
},
r
oundTest
{
99994999
,
4
,
"99990000"
,
"99990000"
,
"100000000"
,
99990000
},
R
oundTest
{
99995000
,
4
,
"99990000"
,
"100000000"
,
"100000000"
,
100000000
},
r
oundTest
{
99995000
,
4
,
"99990000"
,
"100000000"
,
"100000000"
,
100000000
},
R
oundTest
{
99999999
,
4
,
"99990000"
,
"100000000"
,
"100000000"
,
100000000
},
r
oundTest
{
99999999
,
4
,
"99990000"
,
"100000000"
,
"100000000"
,
100000000
},
R
oundTest
{
12994999
,
4
,
"12990000"
,
"12990000"
,
"13000000"
,
12990000
},
r
oundTest
{
12994999
,
4
,
"12990000"
,
"12990000"
,
"13000000"
,
12990000
},
R
oundTest
{
12995000
,
4
,
"12990000"
,
"13000000"
,
"13000000"
,
13000000
},
r
oundTest
{
12995000
,
4
,
"12990000"
,
"13000000"
,
"13000000"
,
13000000
},
R
oundTest
{
12999999
,
4
,
"12990000"
,
"13000000"
,
"13000000"
,
13000000
},
r
oundTest
{
12999999
,
4
,
"12990000"
,
"13000000"
,
"13000000"
,
13000000
},
}
}
export
func
TestDecimalRound
(
t
*
testing
.
T
)
{
export
func
TestDecimalRound
(
t
*
testing
.
T
)
{
for
i
:=
0
;
i
<
len
(
roundtests
);
i
++
{
for
i
:=
0
;
i
<
len
(
roundtests
);
i
++
{
test
:=
&
roundtests
[
i
];
test
:=
&
roundtests
[
i
];
s
:=
strconv
.
N
ewDecimal
(
test
.
i
)
.
RoundDown
(
test
.
nd
)
.
String
();
s
:=
strconv
.
n
ewDecimal
(
test
.
i
)
.
RoundDown
(
test
.
nd
)
.
String
();
if
s
!=
test
.
down
{
if
s
!=
test
.
down
{
t
.
Errorf
(
"Decimal %v RoundDown %d = %v, want %v
\n
"
,
t
.
Errorf
(
"Decimal %v RoundDown %d = %v, want %v
\n
"
,
test
.
i
,
test
.
nd
,
s
,
test
.
down
);
test
.
i
,
test
.
nd
,
s
,
test
.
down
);
}
}
s
=
strconv
.
N
ewDecimal
(
test
.
i
)
.
Round
(
test
.
nd
)
.
String
();
s
=
strconv
.
n
ewDecimal
(
test
.
i
)
.
Round
(
test
.
nd
)
.
String
();
if
s
!=
test
.
round
{
if
s
!=
test
.
round
{
t
.
Errorf
(
"Decimal %v Round %d = %v, want %v
\n
"
,
t
.
Errorf
(
"Decimal %v Round %d = %v, want %v
\n
"
,
test
.
i
,
test
.
nd
,
s
,
test
.
down
);
test
.
i
,
test
.
nd
,
s
,
test
.
down
);
}
}
s
=
strconv
.
N
ewDecimal
(
test
.
i
)
.
RoundUp
(
test
.
nd
)
.
String
();
s
=
strconv
.
n
ewDecimal
(
test
.
i
)
.
RoundUp
(
test
.
nd
)
.
String
();
if
s
!=
test
.
up
{
if
s
!=
test
.
up
{
t
.
Errorf
(
"Decimal %v RoundUp %d = %v, want %v
\n
"
,
t
.
Errorf
(
"Decimal %v RoundUp %d = %v, want %v
\n
"
,
test
.
i
,
test
.
nd
,
s
,
test
.
up
);
test
.
i
,
test
.
nd
,
s
,
test
.
up
);
...
@@ -87,30 +87,30 @@ export func TestDecimalRound(t *testing.T) {
...
@@ -87,30 +87,30 @@ export func TestDecimalRound(t *testing.T) {
}
}
}
}
type
R
oundIntTest
struct
{
type
r
oundIntTest
struct
{
i
uint64
;
i
uint64
;
shift
int
;
shift
int
;
int
uint64
;
int
uint64
;
}
}
var
roundinttests
=
[]
R
oundIntTest
{
var
roundinttests
=
[]
r
oundIntTest
{
R
oundIntTest
{
0
,
100
,
0
},
r
oundIntTest
{
0
,
100
,
0
},
R
oundIntTest
{
512
,
-
8
,
2
},
r
oundIntTest
{
512
,
-
8
,
2
},
R
oundIntTest
{
513
,
-
8
,
2
},
r
oundIntTest
{
513
,
-
8
,
2
},
R
oundIntTest
{
640
,
-
8
,
2
},
r
oundIntTest
{
640
,
-
8
,
2
},
R
oundIntTest
{
641
,
-
8
,
3
},
r
oundIntTest
{
641
,
-
8
,
3
},
R
oundIntTest
{
384
,
-
8
,
2
},
r
oundIntTest
{
384
,
-
8
,
2
},
R
oundIntTest
{
385
,
-
8
,
2
},
r
oundIntTest
{
385
,
-
8
,
2
},
R
oundIntTest
{
383
,
-
8
,
1
},
r
oundIntTest
{
383
,
-
8
,
1
},
R
oundIntTest
{
1
,
100
,
1
<<
64
-
1
},
r
oundIntTest
{
1
,
100
,
1
<<
64
-
1
},
R
oundIntTest
{
1000
,
0
,
1000
},
r
oundIntTest
{
1000
,
0
,
1000
},
}
}
export
func
TestDecimalRoundedInteger
(
t
*
testing
.
T
)
{
export
func
TestDecimalRoundedInteger
(
t
*
testing
.
T
)
{
for
i
:=
0
;
i
<
len
(
roundinttests
);
i
++
{
for
i
:=
0
;
i
<
len
(
roundinttests
);
i
++
{
test
:=
roundinttests
[
i
];
test
:=
roundinttests
[
i
];
// TODO: should be able to use int := here.
// TODO: should be able to use int := here.
int1
:=
strconv
.
N
ewDecimal
(
test
.
i
)
.
Shift
(
test
.
shift
)
.
RoundedInteger
();
int1
:=
strconv
.
n
ewDecimal
(
test
.
i
)
.
Shift
(
test
.
shift
)
.
RoundedInteger
();
if
int1
!=
test
.
int
{
if
int1
!=
test
.
int
{
t
.
Errorf
(
"Decimal %v >> %v RoundedInteger = %v, want %v
\n
"
,
t
.
Errorf
(
"Decimal %v >> %v RoundedInteger = %v, want %v
\n
"
,
test
.
i
,
test
.
shift
,
int1
,
test
.
int
);
test
.
i
,
test
.
shift
,
int1
,
test
.
int
);
...
...
src/lib/strconv/fp_test.go
View file @
8a7cbadb
...
@@ -24,16 +24,16 @@ func pow2(i int) float64 {
...
@@ -24,16 +24,16 @@ func pow2(i int) float64 {
return
pow2
(
i
/
2
)
*
pow2
(
i
-
i
/
2
);
return
pow2
(
i
/
2
)
*
pow2
(
i
-
i
/
2
);
}
}
// Wrapper around strconv.
a
tof64. Handles dddddp+ddd (binary exponent)
// Wrapper around strconv.
A
tof64. Handles dddddp+ddd (binary exponent)
// itself, passes the rest on to strconv.
a
tof64.
// itself, passes the rest on to strconv.
A
tof64.
func
myatof64
(
s
string
)
(
f
float64
,
ok
bool
)
{
func
myatof64
(
s
string
)
(
f
float64
,
ok
bool
)
{
a
:=
strings
.
split
(
s
,
"p"
);
a
:=
strings
.
split
(
s
,
"p"
);
if
len
(
a
)
==
2
{
if
len
(
a
)
==
2
{
n
,
err
:=
strconv
.
a
toi64
(
a
[
0
]);
n
,
err
:=
strconv
.
A
toi64
(
a
[
0
]);
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
false
;
return
0
,
false
;
}
}
e
,
err1
:=
strconv
.
a
toi
(
a
[
1
]);
e
,
err1
:=
strconv
.
A
toi
(
a
[
1
]);
if
err1
!=
nil
{
if
err1
!=
nil
{
println
(
"bad e"
,
a
[
1
]);
println
(
"bad e"
,
a
[
1
]);
return
0
,
false
;
return
0
,
false
;
...
@@ -61,31 +61,31 @@ func myatof64(s string) (f float64, ok bool) {
...
@@ -61,31 +61,31 @@ func myatof64(s string) (f float64, ok bool) {
}
}
return
v
*
pow2
(
e
),
true
;
return
v
*
pow2
(
e
),
true
;
}
}
f1
,
err
:=
strconv
.
a
tof64
(
s
);
f1
,
err
:=
strconv
.
A
tof64
(
s
);
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
false
;
return
0
,
false
;
}
}
return
f1
,
true
;
return
f1
,
true
;
}
}
// Wrapper around strconv.
a
tof32. Handles dddddp+ddd (binary exponent)
// Wrapper around strconv.
A
tof32. Handles dddddp+ddd (binary exponent)
// itself, passes the rest on to strconv.
a
tof32.
// itself, passes the rest on to strconv.
A
tof32.
func
myatof32
(
s
string
)
(
f
float32
,
ok
bool
)
{
func
myatof32
(
s
string
)
(
f
float32
,
ok
bool
)
{
a
:=
strings
.
split
(
s
,
"p"
);
a
:=
strings
.
split
(
s
,
"p"
);
if
len
(
a
)
==
2
{
if
len
(
a
)
==
2
{
n
,
err
:=
strconv
.
a
toi
(
a
[
0
]);
n
,
err
:=
strconv
.
A
toi
(
a
[
0
]);
if
err
!=
nil
{
if
err
!=
nil
{
println
(
"bad n"
,
a
[
0
]);
println
(
"bad n"
,
a
[
0
]);
return
0
,
false
;
return
0
,
false
;
}
}
e
,
err1
:=
strconv
.
a
toi
(
a
[
1
]);
e
,
err1
:=
strconv
.
A
toi
(
a
[
1
]);
if
err1
!=
nil
{
if
err1
!=
nil
{
println
(
"bad p"
,
a
[
1
]);
println
(
"bad p"
,
a
[
1
]);
return
0
,
false
;
return
0
,
false
;
}
}
return
float32
(
float64
(
n
)
*
pow2
(
e
)),
true
;
return
float32
(
float64
(
n
)
*
pow2
(
e
)),
true
;
}
}
f1
,
err1
:=
strconv
.
a
tof32
(
s
);
f1
,
err1
:=
strconv
.
A
tof32
(
s
);
if
err1
!=
nil
{
if
err1
!=
nil
{
return
0
,
false
;
return
0
,
false
;
}
}
...
...
src/lib/strconv/ftoa.go
View file @
8a7cbadb
...
@@ -13,20 +13,20 @@ package strconv
...
@@ -13,20 +13,20 @@ package strconv
import
"strconv"
import
"strconv"
// TODO: move elsewhere?
// TODO: move elsewhere?
package
type
F
loatInfo
struct
{
type
f
loatInfo
struct
{
mantbits
uint
;
mantbits
uint
;
expbits
uint
;
expbits
uint
;
bias
int
;
bias
int
;
}
}
package
var
float32info
=
F
loatInfo
{
23
,
8
,
-
127
}
var
float32info
=
f
loatInfo
{
23
,
8
,
-
127
}
package
var
float64info
=
F
loatInfo
{
52
,
11
,
-
1023
}
var
float64info
=
f
loatInfo
{
52
,
11
,
-
1023
}
func
FmtB
(
neg
bool
,
mant
uint64
,
exp
int
,
flt
*
F
loatInfo
)
string
func
fmtB
(
neg
bool
,
mant
uint64
,
exp
int
,
flt
*
f
loatInfo
)
string
func
FmtE
(
neg
bool
,
d
*
D
ecimal
,
prec
int
)
string
func
fmtE
(
neg
bool
,
d
*
d
ecimal
,
prec
int
)
string
func
FmtF
(
neg
bool
,
d
*
D
ecimal
,
prec
int
)
string
func
fmtF
(
neg
bool
,
d
*
d
ecimal
,
prec
int
)
string
func
GenericFtoa
(
bits
uint64
,
fmt
byte
,
prec
int
,
flt
*
F
loatInfo
)
string
func
genericFtoa
(
bits
uint64
,
fmt
byte
,
prec
int
,
flt
*
f
loatInfo
)
string
func
M
ax
(
a
,
b
int
)
int
func
m
ax
(
a
,
b
int
)
int
func
RoundShortest
(
d
*
Decimal
,
mant
uint64
,
exp
int
,
flt
*
F
loatInfo
)
func
roundShortest
(
d
*
decimal
,
mant
uint64
,
exp
int
,
flt
*
f
loatInfo
)
func
floatsize
()
int
{
func
floatsize
()
int
{
// Figure out whether float is float32 or float64.
// Figure out whether float is float32 or float64.
...
@@ -40,22 +40,22 @@ func floatsize() int {
...
@@ -40,22 +40,22 @@ func floatsize() int {
}
}
export
var
FloatSize
=
floatsize
()
export
var
FloatSize
=
floatsize
()
export
func
f
toa32
(
f
float32
,
fmt
byte
,
prec
int
)
string
{
export
func
F
toa32
(
f
float32
,
fmt
byte
,
prec
int
)
string
{
return
G
enericFtoa
(
uint64
(
sys
.
float32bits
(
f
)),
fmt
,
prec
,
&
float32info
);
return
g
enericFtoa
(
uint64
(
sys
.
float32bits
(
f
)),
fmt
,
prec
,
&
float32info
);
}
}
export
func
f
toa64
(
f
float64
,
fmt
byte
,
prec
int
)
string
{
export
func
F
toa64
(
f
float64
,
fmt
byte
,
prec
int
)
string
{
return
G
enericFtoa
(
sys
.
float64bits
(
f
),
fmt
,
prec
,
&
float64info
);
return
g
enericFtoa
(
sys
.
float64bits
(
f
),
fmt
,
prec
,
&
float64info
);
}
}
export
func
f
toa
(
f
float
,
fmt
byte
,
prec
int
)
string
{
export
func
F
toa
(
f
float
,
fmt
byte
,
prec
int
)
string
{
if
FloatSize
==
32
{
if
FloatSize
==
32
{
return
f
toa32
(
float32
(
f
),
fmt
,
prec
);
return
F
toa32
(
float32
(
f
),
fmt
,
prec
);
}
}
return
f
toa64
(
float64
(
f
),
fmt
,
prec
);
return
F
toa64
(
float64
(
f
),
fmt
,
prec
);
}
}
func
GenericFtoa
(
bits
uint64
,
fmt
byte
,
prec
int
,
flt
*
F
loatInfo
)
string
{
func
genericFtoa
(
bits
uint64
,
fmt
byte
,
prec
int
,
flt
*
f
loatInfo
)
string
{
neg
:=
bits
>>
flt
.
expbits
>>
flt
.
mantbits
!=
0
;
neg
:=
bits
>>
flt
.
expbits
>>
flt
.
mantbits
!=
0
;
exp
:=
int
(
bits
>>
flt
.
mantbits
)
&
(
1
<<
flt
.
expbits
-
1
);
exp
:=
int
(
bits
>>
flt
.
mantbits
)
&
(
1
<<
flt
.
expbits
-
1
);
mant
:=
bits
&
(
uint64
(
1
)
<<
flt
.
mantbits
-
1
);
mant
:=
bits
&
(
uint64
(
1
)
<<
flt
.
mantbits
-
1
);
...
@@ -83,26 +83,26 @@ func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string {
...
@@ -83,26 +83,26 @@ func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string {
// Pick off easy binary format.
// Pick off easy binary format.
if
fmt
==
'b'
{
if
fmt
==
'b'
{
return
F
mtB
(
neg
,
mant
,
exp
,
flt
);
return
f
mtB
(
neg
,
mant
,
exp
,
flt
);
}
}
// Create exact decimal representation.
// Create exact decimal representation.
// The shift is exp - flt.mantbits because mant is a 1-bit integer
// The shift is exp - flt.mantbits because mant is a 1-bit integer
// followed by a flt.mantbits fraction, and we are treating it as
// followed by a flt.mantbits fraction, and we are treating it as
// a 1+flt.mantbits-bit integer.
// a 1+flt.mantbits-bit integer.
d
:=
N
ewDecimal
(
mant
)
.
Shift
(
exp
-
int
(
flt
.
mantbits
));
d
:=
n
ewDecimal
(
mant
)
.
Shift
(
exp
-
int
(
flt
.
mantbits
));
// Round appropriately.
// Round appropriately.
// Negative precision means "only as much as needed to be exact."
// Negative precision means "only as much as needed to be exact."
shortest
:=
false
;
shortest
:=
false
;
if
prec
<
0
{
if
prec
<
0
{
shortest
=
true
;
shortest
=
true
;
R
oundShortest
(
d
,
mant
,
exp
,
flt
);
r
oundShortest
(
d
,
mant
,
exp
,
flt
);
switch
fmt
{
switch
fmt
{
case
'e'
:
case
'e'
:
prec
=
d
.
nd
-
1
;
prec
=
d
.
nd
-
1
;
case
'f'
:
case
'f'
:
prec
=
M
ax
(
d
.
nd
-
d
.
dp
,
0
);
prec
=
m
ax
(
d
.
nd
-
d
.
dp
,
0
);
case
'g'
:
case
'g'
:
prec
=
d
.
nd
;
prec
=
d
.
nd
;
}
}
...
@@ -122,9 +122,9 @@ func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string {
...
@@ -122,9 +122,9 @@ func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string {
switch
fmt
{
switch
fmt
{
case
'e'
:
case
'e'
:
return
F
mtE
(
neg
,
d
,
prec
);
return
f
mtE
(
neg
,
d
,
prec
);
case
'f'
:
case
'f'
:
return
F
mtF
(
neg
,
d
,
prec
);
return
f
mtF
(
neg
,
d
,
prec
);
case
'g'
:
case
'g'
:
// trailing zeros are removed.
// trailing zeros are removed.
if
prec
>
d
.
nd
{
if
prec
>
d
.
nd
{
...
@@ -139,9 +139,9 @@ func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string {
...
@@ -139,9 +139,9 @@ func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string {
}
}
exp
:=
d
.
dp
-
1
;
exp
:=
d
.
dp
-
1
;
if
exp
<
-
4
||
exp
>=
eprec
{
if
exp
<
-
4
||
exp
>=
eprec
{
return
F
mtE
(
neg
,
d
,
prec
-
1
);
return
f
mtE
(
neg
,
d
,
prec
-
1
);
}
}
return
FmtF
(
neg
,
d
,
M
ax
(
prec
-
d
.
dp
,
0
));
return
fmtF
(
neg
,
d
,
m
ax
(
prec
-
d
.
dp
,
0
));
}
}
return
"%"
+
string
(
fmt
);
return
"%"
+
string
(
fmt
);
...
@@ -150,7 +150,7 @@ func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string {
...
@@ -150,7 +150,7 @@ func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string {
// Round d (= mant * 2^exp) to the shortest number of digits
// Round d (= mant * 2^exp) to the shortest number of digits
// that will let the original floating point value be precisely
// that will let the original floating point value be precisely
// reconstructed. Size is original floating point size (64 or 32).
// reconstructed. Size is original floating point size (64 or 32).
func
RoundShortest
(
d
*
Decimal
,
mant
uint64
,
exp
int
,
flt
*
F
loatInfo
)
{
func
roundShortest
(
d
*
decimal
,
mant
uint64
,
exp
int
,
flt
*
f
loatInfo
)
{
// If mantissa is zero, the number is zero; stop now.
// If mantissa is zero, the number is zero; stop now.
if
mant
==
0
{
if
mant
==
0
{
d
.
nd
=
0
;
d
.
nd
=
0
;
...
@@ -169,7 +169,7 @@ func RoundShortest(d *Decimal, mant uint64, exp int, flt *FloatInfo) {
...
@@ -169,7 +169,7 @@ func RoundShortest(d *Decimal, mant uint64, exp int, flt *FloatInfo) {
// d = mant << (exp - mantbits)
// d = mant << (exp - mantbits)
// Next highest floating point number is mant+1 << exp-mantbits.
// Next highest floating point number is mant+1 << exp-mantbits.
// Our upper bound is halfway inbetween, mant*2+1 << exp-mantbits-1.
// Our upper bound is halfway inbetween, mant*2+1 << exp-mantbits-1.
upper
:=
N
ewDecimal
(
mant
*
2
+
1
)
.
Shift
(
exp
-
int
(
flt
.
mantbits
)
-
1
);
upper
:=
n
ewDecimal
(
mant
*
2
+
1
)
.
Shift
(
exp
-
int
(
flt
.
mantbits
)
-
1
);
// d = mant << (exp - mantbits)
// d = mant << (exp - mantbits)
// Next lowest floating point number is mant-1 << exp-mantbits,
// Next lowest floating point number is mant-1 << exp-mantbits,
...
@@ -187,7 +187,7 @@ func RoundShortest(d *Decimal, mant uint64, exp int, flt *FloatInfo) {
...
@@ -187,7 +187,7 @@ func RoundShortest(d *Decimal, mant uint64, exp int, flt *FloatInfo) {
mantlo
=
mant
*
2
-
1
;
mantlo
=
mant
*
2
-
1
;
explo
=
exp
-
1
;
explo
=
exp
-
1
;
}
}
lower
:=
N
ewDecimal
(
mantlo
*
2
+
1
)
.
Shift
(
explo
-
int
(
flt
.
mantbits
)
-
1
);
lower
:=
n
ewDecimal
(
mantlo
*
2
+
1
)
.
Shift
(
explo
-
int
(
flt
.
mantbits
)
-
1
);
// The upper and lower bounds are possible outputs only if
// The upper and lower bounds are possible outputs only if
// the original mantissa is even, so that IEEE round-to-even
// the original mantissa is even, so that IEEE round-to-even
...
@@ -235,8 +235,8 @@ func RoundShortest(d *Decimal, mant uint64, exp int, flt *FloatInfo) {
...
@@ -235,8 +235,8 @@ func RoundShortest(d *Decimal, mant uint64, exp int, flt *FloatInfo) {
}
}
// %e: -d.ddddde±dd
// %e: -d.ddddde±dd
func
FmtE
(
neg
bool
,
d
*
D
ecimal
,
prec
int
)
string
{
func
fmtE
(
neg
bool
,
d
*
d
ecimal
,
prec
int
)
string
{
buf
:=
make
([]
byte
,
3
+
M
ax
(
prec
,
0
)
+
30
);
// "-0." + prec digits + exp
buf
:=
make
([]
byte
,
3
+
m
ax
(
prec
,
0
)
+
30
);
// "-0." + prec digits + exp
w
:=
0
;
// write index
w
:=
0
;
// write index
// sign
// sign
...
@@ -305,8 +305,8 @@ func FmtE(neg bool, d *Decimal, prec int) string {
...
@@ -305,8 +305,8 @@ func FmtE(neg bool, d *Decimal, prec int) string {
}
}
// %f: -ddddddd.ddddd
// %f: -ddddddd.ddddd
func
FmtF
(
neg
bool
,
d
*
D
ecimal
,
prec
int
)
string
{
func
fmtF
(
neg
bool
,
d
*
d
ecimal
,
prec
int
)
string
{
buf
:=
make
([]
byte
,
1
+
Max
(
d
.
dp
,
1
)
+
1
+
M
ax
(
prec
,
0
));
buf
:=
make
([]
byte
,
1
+
max
(
d
.
dp
,
1
)
+
1
+
m
ax
(
prec
,
0
));
w
:=
0
;
w
:=
0
;
// sign
// sign
...
@@ -349,7 +349,7 @@ func FmtF(neg bool, d *Decimal, prec int) string {
...
@@ -349,7 +349,7 @@ func FmtF(neg bool, d *Decimal, prec int) string {
}
}
// %b: -ddddddddp+ddd
// %b: -ddddddddp+ddd
func
FmtB
(
neg
bool
,
mant
uint64
,
exp
int
,
flt
*
F
loatInfo
)
string
{
func
fmtB
(
neg
bool
,
mant
uint64
,
exp
int
,
flt
*
f
loatInfo
)
string
{
var
buf
[
50
]
byte
;
var
buf
[
50
]
byte
;
w
:=
len
(
buf
);
w
:=
len
(
buf
);
exp
-=
int
(
flt
.
mantbits
);
exp
-=
int
(
flt
.
mantbits
);
...
@@ -383,7 +383,7 @@ func FmtB(neg bool, mant uint64, exp int, flt *FloatInfo) string {
...
@@ -383,7 +383,7 @@ func FmtB(neg bool, mant uint64, exp int, flt *FloatInfo) string {
return
string
(
buf
[
w
:
len
(
buf
)]);
return
string
(
buf
[
w
:
len
(
buf
)]);
}
}
func
M
ax
(
a
,
b
int
)
int
{
func
m
ax
(
a
,
b
int
)
int
{
if
a
>
b
{
if
a
>
b
{
return
a
;
return
a
;
}
}
...
...
src/lib/strconv/ftoa_test.go
View file @
8a7cbadb
...
@@ -9,7 +9,7 @@ import (
...
@@ -9,7 +9,7 @@ import (
"testing"
"testing"
)
)
type
F
toaTest
struct
{
type
f
toaTest
struct
{
f
float64
;
f
float64
;
fmt
byte
;
fmt
byte
;
prec
int
;
prec
int
;
...
@@ -19,83 +19,83 @@ type FtoaTest struct {
...
@@ -19,83 +19,83 @@ type FtoaTest struct {
func
fdiv
(
a
,
b
float64
)
float64
{
return
a
/
b
}
// keep compiler in the dark
func
fdiv
(
a
,
b
float64
)
float64
{
return
a
/
b
}
// keep compiler in the dark
const
(
const
(
B
elow1e23
=
99999999999999974834176
;
b
elow1e23
=
99999999999999974834176
;
A
bove1e23
=
100000000000000008388608
;
a
bove1e23
=
100000000000000008388608
;
)
)
var
ftoatests
=
[]
F
toaTest
{
var
ftoatests
=
[]
f
toaTest
{
F
toaTest
{
1
,
'e'
,
5
,
"1.00000e+00"
},
f
toaTest
{
1
,
'e'
,
5
,
"1.00000e+00"
},
F
toaTest
{
1
,
'f'
,
5
,
"1.00000"
},
f
toaTest
{
1
,
'f'
,
5
,
"1.00000"
},
F
toaTest
{
1
,
'g'
,
5
,
"1"
},
f
toaTest
{
1
,
'g'
,
5
,
"1"
},
F
toaTest
{
1
,
'g'
,
-
1
,
"1"
},
f
toaTest
{
1
,
'g'
,
-
1
,
"1"
},
F
toaTest
{
20
,
'g'
,
-
1
,
"20"
},
f
toaTest
{
20
,
'g'
,
-
1
,
"20"
},
F
toaTest
{
1234567.8
,
'g'
,
-
1
,
"1.2345678e+06"
},
f
toaTest
{
1234567.8
,
'g'
,
-
1
,
"1.2345678e+06"
},
F
toaTest
{
200000
,
'g'
,
-
1
,
"200000"
},
f
toaTest
{
200000
,
'g'
,
-
1
,
"200000"
},
F
toaTest
{
2000000
,
'g'
,
-
1
,
"2e+06"
},
f
toaTest
{
2000000
,
'g'
,
-
1
,
"2e+06"
},
F
toaTest
{
0
,
'e'
,
5
,
"0.00000e+00"
},
f
toaTest
{
0
,
'e'
,
5
,
"0.00000e+00"
},
F
toaTest
{
0
,
'f'
,
5
,
"0.00000"
},
f
toaTest
{
0
,
'f'
,
5
,
"0.00000"
},
F
toaTest
{
0
,
'g'
,
5
,
"0"
},
f
toaTest
{
0
,
'g'
,
5
,
"0"
},
F
toaTest
{
0
,
'g'
,
-
1
,
"0"
},
f
toaTest
{
0
,
'g'
,
-
1
,
"0"
},
F
toaTest
{
-
1
,
'e'
,
5
,
"-1.00000e+00"
},
f
toaTest
{
-
1
,
'e'
,
5
,
"-1.00000e+00"
},
F
toaTest
{
-
1
,
'f'
,
5
,
"-1.00000"
},
f
toaTest
{
-
1
,
'f'
,
5
,
"-1.00000"
},
F
toaTest
{
-
1
,
'g'
,
5
,
"-1"
},
f
toaTest
{
-
1
,
'g'
,
5
,
"-1"
},
F
toaTest
{
-
1
,
'g'
,
-
1
,
"-1"
},
f
toaTest
{
-
1
,
'g'
,
-
1
,
"-1"
},
F
toaTest
{
12
,
'e'
,
5
,
"1.20000e+01"
},
f
toaTest
{
12
,
'e'
,
5
,
"1.20000e+01"
},
F
toaTest
{
12
,
'f'
,
5
,
"12.00000"
},
f
toaTest
{
12
,
'f'
,
5
,
"12.00000"
},
F
toaTest
{
12
,
'g'
,
5
,
"12"
},
f
toaTest
{
12
,
'g'
,
5
,
"12"
},
F
toaTest
{
12
,
'g'
,
-
1
,
"12"
},
f
toaTest
{
12
,
'g'
,
-
1
,
"12"
},
F
toaTest
{
123456700
,
'e'
,
5
,
"1.23457e+08"
},
f
toaTest
{
123456700
,
'e'
,
5
,
"1.23457e+08"
},
F
toaTest
{
123456700
,
'f'
,
5
,
"123456700.00000"
},
f
toaTest
{
123456700
,
'f'
,
5
,
"123456700.00000"
},
F
toaTest
{
123456700
,
'g'
,
5
,
"1.2346e+08"
},
f
toaTest
{
123456700
,
'g'
,
5
,
"1.2346e+08"
},
F
toaTest
{
123456700
,
'g'
,
-
1
,
"1.234567e+08"
},
f
toaTest
{
123456700
,
'g'
,
-
1
,
"1.234567e+08"
},
F
toaTest
{
1.2345e6
,
'e'
,
5
,
"1.23450e+06"
},
f
toaTest
{
1.2345e6
,
'e'
,
5
,
"1.23450e+06"
},
F
toaTest
{
1.2345e6
,
'f'
,
5
,
"1234500.00000"
},
f
toaTest
{
1.2345e6
,
'f'
,
5
,
"1234500.00000"
},
F
toaTest
{
1.2345e6
,
'g'
,
5
,
"1.2345e+06"
},
f
toaTest
{
1.2345e6
,
'g'
,
5
,
"1.2345e+06"
},
F
toaTest
{
1e23
,
'e'
,
17
,
"9.99999999999999916e+22"
},
f
toaTest
{
1e23
,
'e'
,
17
,
"9.99999999999999916e+22"
},
F
toaTest
{
1e23
,
'f'
,
17
,
"99999999999999991611392.00000000000000000"
},
f
toaTest
{
1e23
,
'f'
,
17
,
"99999999999999991611392.00000000000000000"
},
F
toaTest
{
1e23
,
'g'
,
17
,
"9.9999999999999992e+22"
},
f
toaTest
{
1e23
,
'g'
,
17
,
"9.9999999999999992e+22"
},
F
toaTest
{
1e23
,
'e'
,
-
1
,
"1e+23"
},
f
toaTest
{
1e23
,
'e'
,
-
1
,
"1e+23"
},
F
toaTest
{
1e23
,
'f'
,
-
1
,
"100000000000000000000000"
},
f
toaTest
{
1e23
,
'f'
,
-
1
,
"100000000000000000000000"
},
F
toaTest
{
1e23
,
'g'
,
-
1
,
"1e+23"
},
f
toaTest
{
1e23
,
'g'
,
-
1
,
"1e+23"
},
FtoaTest
{
B
elow1e23
,
'e'
,
17
,
"9.99999999999999748e+22"
},
ftoaTest
{
b
elow1e23
,
'e'
,
17
,
"9.99999999999999748e+22"
},
FtoaTest
{
B
elow1e23
,
'f'
,
17
,
"99999999999999974834176.00000000000000000"
},
ftoaTest
{
b
elow1e23
,
'f'
,
17
,
"99999999999999974834176.00000000000000000"
},
FtoaTest
{
B
elow1e23
,
'g'
,
17
,
"9.9999999999999975e+22"
},
ftoaTest
{
b
elow1e23
,
'g'
,
17
,
"9.9999999999999975e+22"
},
FtoaTest
{
B
elow1e23
,
'e'
,
-
1
,
"9.999999999999997e+22"
},
ftoaTest
{
b
elow1e23
,
'e'
,
-
1
,
"9.999999999999997e+22"
},
FtoaTest
{
B
elow1e23
,
'f'
,
-
1
,
"99999999999999970000000"
},
ftoaTest
{
b
elow1e23
,
'f'
,
-
1
,
"99999999999999970000000"
},
FtoaTest
{
B
elow1e23
,
'g'
,
-
1
,
"9.999999999999997e+22"
},
ftoaTest
{
b
elow1e23
,
'g'
,
-
1
,
"9.999999999999997e+22"
},
FtoaTest
{
A
bove1e23
,
'e'
,
17
,
"1.00000000000000008e+23"
},
ftoaTest
{
a
bove1e23
,
'e'
,
17
,
"1.00000000000000008e+23"
},
FtoaTest
{
A
bove1e23
,
'f'
,
17
,
"100000000000000008388608.00000000000000000"
},
ftoaTest
{
a
bove1e23
,
'f'
,
17
,
"100000000000000008388608.00000000000000000"
},
FtoaTest
{
A
bove1e23
,
'g'
,
17
,
"1.0000000000000001e+23"
},
ftoaTest
{
a
bove1e23
,
'g'
,
17
,
"1.0000000000000001e+23"
},
FtoaTest
{
A
bove1e23
,
'e'
,
-
1
,
"1.0000000000000001e+23"
},
ftoaTest
{
a
bove1e23
,
'e'
,
-
1
,
"1.0000000000000001e+23"
},
FtoaTest
{
A
bove1e23
,
'f'
,
-
1
,
"100000000000000010000000"
},
ftoaTest
{
a
bove1e23
,
'f'
,
-
1
,
"100000000000000010000000"
},
FtoaTest
{
A
bove1e23
,
'g'
,
-
1
,
"1.0000000000000001e+23"
},
ftoaTest
{
a
bove1e23
,
'g'
,
-
1
,
"1.0000000000000001e+23"
},
F
toaTest
{
fdiv
(
5e-304
,
1e20
),
'g'
,
-
1
,
"5e-324"
},
f
toaTest
{
fdiv
(
5e-304
,
1e20
),
'g'
,
-
1
,
"5e-324"
},
F
toaTest
{
fdiv
(
-
5e-304
,
1e20
),
'g'
,
-
1
,
"-5e-324"
},
f
toaTest
{
fdiv
(
-
5e-304
,
1e20
),
'g'
,
-
1
,
"-5e-324"
},
F
toaTest
{
32
,
'g'
,
-
1
,
"32"
},
f
toaTest
{
32
,
'g'
,
-
1
,
"32"
},
F
toaTest
{
32
,
'g'
,
0
,
"3e+01"
},
f
toaTest
{
32
,
'g'
,
0
,
"3e+01"
},
F
toaTest
{
100
,
'x'
,
-
1
,
"%x"
},
f
toaTest
{
100
,
'x'
,
-
1
,
"%x"
},
F
toaTest
{
sys
.
NaN
(),
'g'
,
-
1
,
"NaN"
},
f
toaTest
{
sys
.
NaN
(),
'g'
,
-
1
,
"NaN"
},
F
toaTest
{
-
sys
.
NaN
(),
'g'
,
-
1
,
"NaN"
},
f
toaTest
{
-
sys
.
NaN
(),
'g'
,
-
1
,
"NaN"
},
F
toaTest
{
sys
.
Inf
(
0
),
'g'
,
-
1
,
"+Inf"
},
f
toaTest
{
sys
.
Inf
(
0
),
'g'
,
-
1
,
"+Inf"
},
F
toaTest
{
sys
.
Inf
(
-
1
),
'g'
,
-
1
,
"-Inf"
},
f
toaTest
{
sys
.
Inf
(
-
1
),
'g'
,
-
1
,
"-Inf"
},
F
toaTest
{
-
sys
.
Inf
(
0
),
'g'
,
-
1
,
"-Inf"
},
f
toaTest
{
-
sys
.
Inf
(
0
),
'g'
,
-
1
,
"-Inf"
},
F
toaTest
{
-
1
,
'b'
,
-
1
,
"-4503599627370496p-52"
},
f
toaTest
{
-
1
,
'b'
,
-
1
,
"-4503599627370496p-52"
},
}
}
export
func
TestFtoa
(
t
*
testing
.
T
)
{
export
func
TestFtoa
(
t
*
testing
.
T
)
{
...
@@ -104,12 +104,12 @@ export func TestFtoa(t *testing.T) {
...
@@ -104,12 +104,12 @@ export func TestFtoa(t *testing.T) {
}
}
for
i
:=
0
;
i
<
len
(
ftoatests
);
i
++
{
for
i
:=
0
;
i
<
len
(
ftoatests
);
i
++
{
test
:=
&
ftoatests
[
i
];
test
:=
&
ftoatests
[
i
];
s
:=
strconv
.
f
toa64
(
test
.
f
,
test
.
fmt
,
test
.
prec
);
s
:=
strconv
.
F
toa64
(
test
.
f
,
test
.
fmt
,
test
.
prec
);
if
s
!=
test
.
s
{
if
s
!=
test
.
s
{
t
.
Error
(
"test"
,
test
.
f
,
string
(
test
.
fmt
),
test
.
prec
,
"want"
,
test
.
s
,
"got"
,
s
);
t
.
Error
(
"test"
,
test
.
f
,
string
(
test
.
fmt
),
test
.
prec
,
"want"
,
test
.
s
,
"got"
,
s
);
}
}
if
float64
(
float32
(
test
.
f
))
==
test
.
f
&&
test
.
fmt
!=
'b'
{
if
float64
(
float32
(
test
.
f
))
==
test
.
f
&&
test
.
fmt
!=
'b'
{
s
:=
strconv
.
f
toa32
(
float32
(
test
.
f
),
test
.
fmt
,
test
.
prec
);
s
:=
strconv
.
F
toa32
(
float32
(
test
.
f
),
test
.
fmt
,
test
.
prec
);
if
s
!=
test
.
s
{
if
s
!=
test
.
s
{
t
.
Error
(
"test32"
,
test
.
f
,
string
(
test
.
fmt
),
test
.
prec
,
"want"
,
test
.
s
,
"got"
,
s
);
t
.
Error
(
"test32"
,
test
.
f
,
string
(
test
.
fmt
),
test
.
prec
,
"want"
,
test
.
s
,
"got"
,
s
);
}
}
...
...
src/lib/strconv/itoa.go
View file @
8a7cbadb
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
package
strconv
package
strconv
export
func
i
toa64
(
i
int64
)
string
{
export
func
I
toa64
(
i
int64
)
string
{
if
i
==
0
{
if
i
==
0
{
return
"0"
return
"0"
}
}
...
@@ -29,10 +29,9 @@ export func itoa64(i int64) string {
...
@@ -29,10 +29,9 @@ export func itoa64(i int64) string {
}
}
return
string
(
b
[
bp
:
len
(
b
)])
return
string
(
b
[
bp
:
len
(
b
)])
//return string((&b)[bp:len(b)])
}
}
export
func
i
toa
(
i
int
)
string
{
export
func
I
toa
(
i
int
)
string
{
return
i
toa64
(
int64
(
i
));
return
I
toa64
(
int64
(
i
));
}
}
src/lib/strconv/itoa_test.go
View file @
8a7cbadb
...
@@ -11,47 +11,47 @@ import (
...
@@ -11,47 +11,47 @@ import (
"testing"
;
"testing"
;
)
)
type
I
toa64Test
struct
{
type
i
toa64Test
struct
{
in
int64
;
in
int64
;
out
string
;
out
string
;
}
}
var
itoa64tests
=
[]
I
toa64Test
{
var
itoa64tests
=
[]
i
toa64Test
{
I
toa64Test
{
0
,
"0"
},
i
toa64Test
{
0
,
"0"
},
I
toa64Test
{
1
,
"1"
},
i
toa64Test
{
1
,
"1"
},
I
toa64Test
{
-
1
,
"-1"
},
i
toa64Test
{
-
1
,
"-1"
},
I
toa64Test
{
12345678
,
"12345678"
},
i
toa64Test
{
12345678
,
"12345678"
},
I
toa64Test
{
-
987654321
,
"-987654321"
},
i
toa64Test
{
-
987654321
,
"-987654321"
},
I
toa64Test
{
1
<<
31
-
1
,
"2147483647"
},
i
toa64Test
{
1
<<
31
-
1
,
"2147483647"
},
I
toa64Test
{
-
1
<<
31
+
1
,
"-2147483647"
},
i
toa64Test
{
-
1
<<
31
+
1
,
"-2147483647"
},
I
toa64Test
{
1
<<
31
,
"2147483648"
},
i
toa64Test
{
1
<<
31
,
"2147483648"
},
I
toa64Test
{
-
1
<<
31
,
"-2147483648"
},
i
toa64Test
{
-
1
<<
31
,
"-2147483648"
},
I
toa64Test
{
1
<<
31
+
1
,
"2147483649"
},
i
toa64Test
{
1
<<
31
+
1
,
"2147483649"
},
I
toa64Test
{
-
1
<<
31
-
1
,
"-2147483649"
},
i
toa64Test
{
-
1
<<
31
-
1
,
"-2147483649"
},
I
toa64Test
{
1
<<
32
-
1
,
"4294967295"
},
i
toa64Test
{
1
<<
32
-
1
,
"4294967295"
},
I
toa64Test
{
-
1
<<
32
+
1
,
"-4294967295"
},
i
toa64Test
{
-
1
<<
32
+
1
,
"-4294967295"
},
I
toa64Test
{
1
<<
32
,
"4294967296"
},
i
toa64Test
{
1
<<
32
,
"4294967296"
},
I
toa64Test
{
-
1
<<
32
,
"-4294967296"
},
i
toa64Test
{
-
1
<<
32
,
"-4294967296"
},
I
toa64Test
{
1
<<
32
+
1
,
"4294967297"
},
i
toa64Test
{
1
<<
32
+
1
,
"4294967297"
},
I
toa64Test
{
-
1
<<
32
-
1
,
"-4294967297"
},
i
toa64Test
{
-
1
<<
32
-
1
,
"-4294967297"
},
I
toa64Test
{
1
<<
50
,
"1125899906842624"
},
i
toa64Test
{
1
<<
50
,
"1125899906842624"
},
I
toa64Test
{
1
<<
63
-
1
,
"9223372036854775807"
},
i
toa64Test
{
1
<<
63
-
1
,
"9223372036854775807"
},
I
toa64Test
{
-
1
<<
63
+
1
,
"-9223372036854775807"
},
i
toa64Test
{
-
1
<<
63
+
1
,
"-9223372036854775807"
},
I
toa64Test
{
-
1
<<
63
,
"-9223372036854775808"
},
i
toa64Test
{
-
1
<<
63
,
"-9223372036854775808"
},
}
}
export
func
TestItoa
(
t
*
testing
.
T
)
{
export
func
TestItoa
(
t
*
testing
.
T
)
{
for
i
:=
0
;
i
<
len
(
itoa64tests
);
i
++
{
for
i
:=
0
;
i
<
len
(
itoa64tests
);
i
++
{
test
:=
itoa64tests
[
i
];
test
:=
itoa64tests
[
i
];
s
:=
strconv
.
i
toa64
(
test
.
in
);
s
:=
strconv
.
I
toa64
(
test
.
in
);
if
s
!=
test
.
out
{
if
s
!=
test
.
out
{
t
.
Error
(
"strconv.
i
toa64(%v) = %v want %v
\n
"
,
t
.
Error
(
"strconv.
I
toa64(%v) = %v want %v
\n
"
,
test
.
in
,
s
,
test
.
out
);
test
.
in
,
s
,
test
.
out
);
}
}
if
int64
(
int
(
test
.
in
))
==
test
.
in
{
if
int64
(
int
(
test
.
in
))
==
test
.
in
{
s
:=
strconv
.
i
toa
(
int
(
test
.
in
));
s
:=
strconv
.
I
toa
(
int
(
test
.
in
));
if
s
!=
test
.
out
{
if
s
!=
test
.
out
{
t
.
Error
(
"strconv.
i
toa(%v) = %v want %v
\n
"
,
t
.
Error
(
"strconv.
I
toa(%v) = %v want %v
\n
"
,
test
.
in
,
s
,
test
.
out
);
test
.
in
,
s
,
test
.
out
);
}
}
}
}
...
@@ -59,17 +59,17 @@ export func TestItoa(t *testing.T) {
...
@@ -59,17 +59,17 @@ export func TestItoa(t *testing.T) {
}
}
// TODO: Use once there is a strconv.uitoa
// TODO: Use once there is a strconv.uitoa
type
U
itoa64Test
struct
{
type
u
itoa64Test
struct
{
in
uint64
;
in
uint64
;
out
string
;
out
string
;
}
}
// TODO: should be able to call this atoui64tests.
// TODO: should be able to call this atoui64tests.
var
uitoa64tests
=
[]
U
itoa64Test
{
var
uitoa64tests
=
[]
u
itoa64Test
{
U
itoa64Test
{
1
<<
63
-
1
,
"9223372036854775807"
},
u
itoa64Test
{
1
<<
63
-
1
,
"9223372036854775807"
},
U
itoa64Test
{
1
<<
63
,
"9223372036854775808"
},
u
itoa64Test
{
1
<<
63
,
"9223372036854775808"
},
U
itoa64Test
{
1
<<
63
+
1
,
"9223372036854775809"
},
u
itoa64Test
{
1
<<
63
+
1
,
"9223372036854775809"
},
U
itoa64Test
{
1
<<
64
-
2
,
"18446744073709551614"
},
u
itoa64Test
{
1
<<
64
-
2
,
"18446744073709551614"
},
U
itoa64Test
{
1
<<
64
-
1
,
"18446744073709551615"
},
u
itoa64Test
{
1
<<
64
-
1
,
"18446744073709551615"
},
}
}
src/lib/strconv/quote_test.go
View file @
8a7cbadb
...
@@ -9,18 +9,18 @@ import (
...
@@ -9,18 +9,18 @@ import (
"testing"
;
"testing"
;
)
)
type
Q
uoteTest
struct
{
type
q
uoteTest
struct
{
in
string
;
in
string
;
out
string
;
out
string
;
}
}
var
quotetests
=
[]
Q
uoteTest
{
var
quotetests
=
[]
q
uoteTest
{
Q
uoteTest
{
"
\a\b\f\r\n\t\v
"
,
`"\a\b\f\r\n\t\v"`
},
q
uoteTest
{
"
\a\b\f\r\n\t\v
"
,
`"\a\b\f\r\n\t\v"`
},
Q
uoteTest
{
"
\\
"
,
`"\\"`
},
q
uoteTest
{
"
\\
"
,
`"\\"`
},
Q
uoteTest
{
"abc
\xff
def"
,
`"abc\xffdef"`
},
q
uoteTest
{
"abc
\xff
def"
,
`"abc\xffdef"`
},
Q
uoteTest
{
"
\u263a
"
,
`"\u263a"`
},
q
uoteTest
{
"
\u263a
"
,
`"\u263a"`
},
Q
uoteTest
{
"
\U0010ffff
"
,
`"\U0010ffff"`
},
q
uoteTest
{
"
\U0010ffff
"
,
`"\U0010ffff"`
},
Q
uoteTest
{
"
\x04
"
,
`"\x04"`
},
q
uoteTest
{
"
\x04
"
,
`"\x04"`
},
}
}
export
func
TestQuote
(
t
*
testing
.
T
)
{
export
func
TestQuote
(
t
*
testing
.
T
)
{
...
@@ -32,50 +32,50 @@ export func TestQuote(t *testing.T) {
...
@@ -32,50 +32,50 @@ export func TestQuote(t *testing.T) {
}
}
}
}
type
C
anBackquoteTest
struct
{
type
c
anBackquoteTest
struct
{
in
string
;
in
string
;
out
bool
;
out
bool
;
}
}
var
canbackquotetests
=
[]
C
anBackquoteTest
{
var
canbackquotetests
=
[]
c
anBackquoteTest
{
C
anBackquoteTest
{
"`"
,
false
},
c
anBackquoteTest
{
"`"
,
false
},
C
anBackquoteTest
{
string
(
0
),
false
},
c
anBackquoteTest
{
string
(
0
),
false
},
C
anBackquoteTest
{
string
(
1
),
false
},
c
anBackquoteTest
{
string
(
1
),
false
},
C
anBackquoteTest
{
string
(
2
),
false
},
c
anBackquoteTest
{
string
(
2
),
false
},
C
anBackquoteTest
{
string
(
3
),
false
},
c
anBackquoteTest
{
string
(
3
),
false
},
C
anBackquoteTest
{
string
(
4
),
false
},
c
anBackquoteTest
{
string
(
4
),
false
},
C
anBackquoteTest
{
string
(
5
),
false
},
c
anBackquoteTest
{
string
(
5
),
false
},
C
anBackquoteTest
{
string
(
6
),
false
},
c
anBackquoteTest
{
string
(
6
),
false
},
C
anBackquoteTest
{
string
(
7
),
false
},
c
anBackquoteTest
{
string
(
7
),
false
},
C
anBackquoteTest
{
string
(
8
),
false
},
c
anBackquoteTest
{
string
(
8
),
false
},
C
anBackquoteTest
{
string
(
9
),
false
},
c
anBackquoteTest
{
string
(
9
),
false
},
C
anBackquoteTest
{
string
(
10
),
false
},
c
anBackquoteTest
{
string
(
10
),
false
},
C
anBackquoteTest
{
string
(
11
),
false
},
c
anBackquoteTest
{
string
(
11
),
false
},
C
anBackquoteTest
{
string
(
12
),
false
},
c
anBackquoteTest
{
string
(
12
),
false
},
C
anBackquoteTest
{
string
(
13
),
false
},
c
anBackquoteTest
{
string
(
13
),
false
},
C
anBackquoteTest
{
string
(
14
),
false
},
c
anBackquoteTest
{
string
(
14
),
false
},
C
anBackquoteTest
{
string
(
15
),
false
},
c
anBackquoteTest
{
string
(
15
),
false
},
C
anBackquoteTest
{
string
(
16
),
false
},
c
anBackquoteTest
{
string
(
16
),
false
},
C
anBackquoteTest
{
string
(
17
),
false
},
c
anBackquoteTest
{
string
(
17
),
false
},
C
anBackquoteTest
{
string
(
18
),
false
},
c
anBackquoteTest
{
string
(
18
),
false
},
C
anBackquoteTest
{
string
(
19
),
false
},
c
anBackquoteTest
{
string
(
19
),
false
},
C
anBackquoteTest
{
string
(
20
),
false
},
c
anBackquoteTest
{
string
(
20
),
false
},
C
anBackquoteTest
{
string
(
21
),
false
},
c
anBackquoteTest
{
string
(
21
),
false
},
C
anBackquoteTest
{
string
(
22
),
false
},
c
anBackquoteTest
{
string
(
22
),
false
},
C
anBackquoteTest
{
string
(
23
),
false
},
c
anBackquoteTest
{
string
(
23
),
false
},
C
anBackquoteTest
{
string
(
24
),
false
},
c
anBackquoteTest
{
string
(
24
),
false
},
C
anBackquoteTest
{
string
(
25
),
false
},
c
anBackquoteTest
{
string
(
25
),
false
},
C
anBackquoteTest
{
string
(
26
),
false
},
c
anBackquoteTest
{
string
(
26
),
false
},
C
anBackquoteTest
{
string
(
27
),
false
},
c
anBackquoteTest
{
string
(
27
),
false
},
C
anBackquoteTest
{
string
(
28
),
false
},
c
anBackquoteTest
{
string
(
28
),
false
},
C
anBackquoteTest
{
string
(
29
),
false
},
c
anBackquoteTest
{
string
(
29
),
false
},
C
anBackquoteTest
{
string
(
30
),
false
},
c
anBackquoteTest
{
string
(
30
),
false
},
C
anBackquoteTest
{
string
(
31
),
false
},
c
anBackquoteTest
{
string
(
31
),
false
},
C
anBackquoteTest
{
`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`
,
true
},
c
anBackquoteTest
{
`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`
,
true
},
C
anBackquoteTest
{
`0123456789`
,
true
},
c
anBackquoteTest
{
`0123456789`
,
true
},
C
anBackquoteTest
{
`ABCDEFGHIJKLMNOPQRSTUVWXYZ`
,
true
},
c
anBackquoteTest
{
`ABCDEFGHIJKLMNOPQRSTUVWXYZ`
,
true
},
C
anBackquoteTest
{
`abcdefghijklmnopqrstuvwxyz`
,
true
},
c
anBackquoteTest
{
`abcdefghijklmnopqrstuvwxyz`
,
true
},
C
anBackquoteTest
{
`☺`
,
true
},
c
anBackquoteTest
{
`☺`
,
true
},
}
}
export
func
TestCanBackquote
(
t
*
testing
.
T
)
{
export
func
TestCanBackquote
(
t
*
testing
.
T
)
{
...
...
test/chan/goroutines.go
View file @
8a7cbadb
...
@@ -22,7 +22,7 @@ func main() {
...
@@ -22,7 +22,7 @@ func main() {
var
n
=
10000
;
var
n
=
10000
;
if
sys
.
argc
()
>
1
{
if
sys
.
argc
()
>
1
{
var
err
*
os
.
Error
;
var
err
*
os
.
Error
;
n
,
err
=
strconv
.
a
toi
(
sys
.
argv
(
1
));
n
,
err
=
strconv
.
A
toi
(
sys
.
argv
(
1
));
if
err
!=
nil
{
if
err
!=
nil
{
print
(
"bad arg
\n
"
);
print
(
"bad arg
\n
"
);
sys
.
exit
(
1
);
sys
.
exit
(
1
);
...
...
test/fixedbugs/bug120.go
View file @
8a7cbadb
...
@@ -40,15 +40,15 @@ func main() {
...
@@ -40,15 +40,15 @@ func main() {
ok
:=
true
;
ok
:=
true
;
for
i
:=
0
;
i
<
len
(
tests
);
i
++
{
for
i
:=
0
;
i
<
len
(
tests
);
i
++
{
t
:=
tests
[
i
];
t
:=
tests
[
i
];
v
:=
strconv
.
f
toa64
(
t
.
f
,
'g'
,
-
1
);
v
:=
strconv
.
F
toa64
(
t
.
f
,
'g'
,
-
1
);
if
v
!=
t
.
out
{
if
v
!=
t
.
out
{
println
(
"Bad float64 const:"
,
t
.
in
,
"want"
,
t
.
out
,
"got"
,
v
);
println
(
"Bad float64 const:"
,
t
.
in
,
"want"
,
t
.
out
,
"got"
,
v
);
x
,
err
:=
strconv
.
a
tof64
(
t
.
out
);
x
,
err
:=
strconv
.
A
tof64
(
t
.
out
);
if
err
!=
nil
{
if
err
!=
nil
{
panicln
(
"bug120: strconv.
a
tof64"
,
t
.
out
);
panicln
(
"bug120: strconv.
A
tof64"
,
t
.
out
);
}
}
println
(
"
\t
want exact:"
,
strconv
.
f
toa64
(
x
,
'g'
,
1000
));
println
(
"
\t
want exact:"
,
strconv
.
F
toa64
(
x
,
'g'
,
1000
));
println
(
"
\t
got exact: "
,
strconv
.
f
toa64
(
t
.
f
,
'g'
,
1000
));
println
(
"
\t
got exact: "
,
strconv
.
F
toa64
(
t
.
f
,
'g'
,
1000
));
ok
=
false
;
ok
=
false
;
}
}
}
}
...
...
test/mallocrep1.go
View file @
8a7cbadb
...
@@ -87,7 +87,7 @@ func AllocAndFree(size, count int) {
...
@@ -87,7 +87,7 @@ func AllocAndFree(size, count int) {
}
}
func
atoi
(
s
string
)
int
{
func
atoi
(
s
string
)
int
{
i
,
xx1
:=
strconv
.
a
toi
(
s
);
i
,
xx1
:=
strconv
.
A
toi
(
s
);
return
i
return
i
}
}
...
...
test/map.go
View file @
8a7cbadb
...
@@ -61,8 +61,8 @@ func main() {
...
@@ -61,8 +61,8 @@ func main() {
var
apT
[
2
*
count
]
*
T
;
var
apT
[
2
*
count
]
*
T
;
for
i
:=
0
;
i
<
count
;
i
++
{
for
i
:=
0
;
i
<
count
;
i
++
{
s
:=
strconv
.
i
toa
(
i
);
s
:=
strconv
.
I
toa
(
i
);
s10
:=
strconv
.
i
toa
(
i
*
10
);
s10
:=
strconv
.
I
toa
(
i
*
10
);
f
:=
float
(
i
);
f
:=
float
(
i
);
t
:=
T
{
int64
(
i
),
f
};
t
:=
T
{
int64
(
i
),
f
};
apT
[
i
]
=
new
(
T
);
apT
[
i
]
=
new
(
T
);
...
@@ -137,8 +137,8 @@ func main() {
...
@@ -137,8 +137,8 @@ func main() {
// test construction directly
// test construction directly
for
i
:=
0
;
i
<
count
;
i
++
{
for
i
:=
0
;
i
<
count
;
i
++
{
s
:=
strconv
.
i
toa
(
i
);
s
:=
strconv
.
I
toa
(
i
);
s10
:=
strconv
.
i
toa
(
i
*
10
);
s10
:=
strconv
.
I
toa
(
i
*
10
);
f
:=
float
(
i
);
f
:=
float
(
i
);
t
:=
T
{
int64
(
i
),
f
};
t
:=
T
{
int64
(
i
),
f
};
// BUG m := M(i, i+1);
// BUG m := M(i, i+1);
...
@@ -191,7 +191,7 @@ func main() {
...
@@ -191,7 +191,7 @@ func main() {
// test existence with tuple check
// test existence with tuple check
// failed lookups yield a false value for the boolean.
// failed lookups yield a false value for the boolean.
for
i
:=
0
;
i
<
count
;
i
++
{
for
i
:=
0
;
i
<
count
;
i
++
{
s
:=
strconv
.
i
toa
(
i
);
s
:=
strconv
.
I
toa
(
i
);
f
:=
float
(
i
);
f
:=
float
(
i
);
t
:=
T
{
int64
(
i
),
f
};
t
:=
T
{
int64
(
i
),
f
};
{
{
...
@@ -329,7 +329,7 @@ func main() {
...
@@ -329,7 +329,7 @@ func main() {
// test nonexistence with tuple check
// test nonexistence with tuple check
// failed lookups yield a false value for the boolean.
// failed lookups yield a false value for the boolean.
for
i
:=
count
;
i
<
2
*
count
;
i
++
{
for
i
:=
count
;
i
<
2
*
count
;
i
++
{
s
:=
strconv
.
i
toa
(
i
);
s
:=
strconv
.
I
toa
(
i
);
f
:=
float
(
i
);
f
:=
float
(
i
);
t
:=
T
{
int64
(
i
),
f
};
t
:=
T
{
int64
(
i
),
f
};
{
{
...
@@ -467,7 +467,7 @@ func main() {
...
@@ -467,7 +467,7 @@ func main() {
// tests for structured map element updates
// tests for structured map element updates
for
i
:=
0
;
i
<
count
;
i
++
{
for
i
:=
0
;
i
<
count
;
i
++
{
s
:=
strconv
.
i
toa
(
i
);
s
:=
strconv
.
I
toa
(
i
);
mspa
[
s
][
i
%
2
]
=
"deleted"
;
mspa
[
s
][
i
%
2
]
=
"deleted"
;
if
mspa
[
s
][
i
%
2
]
!=
"deleted"
{
if
mspa
[
s
][
i
%
2
]
!=
"deleted"
{
fmt
.
Printf
(
"update mspa[%s][%d] = %s
\n
"
,
s
,
i
%
2
,
mspa
[
s
][
i
%
2
]);
fmt
.
Printf
(
"update mspa[%s][%d] = %s
\n
"
,
s
,
i
%
2
,
mspa
[
s
][
i
%
2
]);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment