Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
314b358a
Commit
314b358a
authored
Nov 05, 2009
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gofmt-ify strconv
(and remove an empty line at the end of a file) R=rsc
http://go/go-review/1017046
parent
5d377054
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
46 deletions
+45
-46
atof_test.go
src/pkg/strconv/atof_test.go
+0
-0
atoi_test.go
src/pkg/strconv/atoi_test.go
+0
-0
decimal.go
src/pkg/strconv/decimal.go
+45
-46
No files found.
src/pkg/strconv/atof_test.go
View file @
314b358a
src/pkg/strconv/atoi_test.go
View file @
314b358a
src/pkg/strconv/decimal.go
View file @
314b358a
...
...
@@ -16,10 +16,10 @@ import "bytes"
type
decimal
struct
{
// TODO(rsc): Can make d[] a bit smaller and add
// truncated bool;
d
[
2000
]
byte
;
// digits
d
[
2000
]
byte
;
// digits
nd
int
;
// number of digits used
dp
int
;
// decimal point
}
;
}
func
(
a
*
decimal
)
String
()
string
{
n
:=
10
+
a
.
nd
;
...
...
@@ -42,20 +42,20 @@ func (a *decimal) String() string {
w
++
;
buf
[
w
]
=
'.'
;
w
++
;
w
+=
digitZero
(
buf
[
w
:
w
+
-
a
.
dp
]);
w
+=
bytes
.
Copy
(
buf
[
w
:
w
+
a
.
nd
],
a
.
d
[
0
:
a
.
nd
]);
w
+=
digitZero
(
buf
[
w
:
w
+
-
a
.
dp
]);
w
+=
bytes
.
Copy
(
buf
[
w
:
w
+
a
.
nd
],
a
.
d
[
0
:
a
.
nd
]);
case
a
.
dp
<
a
.
nd
:
// decimal point in middle of digits
w
+=
bytes
.
Copy
(
buf
[
w
:
w
+
a
.
dp
],
a
.
d
[
0
:
a
.
dp
]);
w
+=
bytes
.
Copy
(
buf
[
w
:
w
+
a
.
dp
],
a
.
d
[
0
:
a
.
dp
]);
buf
[
w
]
=
'.'
;
w
++
;
w
+=
bytes
.
Copy
(
buf
[
w
:
w
+
a
.
nd
-
a
.
dp
],
a
.
d
[
a
.
dp
:
a
.
nd
]);
w
+=
bytes
.
Copy
(
buf
[
w
:
w
+
a
.
nd
-
a
.
dp
],
a
.
d
[
a
.
dp
:
a
.
nd
]);
default
:
// zeros fill space between digits and decimal point
w
+=
bytes
.
Copy
(
buf
[
w
:
w
+
a
.
nd
],
a
.
d
[
0
:
a
.
nd
]);
w
+=
digitZero
(
buf
[
w
:
w
+
a
.
dp
-
a
.
nd
]);
w
+=
bytes
.
Copy
(
buf
[
w
:
w
+
a
.
nd
],
a
.
d
[
0
:
a
.
nd
]);
w
+=
digitZero
(
buf
[
w
:
w
+
a
.
dp
-
a
.
nd
]);
}
return
string
(
buf
[
0
:
w
]);
}
...
...
@@ -78,7 +78,7 @@ func digitZero(dst []byte) int {
// (They are meaningless; the decimal point is tracked
// independent of the number of digits.)
func
trim
(
a
*
decimal
)
{
for
a
.
nd
>
0
&&
a
.
d
[
a
.
nd
-
1
]
==
'0'
{
for
a
.
nd
>
0
&&
a
.
d
[
a
.
nd
-
1
]
==
'0'
{
a
.
nd
--
;
}
if
a
.
nd
==
0
{
...
...
@@ -95,14 +95,14 @@ func (a *decimal) Assign(v uint64) {
for
v
>
0
{
v1
:=
v
/
10
;
v
-=
10
*
v1
;
buf
[
n
]
=
byte
(
v
+
'0'
);
buf
[
n
]
=
byte
(
v
+
'0'
);
n
++
;
v
=
v1
;
}
// Reverse again to produce forward decimal in a.d.
a
.
nd
=
0
;
for
n
--
;
n
>=
0
;
n
--
{
for
n
--
;
n
>=
0
;
n
--
{
a
.
d
[
a
.
nd
]
=
buf
[
n
];
a
.
nd
++
;
}
...
...
@@ -141,7 +141,7 @@ func rightShift(a *decimal, k uint) {
break
;
}
c
:=
int
(
a
.
d
[
r
]);
n
=
n
*
10
+
c
-
'0'
;
n
=
n
*
10
+
c
-
'0'
;
}
a
.
dp
-=
r
-
1
;
...
...
@@ -152,7 +152,7 @@ func rightShift(a *decimal, k uint) {
n
-=
dig
<<
k
;
a
.
d
[
w
]
=
byte
(
dig
+
'0'
);
w
++
;
n
=
n
*
10
+
c
-
'0'
;
n
=
n
*
10
+
c
-
'0'
;
}
// Put down extra digits.
...
...
@@ -183,7 +183,7 @@ type leftCheat struct {
cutoff
string
;
// minus one digit if original < a.
}
var
leftcheats
=
[]
leftCheat
{
var
leftcheats
=
[]
leftCheat
{
// Leading digits of 1/2^i = 5^i.
// 5^23 is not an exact 64-bit floating point number,
// so have to use bc for the math.
...
...
@@ -196,34 +196,34 @@ var leftcheats = []leftCheat {
int(log2*NR+1), $0, 2**NR)
}'
*/
leftCheat
{
0
,
""
},
leftCheat
{
1
,
"5"
},
// * 2
leftCheat
{
1
,
"25"
},
// * 4
leftCheat
{
1
,
"125"
},
// * 8
leftCheat
{
2
,
"625"
},
// * 16
leftCheat
{
2
,
"3125"
},
// * 32
leftCheat
{
2
,
"15625"
},
// * 64
leftCheat
{
3
,
"78125"
},
// * 128
leftCheat
{
3
,
"390625"
},
// * 256
leftCheat
{
3
,
"1953125"
},
// * 512
leftCheat
{
4
,
"9765625"
},
// * 1024
leftCheat
{
4
,
"48828125"
},
// * 2048
leftCheat
{
4
,
"244140625"
},
// * 4096
leftCheat
{
4
,
"1220703125"
},
// * 8192
leftCheat
{
5
,
"6103515625"
},
// * 16384
leftCheat
{
5
,
"30517578125"
},
// * 32768
leftCheat
{
5
,
"152587890625"
},
// * 65536
leftCheat
{
6
,
"762939453125"
},
// * 131072
leftCheat
{
6
,
"3814697265625"
},
// * 262144
leftCheat
{
6
,
"19073486328125"
},
// * 524288
leftCheat
{
7
,
"95367431640625"
},
// * 1048576
leftCheat
{
7
,
"476837158203125"
},
// * 2097152
leftCheat
{
7
,
"2384185791015625"
},
// * 4194304
leftCheat
{
7
,
"11920928955078125"
},
// * 8388608
leftCheat
{
8
,
"59604644775390625"
},
// * 16777216
leftCheat
{
8
,
"298023223876953125"
},
// * 33554432
leftCheat
{
8
,
"1490116119384765625"
},
// * 67108864
leftCheat
{
9
,
"7450580596923828125"
},
// * 134217728
leftCheat
{
0
,
""
},
leftCheat
{
1
,
"5"
},
// * 2
leftCheat
{
1
,
"25"
},
// * 4
leftCheat
{
1
,
"125"
},
// * 8
leftCheat
{
2
,
"625"
},
// * 16
leftCheat
{
2
,
"3125"
},
// * 32
leftCheat
{
2
,
"15625"
},
// * 64
leftCheat
{
3
,
"78125"
},
// * 128
leftCheat
{
3
,
"390625"
},
// * 256
leftCheat
{
3
,
"1953125"
},
// * 512
leftCheat
{
4
,
"9765625"
},
// * 1024
leftCheat
{
4
,
"48828125"
},
// * 2048
leftCheat
{
4
,
"244140625"
},
// * 4096
leftCheat
{
4
,
"1220703125"
},
// * 8192
leftCheat
{
5
,
"6103515625"
},
// * 16384
leftCheat
{
5
,
"30517578125"
},
// * 32768
leftCheat
{
5
,
"152587890625"
},
// * 65536
leftCheat
{
6
,
"762939453125"
},
// * 131072
leftCheat
{
6
,
"3814697265625"
},
// * 262144
leftCheat
{
6
,
"19073486328125"
},
// * 524288
leftCheat
{
7
,
"95367431640625"
},
// * 1048576
leftCheat
{
7
,
"476837158203125"
},
// * 2097152
leftCheat
{
7
,
"2384185791015625"
},
// * 4194304
leftCheat
{
7
,
"11920928955078125"
},
// * 8388608
leftCheat
{
8
,
"59604644775390625"
},
// * 16777216
leftCheat
{
8
,
"298023223876953125"
},
// * 33554432
leftCheat
{
8
,
"1490116119384765625"
},
// * 67108864
leftCheat
{
9
,
"7450580596923828125"
},
// * 134217728
}
// Is the leading prefix of b lexicographically less than s?
...
...
@@ -242,7 +242,7 @@ func prefixIsLessThan(b []byte, s string) bool {
// Binary shift left (/ 2) by k bits. k <= maxShift to avoid overflow.
func
leftShift
(
a
*
decimal
,
k
uint
)
{
delta
:=
leftcheats
[
k
]
.
delta
;
if
prefixIsLessThan
(
a
.
d
[
0
:
a
.
nd
],
leftcheats
[
k
]
.
cutoff
)
{
if
prefixIsLessThan
(
a
.
d
[
0
:
a
.
nd
],
leftcheats
[
k
]
.
cutoff
)
{
delta
--
;
}
...
...
@@ -252,7 +252,7 @@ func leftShift(a *decimal, k uint) {
// Pick up a digit, put down a digit.
for
r
--
;
r
>=
0
;
r
--
{
n
+=
(
int
(
a
.
d
[
r
])
-
'0'
)
<<
k
;
n
+=
(
int
(
a
.
d
[
r
])
-
'0'
)
<<
k
;
quo
:=
n
/
10
;
rem
:=
n
-
10
*
quo
;
w
--
;
...
...
@@ -302,7 +302,7 @@ func shouldRoundUp(a *decimal, nd int) bool {
return
false
;
}
if
a
.
d
[
nd
]
==
'5'
&&
nd
+
1
==
a
.
nd
{
// exactly halfway - round to even
return
(
a
.
d
[
nd
-
1
]
-
'0'
)
%
2
!=
0
;
return
(
a
.
d
[
nd
-
1
]
-
'0'
)
%
2
!=
0
;
}
// not halfway - digit tells all
return
a
.
d
[
nd
]
>=
'5'
;
...
...
@@ -375,4 +375,3 @@ func (a *decimal) RoundedInteger() uint64 {
}
return
n
;
}
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