Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0046695d
Commit
0046695d
authored
May 05, 1991
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added divmod and abs, fixed negative powers
parent
175a9ea8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
18 deletions
+70
-18
Objects/intobject.c
Objects/intobject.c
+70
-18
No files found.
Objects/intobject.c
View file @
0046695d
...
...
@@ -230,31 +230,71 @@ int_rem(v, w)
return
newintobject
(
v
->
ob_ival
%
((
intobject
*
)
w
)
->
ob_ival
);
}
static
object
*
int_divmod
(
x
,
y
)
intobject
*
x
;
register
object
*
y
;
{
object
*
v
,
*
v0
,
*
v1
;
long
xi
,
yi
,
xdivy
,
xmody
;
if
(
!
is_intobject
(
y
))
{
err_badarg
();
return
NULL
;
}
xi
=
x
->
ob_ival
;
yi
=
getintvalue
(
y
);
if
(
yi
==
0
)
return
err_zdiv
();
if
(
yi
<
0
)
{
xdivy
=
-
xi
/
-
yi
;
}
else
{
xdivy
=
xi
/
yi
;
}
xmody
=
xi
-
xdivy
*
yi
;
if
(
xmody
<
0
&&
yi
>
0
||
xmody
>
0
&&
yi
<
0
)
{
xmody
+=
yi
;
xdivy
-=
1
;
}
v
=
newtupleobject
(
2
);
v0
=
newintobject
(
xdivy
);
v1
=
newintobject
(
xmody
);
if
(
v
==
NULL
||
v0
==
NULL
||
v1
==
NULL
||
settupleitem
(
v
,
0
,
v0
)
!=
0
||
settupleitem
(
v
,
1
,
v1
)
!=
0
)
{
XDECREF
(
v
);
XDECREF
(
v0
);
XDECREF
(
v1
);
v
=
NULL
;
}
return
v
;
}
static
object
*
int_pow
(
v
,
w
)
intobject
*
v
;
register
object
*
w
;
{
register
long
iv
,
iw
,
ix
;
register
int
neg
;
if
(
!
is_intobject
(
w
))
{
err_badarg
();
return
NULL
;
}
iv
=
v
->
ob_ival
;
iw
=
((
intobject
*
)
w
)
->
ob_ival
;
neg
=
0
;
if
(
iw
<
0
)
neg
=
1
,
iw
=
-
iw
;
if
(
iw
<
0
)
{
err_setstr
(
RuntimeError
,
"integer to the negative power"
);
return
NULL
;
}
ix
=
1
;
for
(;
iw
>
0
;
iw
--
)
while
(
--
iw
>=
0
)
{
long
prev
=
ix
;
ix
=
ix
*
iv
;
if
(
neg
)
{
if
(
ix
==
0
)
return
err_zdiv
();
ix
=
1
/
ix
;
if
(
iv
==
0
)
break
;
/* 0 to some power -- avoid ix / 0 */
if
(
ix
/
iv
!=
prev
)
return
err_ovf
()
;
}
/* XXX How to check for overflow? */
return
newintobject
(
ix
);
}
...
...
@@ -278,15 +318,27 @@ int_pos(v)
return
(
object
*
)
v
;
}
static
object
*
int_abs
(
v
)
intobject
*
v
;
{
if
(
v
->
ob_ival
>=
0
)
return
int_pos
(
v
);
else
return
int_neg
(
v
);
}
static
number_methods
int_as_number
=
{
int_add
,
/*tp_add*/
int_sub
,
/*tp_subtract*/
int_mul
,
/*tp_multiply*/
int_div
,
/*tp_divide*/
int_rem
,
/*tp_remainder*/
int_pow
,
/*tp_power*/
int_neg
,
/*tp_negate*/
int_pos
,
/*tp_plus*/
int_add
,
/*nb_add*/
int_sub
,
/*nb_subtract*/
int_mul
,
/*nb_multiply*/
int_div
,
/*nb_divide*/
int_rem
,
/*nb_remainder*/
int_divmod
,
/*nb_divmod*/
int_pow
,
/*nb_power*/
int_neg
,
/*nb_negative*/
int_pos
,
/*nb_positive*/
int_abs
,
/*nb_absolute*/
};
typeobject
Inttype
=
{
...
...
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