Python integer 32 bit

Data Types¶

A conforming implementation of the array API standard must provide and support the following data types.

bool¶

int8¶

An 8-bit signed integer whose values exist on the interval [-128, +127] .

int16¶

A 16-bit signed integer whose values exist on the interval [−32,767, +32,767] .

int32¶

A 32-bit signed integer whose values exist on the interval [−2,147,483,647, +2,147,483,647] .

int64¶

A 64-bit signed integer whose values exist on the interval [−9,223,372,036,854,775,807, +9,223,372,036,854,775,807] .

uint8¶

An 8-bit unsigned integer whose values exist on the interval [0, +255] .

uint16¶

A 16-bit unsigned integer whose values exist on the interval [0, +65,535] .

uint32¶

A 32-bit unsigned integer whose values exist on the interval [0, +4,294,967,295] .

uint64¶

A 64-bit unsigned integer whose values exist on the interval [0, +18,446,744,073,709,551,615] .

float32¶

IEEE 754 single-precision (32-bit) binary floating-point number (see IEEE 754-2019).

float64¶

IEEE 754 double-precision (64-bit) binary floating-point number (see IEEE 754-2019).

complex64¶

Single-precision (64-bit) complex floating-point number whose real and imaginary components must be IEEE 754 single-precision (32-bit) binary floating-point numbers (see IEEE 754-2019).

complex128¶

Double-precision (128-bit) complex floating-point number whose real and imaginary components must be IEEE 754 double-precision (64-bit) binary floating-point numbers (see IEEE 754-2019).

IEEE 754-2019 requires support for subnormal (a.k.a., denormal) numbers, which are useful for supporting gradual underflow. However, hardware support for subnormal numbers is not universal, and many platforms (e.g., accelerators) and compilers support toggling denormals-are-zero (DAZ) and/or flush-to-zero (FTZ) behavior to increase performance and to guard against timing attacks.

Accordingly, subnormal behavior is left unspecified and, thus, implementation-defined. Conforming implementations may vary in their support for subnormal numbers.

A conforming implementation of the array API standard may provide and support additional data types beyond those described in this specification.

Data Type Objects¶

Data types (“dtypes”) are objects which are used as dtype specifiers in functions and methods (e.g., zeros((2, 3), dtype=float32) ).

A conforming implementation may add additional methods or attributes to data type objects beyond those described in this specification.

Implementations may provide other ways to specify data types (e.g., zeros((2, 3), dtype=’f4′) ) which are not described in this specification; however, in order to ensure portability, array library consumers are recommended to use data type objects as provided by specification conforming array libraries.

A conforming implementation of the array API standard must provide and support data type objects having the following attributes and methods.

Methods¶

Computes the truth value of self == other in order to test for data type object equality.

Default Data Types¶

A conforming implementation of the array API standard must define the following default data types.

  • a default real-valued floating-point data type (either float32 or float64 ).
  • a default complex floating-point data type (either complex64 or complex128 ).
  • a default integer data type (either int32 or int64 ).
  • a default array index data type (either int32 or int64 ).

The default real-valued floating-point and complex floating-point data types must be the same across platforms.

The default complex floating-point point data type should match the default real-valued floating-point data type. For example, if the default real-valued floating-point data type is float32 , the default complex floating-point data type must be complex64 . If the default real-valued floating-point data type is float64 , the default complex floating-point data type must be complex128 .

The default integer data type should be the same across platforms, but the default may vary depending on whether Python is 32-bit or 64-bit.

The default array index data type may be int32 on 32-bit platforms, but the default should be int64 otherwise.

Note that it is possible that a library supports multiple devices, with not all those device types supporting the same data types. In this case, the default integer or floating-point data types may vary with device. If that is the case, the library should clearly warn about this in its documentation.

The default data types should be clearly defined in a conforming library’s documentation.

Data Type Categories¶

For the purpose of organizing functions within this specification, the following data type categories are defined.

Conforming libraries are not required to organize data types according to these categories. These categories are only intended for use within this specification.

Numeric Data Types¶

int8 , int16 , int32 , int64 , uint8 , uint16 , uint32 , uint64 , float32 , float64 , complex64 , and complex128 .

Real-valued Data Types¶

int8 , int16 , int32 , int64 , uint8 , uint16 , uint32 , uint64 , float32 , and float64 .

Integer Data Types¶

int8 , int16 , int32 , int64 , uint8 , uint16 , uint32 , and uint64 .

Floating-point Data Types¶

float32 , float64 , complex64 , and complex128 .

Real-valued Floating-point Data Types¶

Complex Floating-point Data Types¶

Источник

Python Integers

Summary: in this tutorial, you’ll learn about Python integers and how Python stores integers in the memory.

Integers are whole numbers that include negative numbers, zero, and positive numbers such as -3, -2, -1, 0, 1, 2, 3.

Python uses the class int to represent all integer numbers. All integers are objects.

How computers store integers

Computers can’t store integers directly. Instead, they only can store binary numbers such as 0 and 1.

To store integers, the computers need to use binary numbers to represent the integers.

For example, to store the number 5, the computers need to represent it using a base-2 number:

5 = 1 x 2 2 + 0 x 2 1 + 1 x 2 0

As you can see, it takes 3 bits to store the number 5 in the memory:

Suppose that you have 8 bits, you can store up to 255 integers from zero to 255:

255= 1x 2 7 + 1 x 2 6 + 1 x 2 5 + 1x 2 4 + 1 x 2 3 + 1 x 2 2 + 1x 2 1 + 1 x 2 0

By using 8 bits, you can store up to 2 8 – 1 = 255 integers.

To store both negative integers, zero, and positive integers, you need to reserve 1 bit for storing the sign, negative (-) and positive (+). Therefore, with 8 bits:

  • The largest integer that the computers can represent is 2 7 = 127.
  • And the computers can store all integers in the range (-127, 127)

Because the number zero doesn’t have a sign, the computers can squeeze out an extra number. Therefore, 8 bits can store all integers from -128 to 127.

Similarly, if you want to use 16 bits, 32 bits, and 64 bits to store integers, the ranges would be:

  • 16-bits ~ [-2 15 , 2 15 – 1] = [ -32,768 , 32,767 ]
  • 32-bits ~ [-2 31 , 2 31 – 1] = [- 2,147,483,648 , 2,147,483,647 ]
  • 64-bits ~ [-2 63 , 2 63 – 1] = [ -9,223,372,036,854,775,808 , 9,223,372,036,854,775,807 ]

How Python represents integers

Other programming languages such as Java and C# use a fixed number of bits to store integers.

For example, C# has the int type that uses 32-bits and the long type that uses 64 bits to represent integers. Based on the integer types, you can determine the ranges of the integers those types can represent.

Python, however, doesn’t use a fixed number of bit to store integers. Instead, Python uses a variable number of bits to store integers. For example, 8 bits, 16 bits, 32 bits, 64 bits, 128 bits, and so on.

The maximum integer number that Python can represent depends on the memory available.

Also, integers are objects. Python needs an extra fixed number of bytes as an overhead for each integer.

It’s important to note that the bigger the integer numbers are, the slower the calculations such as + , — , … will be.

Python int type

The following defines a variable that references an integer and uses the type() function to get the class name of the integer:

counter = 10 print(type(counter))Code language: Python (python)
class 'int'>Code language: Python (python)

As you can see clearly from the ouput, an integer is an instance of the class int .

Getting the size of an integer

To get the size of an integer, you use the getsizeof() function of the sys module.

The getsizeof() function returns the number of bytes that Python uses to represent an integer. For example:

from sys import getsizeof counter = 0 size = getsizeof(counter) print(size) # 24 bytesCode language: Python (python)
24Code language: Python (python)

To store the number 0, Python uses 24 bytes. Since storing the number zero, Python needs to use only 1 bit. Note that 1 byte equals 8 bits.

Therefore, you can think that Python uses 24 bytes as an overhead for storing an integer object.

The following returns the size of the integer 100:

from sys import getsizeof counter = 100 size = getsizeof(counter) print(size) # 28 bytesCode language: Python (python)
28Code language: Python (python)

It returns 28 bytes. Since 24 bytes is an overhead, Python uses 4 bytes to represent the number 100.

The following shows the size of the integer 2 64 :

from sys import getsizeof counter = 2**64 size = getsizeof(counter) print(size) # 36 bytesCode language: Python (python)
36Code language: Python (python)

So to store the integer 2 64 , Python uses 36 bytes.

Python integer operations

Python integers support all standard operations including:

  • Addition +
  • Subtraction –
  • Multiplication *
  • Division /
  • Exponents **

The result of addition, subtraction, multiplication, and exponents of integers is an integer. For example:

a = 10 b = 20 c = a + b print(c) print(type(c)) c = a - b print(c) print(type(c)) c = a * b print(c) print(type(c)) c = a ** b print(c) print(type(c))Code language: Python (python)
30 class 'int'> -10 class 'int'> 200 class 'int'> 100000000000000000000 class 'int'>Code language: Python (python)

However, the division of two integers always returns a floating-point number. For example:

a = 10 b = 5 c = a / b print(c) print(type(c))Code language: Python (python)
2.0 class 'float'>Code language: Python (python)

Summary

  • Integers are whole numbers that include negative whole numbers, zero, and positive whole numbers.
  • Computers use binary numbers to represent integers.
  • Python uses a variable number of bits to represent integers. Therefore, the largest integer number that Python can represent depends on the available memory of the computer.
  • In Python, all integers are instances of the class int .
  • Use the getsizeof() function of the sys module to get the number of bytes of an integer.
  • Python integers support all standard operations including addition, subtraction, multiplication, division, and exponent.

Источник

Читайте также:  Java base is null
Оцените статью