热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python中模运算是什么意思_Python如何实现模运算?

Imcuriousinregardstothetimeandspacecomplexitiesofthe%operatorinPython.Also,doesPythonuseab

I'm curious in regards to the time and space complexities of the % operator in Python. Also, does Python use a bitwise operation for % 2?

Edit:

I'm asking about Python 2.7's implementation, just in case it differs slightly from that of Python 3

解决方案

Python uses the classic Algorithm D from Knuth's 'The Art of Computer Programming'. The running time is (generally) proportional to the product of lengths of the two numbers. Space is proportional to the sum of the lengths of the two numbers.

The actual division occurs in Objects/longobject.c, see x_divrem(). For background on the internal representation of a Python long, see Include/longintrepr.h.

% 2 does not use bitwise operations. The standard idiom for checking if a number is even/odd is & 1.

Python 2 and 3 use the same algorithm.



推荐阅读
author-avatar
天涯小犁_640
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有