Python与C类型的float,double和long类型等效吗? [英] What is the Python equivalent of C types float, double and long?

查看:265
本文介绍了Python与C类型的float,double和long类型等效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些C类型的Python等效于什么:

What is the Python equivalent of these C types:

float
double 
long

我想代表上述三个区域中的 float

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

推荐答案

我相信您的意思是类型,而不是函数。在C语言中, double 只是双精度 float (意味着较小的数字会更准确地表示)。在Python中,它只是一个 float 。类似地, long 只是(<)$ int 存储更大的数字。

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.

在Python 2中,有 int long 浮动。但是,当Python增长到 sys.intmax int 提升为 long 。 c $ c>。这意味着在Python中使用 long 毫无意义。它会在需要时自动变成 long 浮动在内部表示为 double

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.

简短的答案是只使用 int float ,而不必担心基础实现。

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

您可以将 int 转换为 float (并返回),像这样:

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)

查看有关标准Python类型的更多信息这里

See more about the standard Python types here.

这篇关于Python与C类型的float,double和long类型等效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆