Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Python floats have real and imag attributes?

Tags:

python

At least in Python 3, float has attributes real and imag, and a method conjugate(). Since issubclass(float, complex) evaluates to False, what is the reason for these?

like image 849
Roméo Després Avatar asked May 29 '19 09:05

Roméo Després


People also ask

Is Python float 32 or 64?

Python's floating-point numbers are usually 64-bit floating-point numbers, nearly equivalent to np. float64 . In some unusual situations it may be useful to use floating-point numbers with more precision.

What is float data type in Python?

The float type in Python represents the floating point number. Float is used to represent real numbers and is written with a decimal point dividing the integer and fractional parts. For example, 97.98, 32.3+e18, -32.54e100 all are floating point numbers.

How do you represent a float in Python?

Float() returns the value based on the argument or parameter value that is being passed to it. If no value or blank parameter is passed, it will return the values 0.0 as the floating-point output. Python float values are represented as 64-bit double-precision values.

What is a float function?

Definition and Usage The float() function converts the specified value into a floating point number.


1 Answers

It is obviously a design choice and it is very well rooted in Python numeric types (i.e. bool, int, float, complex), as clear from the source code (e.g. for float).

This has been discussed in PEP 3141, which resulted in the numbers module for Numeric abstract base classes module. As you can see, .real, .imag and .conjugate() are part of the generic Number abstraction.

From a practical perspective, this means that any numeric algorithm can be safely written for complex and it will gracefully work for any Number subtype.

like image 95
norok2 Avatar answered Oct 07 '22 18:10

norok2