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
2e7e7607
Commit
2e7e7607
authored
Nov 20, 2008
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
slightly simpler math.Pow per gri's suggestion
R=gri DELTA=28 (2 added, 9 deleted, 17 changed) OCL=19707 CL=19707
parent
5014da7c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
26 deletions
+19
-26
src/lib/math/pow.go
src/lib/math/pow.go
+19
-26
No files found.
src/lib/math/pow.go
View file @
2e7e7607
...
...
@@ -6,7 +6,7 @@ package math
import
"math"
// x^y: exponentation
// x^y: exponent
i
ation
export
func
Pow
(
x
,
y
float64
)
float64
{
// TODO: x or y NaN, ±Inf, maybe ±0.
switch
{
...
...
@@ -38,7 +38,9 @@ export func Pow(x, y float64) float64 {
return
Exp
(
y
*
Log
(
x
));
}
ans
:=
float64
(
1
);
// ans = a1 * 2^ae (= 1 for now).
a1
:=
float64
(
1
);
ae
:=
0
;
// ans *= x^yf
if
yf
!=
0
{
...
...
@@ -46,25 +48,18 @@ export func Pow(x, y float64) float64 {
yf
--
;
yi
++
;
}
a
ns
=
Exp
(
yf
*
Log
(
x
));
a
1
=
Exp
(
yf
*
Log
(
x
));
}
// ans *= x^yi
// by multiplying in successive squarings
// of x according to bits of yi.
// accumulate powers of two into exp.
// will still have to do ans *= 2^exp later.
x1
,
xe
:=
sys
.
frexp
(
x
);
exp
:=
0
;
if
i
:=
int64
(
yi
);
i
!=
0
{
for
{
for
i
:=
int64
(
yi
);
i
!=
0
;
i
>>=
1
{
if
i
&
1
==
1
{
ans
*=
x1
;
exp
+=
xe
;
}
i
>>=
1
;
if
i
==
0
{
break
;
a1
*=
x1
;
ae
+=
xe
;
}
x1
*=
x1
;
xe
<<=
1
;
...
...
@@ -73,15 +68,13 @@ export func Pow(x, y float64) float64 {
xe
--
;
}
}
}
// ans
*= 2^exp
// ans
= a1*2^ae
// if flip { ans = 1 / ans }
// but in the opposite order
if
flip
{
a
ns
=
1
/
ans
;
exp
=
-
exp
;
a
1
=
1
/
a1
;
ae
=
-
ae
;
}
return
sys
.
ldexp
(
a
ns
,
exp
);
return
sys
.
ldexp
(
a
1
,
ae
);
}
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