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
b233ac8f
Commit
b233ac8f
authored
Sep 28, 2010
by
Charles L. Dorian
Committed by
Russ Cox
Sep 28, 2010
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
math: Fix off-by-one error in Ilogb and Logb.
Fixes #1141. R=rsc CC=adg, golang-dev
https://golang.org/cl/2194047
parent
0e66a13d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
12 deletions
+13
-12
src/pkg/math/all_test.go
src/pkg/math/all_test.go
+9
-8
src/pkg/math/logb.go
src/pkg/math/logb.go
+4
-4
No files found.
src/pkg/math/all_test.go
View file @
b233ac8f
...
...
@@ -383,16 +383,16 @@ var log = []float64{
2.161703872847352815363655e+00
,
}
var
logb
=
[]
float64
{
3.0000000000000000e+00
,
3.0000000000000000e+00
,
-
1.0000000000000000e+00
,
3.0000000000000000e+00
,
4.0000000000000000e+00
,
2.0000000000000000e+00
,
2.0000000000000000e+00
,
-
2.0000000000000000e+00
,
2.0000000000000000e+00
,
3.0000000000000000e+00
,
1.0000000000000000e+00
,
2.0000000000000000e+00
,
1.0000000000000000e+00
,
4.0000000000000000e+00
,
0.0000000000000000e+00
,
3.0000000000000000e+00
,
}
var
log10
=
[]
float64
{
6.9714316642508290997617083e-01
,
...
...
@@ -1806,8 +1806,9 @@ func TestHypot(t *testing.T) {
func
TestIlogb
(
t
*
testing
.
T
)
{
for
i
:=
0
;
i
<
len
(
vf
);
i
++
{
if
e
:=
Ilogb
(
vf
[
i
]);
frexp
[
i
]
.
i
!=
e
{
t
.
Errorf
(
"Ilogb(%g) = %d, want %d"
,
vf
[
i
],
e
,
frexp
[
i
]
.
i
)
a
:=
frexp
[
i
]
.
i
-
1
// adjust because fr in the interval [½, 1)
if
e
:=
Ilogb
(
vf
[
i
]);
a
!=
e
{
t
.
Errorf
(
"Ilogb(%g) = %d, want %d"
,
vf
[
i
],
e
,
a
)
}
}
for
i
:=
0
;
i
<
len
(
vflogbSC
);
i
++
{
...
...
src/pkg/math/logb.go
View file @
b233ac8f
...
...
@@ -4,7 +4,7 @@
package
math
// Logb(x) returns the binary
logarithm
of non-zero x.
// Logb(x) returns the binary
exponent
of non-zero x.
//
// Special cases are:
// Logb(±Inf) = +Inf
...
...
@@ -22,10 +22,10 @@ func Logb(x float64) float64 {
case
x
!=
x
:
// IsNaN(x):
return
x
}
return
float64
(
int
((
Float64bits
(
x
)
>>
shift
)
&
mask
)
-
bias
)
return
float64
(
int
((
Float64bits
(
x
)
>>
shift
)
&
mask
)
-
(
bias
+
1
)
)
}
// Ilogb(x) returns the binary
logarithm
of non-zero x as an integer.
// Ilogb(x) returns the binary
exponent
of non-zero x as an integer.
//
// Special cases are:
// Ilogb(±Inf) = MaxInt32
...
...
@@ -43,5 +43,5 @@ func Ilogb(x float64) int {
case
x
<
-
MaxFloat64
||
x
>
MaxFloat64
:
// IsInf(x, 0):
return
MaxInt32
}
return
int
((
Float64bits
(
x
)
>>
shift
)
&
mask
)
-
bias
return
int
((
Float64bits
(
x
)
>>
shift
)
&
mask
)
-
(
bias
+
1
)
}
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