What is the Python equivalent of C types float, double and long?

感情迁移 提交于 2020-08-11 03:23:40

问题


What is the Python equivalent of these C types:

float
double 
long

I want to represent a float in the above three forms.


回答1:


I believe you mean types, not functions. In C, a double is just a double-precision float (meaning smaller numbers are represented more accurately). In Python, it's just a float. Similarly, a long is (was) just an int that stored larger numbers.

In Python 2, there is int, long, and float. However, Python automatically promotes int to long when it grows above sys.intmax. That means there is no point in ever using long in Python. It will turn into a long automatically when it needs to. A float is represented as a double internally.

The short answer is to just use int and float, and don't worry about the underlying implementation. Python abstracts that away for you.

You can convert an int to a float (and back) like this:

#!/usr/bin/python

number = 45
real_number = 3.14159265359

number_as_float = float(number)
real_number_as_int = int(real_number)

See more about the standard Python types here.



来源:https://stackoverflow.com/questions/34960636/what-is-the-python-equivalent-of-c-types-float-double-and-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!