简体   繁体   English

什么情况下使“ 1.0”(带.0)这样的浮点数成为整数?

[英]What situation makes a float-point like “1.0” (with .0) an integer?

I wanted to know if there is such a thing as a integer even tho it has a .0 decimal at the end. 我想知道是否有整数这样的东西,即使它的末尾有.0小数。 Is it considered a float or an integer? 它是浮点数还是整数?

It's still a float. 它仍然是一个浮动。 For example, do 例如,做

print(type(1.0))

It prints float . 它打印出float In general, anything with a decimal point is a float . 通常,任何带有小数点的东西都是float

In a programming language, what it means to be a "float" versus an "integer" is to have a particular binary representation in the machine. 在编程语言中,“浮点数”与“整数”的含义是在计算机中具有特定的二进制表示形式。

In Python specifically, which is not statically typed, if you write x = 1.0 then x will be a floating point value. 特别是在非静态类型的Python中,如果您编写x = 1.0x将是一个浮点值。 It is stored in memory using a floating point representation, such as IEEE-754 . 它使用浮点表示形式(例如IEEE-754 )存储在内存中。 If you write x = 1 , then x will be an integer. 如果您写x = 1 ,则x将为整数。 You are telling Python which representation of 1 you want. 您正在告诉Python您想要1的哪种表示形式。 If you were writing in C and you write, int x = 1.0 , then x would still be an integer, since the compiler knows you want x to be an integer, and the compiler will convert it or generate code to do so. 如果您使用C编写并且编写int x = 1.0 ,则x仍将是整数,因为编译器知道您希望x为整数,并且编译器将转换它或生成代码来这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM