简体   繁体   English

算术运算符如何在python中工作?

[英]How do arithmetic operators work in python?

I am wondering how the "+" operator works in python, or indeed how any of the basic arithmetic operators work. 我想知道“ +”运算符在python中如何工作,或者实际上任何基本算术运算符如何工作。 My knowledge is very limited with regards to this topic, so I hope this isn't a repeat of a question already here. 我对这个主题的知识非常有限,所以我希望这里不再重复这个问题。

More specifically, I would like to know how this code: 更具体地说,我想知道这段代码:

a = 5
b = 2
c = a + b
print (c)

produces the result of c = 7 when ran. 运行时产生c = 7的结果。 How does the computer perform this operation? 电脑如何执行此操作? I found a thread on Reddit explaining how the computer performs the calculation in binary ( https://www.reddit.com/r/askscience/comments/1oqxfr/how_do_computers_do_math/ ) which I can understand. 我在Reddit上发现了一个线程,该线程解释了计算机如何以二进制形式( https://www.reddit.com/r/askscience/comments/1oqxfr/how_do_computers_do_math/ )执行计算。 What I fail to comprehend however is how the computer knows how to convert the values of 5 and 2 into binary and then perform the calculation. 但是,我无法理解的是计算机如何知道如何将5和2的值转换为二进制,然后执行计算。 Is there a set formula for doing this for all integers or base 10 numbers? 是否有针对所有整数或以10为底的数字执行此操作的设置公式? Or is there something else happening at a deeper hardware level here? 还是在更深的硬件级别上发生了其他事情?

Again I'm sorry if this a repeat or if the question seems completely silly, I just can't seem to understand how python can take any two numbers and then sum them, add them, divide them or multiply them. 再次抱歉,如果这个重复或问题看起来完全愚蠢,我似乎无法理解python如何获取任意两个数字然后求和,相加,相除或相乘。 Cheers. 干杯。

Cool question. 很酷的问题。

First of all, I do not think that you can type a + b = c . 首先,我认为您不能键入a + b = c It would rather be c = a + b . 宁愿是c = a + b

When the computer calculates this, it first converts 2 and 5 into binary. 当计算机对此进行计算时,首先将2和5转换为二进制。 So 2 gives 10, and 5 gives 101. 所以2给出10,而5给出101。

The two binary numbers are added in the same way that any two base ten numbers are added, resulting in 111, which is then converted back to base ten as 7. 这两个二进制数的添加方式与添加两个基数十的数字相同,从而得到111,然后将其转换回为7的基数十。

The numbers are always in binary. 数字始终为二进制。 The computer just isn't capable of keeping then in a different numerical system (well, there are ternary computers but these are a rare exception). 这样一来,计算机就无法将其保留在不同的数值系统中(嗯,有三元计算机,但是这是罕见的例外)。 The decimal system is just used for a "human representation", so that it is easier to read, but all the symbols (including the symbol "5" in the file, it's just a character) are mapped to numbers through some encoding (eg ASCII). 十进制系统仅用于“人类表示”,因此更易于阅读,但是所有符号(包括文件中的符号"5" ,它只是一个字符)都通过某种编码(例如, ASCII)。 These numbers are, of course in binary, just the computer knows (through the specification of the encoding) that if there is a 1000001 in a context of some string of characters, it has to display the symbol a (in the case of ASCII). 这些数字当然是二进制的,只是计算机知道(通过编码规范),如果在某些字符串的上下文中存在1000001 ,则它必须显示符号a (对于ASCII) 。 That's it, the computer don't know the number 58, for it, these are just two symbols and are kept in the memory as ones and zeros. 就是这样,计算机不知道数字58,因为这些数字只是两个符号,并以1和0的形式保存在内存中。

Now, memory. 现在,记忆。 This is where it's getting interesting. 这就是它变得有趣的地方。 All the instructions and the data are kept in one place as a large buffer of ones and zeros. 所有指令和数据都作为一个大的1和0缓冲区保存在一个地方。 These are passed to the CPU which (using its instruction set) knows what the first chunk of ones and zeros (this is what we call a "word") means. 它们被传递到CPU(使用其指令集),该CPU知道第一个1和0(即我们所说的“字”)块的含义。 The first word is an instruction, then the argument(s) follow. 第一个单词是一条指令,然后是参数。 Depending on the instruction different things happen. 根据指令,会发生不同的事情。 Ok, what happens if the instruction means "add these two numbers" and store the result here? 好的,如果指令的意思是“将这两个数字相加”并将结果存储在这里会怎样?

Well, now it's a hardware job. 好吧,现在这是一项硬件工作。 Adding binary numbers isn't that complicated, it's explained in the link you provided. 添加二进制数并不那么复杂,在您提供的链接中对此进行了说明。 But how the CPU knows that this is the algorithm and how to execute it? 但是CPU如何知道这是算法以及如何执行它? Well, it uses a bunch of "full-adders". 好吧,它使用了一堆“全加器”。 What is a "full-adder"? 什么是“加法器”? This is a hardware circuit that by given two inputs (each one of them is one bit, ie either one or zero) "adds" them and outputs the result to two other bits (one of which it uses for carry). 这是一个硬件电路,通过给定两个输入(每个输入为一位,即一个或零),将它们“相加”,并将结果输出到另外两个位(其中一个用于进位)。 But how the full-adder works? 但是全加器是如何工作的? Well, it is constructed (physically) by half-adders, which are constructed by standard and and xor gates. 好吧,它是(通过物理方式)由半加法器构造的,而半加法器是由标准and门和xor门构成的。 If you're familiar with similar operators ( & and ^ in Python) you probably know how they work. 如果您熟悉类似的运算符(Python中的&^ ),您可能会知道它们是如何工作的。 These gates are designed to work as expected using the physical properties of the elements (the most important of them being silicon) used in the electronic components. 这些门设计为使用电子元件中使用的元素(其中最重要的是硅)的物理特性来按预期工作。 And I think this is where I'll stop. 我认为这是我要停止的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM