Skip to content

Numeric in Python

  • Python

5 types

int

integers are represented using base-2(binary) digits, not base-10(decimal)

10011(base-2) required 5 bits

what’s the range of integer number can be represented using 8 bits?

11111111(base-2) required 8 bits, could represent largest 255

Using 8 bits are able to represent range [-127, 127], since 0 does not require a sign, we can squeeze out an extra number to [-128, 127]

How large an integer can be depends on how many bits are used, some languages(Java, C) provide multiple distinct integer types that use a fixed number of bits. Python doesn’t work that way, the int object uses a variable number of bits, can use 32, 64, 96 bits etc.(increase bits by need)

int arithmetic
  • int +(-, *, **, //, %) int => int
  • int / int => float

integer base change algorithm


Fraction

represent rational number


float

represent real number

CPython float is implemented using the C double type which implements the IEEE double-precision binary float, also called binary64

float uses a fixed number of bytes, in standard, CPython/3.6/64bits using 8 bytes(64 bits)

representation
  • decimal
  • binary

problem: some numbers do not have a finite representation

  • 1/3 can not represent accurate by decimal expansion
  • 1/10 can not represent accurate by binary expansion

Coercing float to integer -> data loss

truncation: math.trunc(), constructor of int() use truncation

floor: math.floor()

ceiling: math.ceil()

round: round()

Banker’s Rounding

Read More »Numeric in Python

伊凡.伊里奇之死

  • Quote

他只知道,這種場合在胸前畫個十字不會出什麼差錯。至於是否也需要鞠躬,他不太確定,所以他選擇了一個折衷的辦法:走進房間後,他變開始畫十字,頭微微前傾,像是在鞠躬的樣子。

他的臉,如同所有遺體,變得更好看了一些,主要是——變得比他活著時還要更加意味深長。臉上的神情似乎在說,該做的事都完成了,而且完成得規規矩矩。

伊凡.伊里奇並沒有明確要結婚的打算,但當這位姑娘愛上他以後,他便問自己:「所以,到底為什麼不結婚?」

這麼說好了,伊凡.伊里奇結婚,是因為他愛上自己的未婚妻,且他發現她也認同自己的人生觀,若說他結婚是因為他圈子的人撮合的,實在對他不公平。伊凡.伊里奇會結婚,是因為這兩個想法:他很高興能夠娶到這樣的妻子,此外,他做了一見金字塔頂端的人們認為正確的事。

他一天不在家且不像她一樣憂愁,她就不會停止她的惡言惡語。

他僅要求家庭生活讓他有飯可吃、有女主人照料和有床可睡,更主要的是——表面上看起來體面,以符合社會的期待。除此之外,他也會在其中尋找快樂,若能找到,他便心存感激;若遇到妻子的攻擊和埋怨,他便立刻逃到自己獨立、封閉的工作世界裡,在工作中尋找樂趣。

然而,這對夫妻偶爾還是擁有濃情蜜意的時光,但這些時光往往稍縱即逝,好似這對夫妻暫時依附於某個島嶼,不久他們又落入心懷仇恨、相互疏遠的汪洋大海中。

在處理事務時,必須要排除一切會影響公務正常運作的生活雜務:不許與人建立任何工作以外的關係,產生關係的理由也只許與工作相關,且關係的本身只能與工作相關。

他學習凱斯維特邏輯學的直言三段論時,學到的例子是:「凱薩是人,人會死,所以凱薩會死。」對他來說,這輩子只有用在凱薩身上時是正確的,不會用在他的身上。那是凱薩這個人,一般的人,所以是正確的;但他既不是凱薩、也不是一般人,他一直是完全、完全不同於別人的人;

對伊凡.伊里奇來說,最大的痛苦是謊言——所有人不知何故都對他說謊,說他只是生病了,不至於死,只需要保持冷靜,好好治療,到時就會有好消息。

他步入死亡的過程是很可怕、令人恐懼的,但他發現,這段過程竟被周圍所有人、被他一輩子所謹守的「體面」,貶低成偶然的不愉快、某種程度的有礙觀瞻(對待他的方式,彷彿他是一位散發惡臭走進客廳的人);

伊凡.伊里奇在經歷長久的折磨之後,有時最渴望的——他自己也不好意思承認——是想要像生病的小孩一樣,受到別人的同情。他想要被寵愛、親吻,想要有人為他流淚,就像在寵愛、安慰小孩一樣。

當那些造就現在伊凡.伊里奇的一切開始出現時,那些曾經認為的快樂,現在在他眼前逐漸消融,變成微不足道,或往往齷齪可鄙的東西。

她的服裝、她的身材、她臉上的表情及她的聲音——都告訴他:「不對勁。你過去和現在生活的方式都是謊言、欺騙,它們向你掩蓋了生與死的真諦。」當他一開始這樣想,他的那股厭惡感,以及隨之而來的身體上的痛苦與折磨,和無法逃脫眼前死亡的感覺,便油然而生。

忽然間,他恍然大悟,那一直折磨他的、不離開他的東西,突然間就從兩旁、從四面八方一下子都離開了。心疼他們,就必須做點什麼,好讓他們不那麼痛苦。使他們、也使自己擺脫這些痛苦。

「死亡結束了,」他對自己說:「再也沒也死亡。」

Python Optimization

Interpreters
  • CPython: the standard python implementation written in C
  • Jython: written in Java
  • IronPython: written in C#
  • PyPy: written in RPython (which is itself a statically-typed subset of Python written C that is designed to write interpreters)

Interning

Reusing objects on-demand

Integer

At startup, CPython pre-loads(catches) a global list of integers in the range[-5, 256], any time an integer is referenced in the range, python will use the cached version of that object.

Optimization strategy: small integers show up often

string

Not all strings are automatically interned

As the python code is compiled, identifiers are interned

peephole

an optimization occur at compile time

pre-calculate constant expressions

  • numeric calculation: 24 * 60 (store 1440)
  • short seqences length < 20: ‘abc’ * 3 (store ‘abcabcabc’)

membership tests (if e in [1, 2, 3]: …)

mutable replaced by immutable

  • list -> tuples
  • set -> frozenset
  • set membership is much faster than list and tuple

Object Mutability in Python

Internal state

changing the data inside the object is called modifying the internal state of the object, the state(data) is changed, but memory address has not changed

Mutable

an object whose internal state can be changed

  • Lists, Sets, Dictionarys, User-defined Classes

Immutable

an object whose internal state can not be changed

  • Numbers(int, float, Booleans), String, Tuples, Frozen Sets, User-defined Classes
  • variable re-assignment change the reference not the internal value

 

Keyword “Arguments” and Spread

“arguments” is another keyword JavaScript engine create during the creation of execution context, arguments contains a list of all the values of all the parameters that passed to a function.

argument is an array-like object, it doesn’t has all the features of JavaScript array

“arguments” will become deprecated in the future, the new thing is called spread