简体   繁体   English

将float转换为int会截断其余部分,似乎不可靠

[英]Casting float to int truncates rest, seem not reliable

Being somehow surprised seeing things like this work: 看到这样的事情使我有些惊讶:

float f = 10.25f;
int i = (int)f;
// Will give you i = 10

What is the gain? 收益是多少?

OTOH 10.25 is quite a different thing than 10, which will be agreed, bad things might happen from such a soft conversion. OTOH 10.25与10完全不同,这将是一致的,这种软转换可能会发生不好的事情。

Which languages raise an error instead? 哪些语言会引发错误?

Would expect someting like: "Error: Can't represent 10.25 as an integer" . 期望有些像: "Error: Can't represent 10.25 as an integer"

WRT to answers given meanwhile: Yes, it might considered reliable the way a function like "round" is. WRT同时给出答案:是的,它可以像“回合”这样的函数认为可靠。 But not straight WRT to integrity of data/information to be expected from cast. 但是,WRT并不能直接实现铸造期望的数据/信息完整性。

Maybe a function "truncate" which defaults to behavior of "int" would make a better choice? 也许一个默认为“int”行为的函数“truncate”会有更好的选择吗?

It is precisely the 恰恰是

(int)f

that tells that the programmer is aware of what he is doing, while silently cutting off the fractional part and storing the rest in an integer is forbidden in most programming languages. 这表明程序员知道自己在做什么,而在大多数编程语言中,禁止静默切掉小数部分并将其余部分存储为整数。

By the way, it is not just that the fractional part is cut off. 顺便说一句,不仅仅是小部分被切断了。 It is also that a floating point number can have a value so large that it can't possibly be represented as an int. 浮点数也可以具有如此大的值,以至于它不可能表示为int。 Consider: 考虑:

(int) 1e20f

The statement int i = (int)f; 语句int i = (int)f; explicitly says "Please take my float f and make it into int ". 明确地说“请把我的浮动f变成int ”。 This is certainly something I quite often find a useful thing to do - why wouldn't you want to be able to convert a float value from a calculation of some sort to an integer? 当然,这是我经常发现有用的事情-为什么您不希望能够将浮点值从某种类型的计算转换为整数? The cast (int) will tell the compiler that "I really want this to be an integer", just like in C you can do char *p = (char *)1234567; 强制转换(int)将告诉编译器“我真的希望它是一个整数”,就像在C语言中一样,可以执行char *p = (char *)1234567; - a typecast is there to tell the compiler "I really know what I'm doing". -在那里有一个类型转换,告诉编译器“我真的知道我在做什么”。

If you do int i = f; 如果你做int i = f; or int i = 10.25; 或者int i = 10.25; the compiler will still do what you "asked for" - convert the float value to an integer. 编译器仍然会执行“请求” - 将float值转换为整数。 It will probably issue a warning to say "You are converting a float to int ", if you enable the appropriate warnings. 如果启用了适当的警告,则可能会发出警告,说“您正在将float转换为int ”。

C and C++ are languages that require you to understand what you are doing, and what the consequences are - some other languages put more "barriers" in place to prevent such things, but that often means that the compiler has to add extra code to check things at runtime - C and C++ are designed to be "fast" languages. C和C ++是需要你理解你在做什么的语言,以及后果是什么 - 其他一些语言为防止这些事情设置了更多“障碍”,但这通常意味着编译器必须添加额外的代码来检查运行时的事物-C和C ++被设计为“快速”语言。

It's a bit like driving a car, putting the car in reverse when there is a wall right behind, and stepping on the gas, will probably cause the car to crash into the wall - if that's not what you want, then "don't do that". 这有点像驾驶汽车,当后面有一堵墙时将汽车反转,然后踩油门,可能会导致汽车撞到墙上 - 如果这不是你想要的,那么“不要去做”。

Firstly, the conversion is most definitely "reliable", as in "it will always do the same thing". 首先,转换绝对是“可靠的”,就像“它总是会做同样的事情”一样。

Whether you want to do that or not is up to you. 您是否愿意这样做取决于您自己。 In general the C/C++ languages are designed to give the programmer a lot of low-level power, and that means that the programmer needs to know what they are doing . 通常,C / C ++语言旨在为程序员提供许多底层功能,这意味着程序员需要知道他们在做什么 If a float-to-int conversion surprises you then you need to think harder. 如果float-to-int转换让你感到惊讶,那么你需要更加努力思考。

In fact, GCC has an option -Wconversion that will highlight cases like this. 实际上,GCC有一个选项-Wconversion ,可以突出显示这样的情况。 It isn't enabled by default, and is not part of -Wall or -Wextra (presumably because the behaviour is well understood and "expected" by most programmers), but the option is there if you need it. 默认情况下不启用它,并且它不是-Wall-Wextra一部分(可能是因为大多数程序员都很好地理解和“预期”了这种行为),但如果你需要它,那么选项就在那里。

Except that it won't give a warning, in this case, because your code includes an explicit cast (int) , so the compiler assumes you did it deliberately. 除非它不会发出警告,在这种情况下,因为您的代码包含一个显式的强制转换(int) ,所以编译器假定您是故意这样做的。

This gives a warning (with -Wconversion ): 这会发出警告(使用-Wconversion ):

int i = f;

This does not: 这不是:

int i = (int)f;

Converting to an integer is useful in cases where you are working with complex data, but ultimately need to convert this data to an int to do something with it. 在使用复杂数据的情况下,转换为整数非常有用,但最终需要将此数据转换为int以对其执行某些操作。 Think of offsets in arrays, or pixels on a screen 想想数组中的偏移或屏幕上的像素

Think of drawing a circle on the screen. 想想在屏幕上画一个圆圈。 There does not exist a fraction of a pixel (so the coordinates are ints), but you cannot calculate the coordinates of the pixel with just ints (sinus works with pi and other floats). 不存在像素的一小部分(因此坐标为整数),但是您不能仅使用整数来计算像素的坐标(窦与pi和其他浮点数一起使用)。

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

相关问题 错误转换为float,然后在python中转换为int - Error casting to float, then int in python Tensorflow - 从int转换为float奇怪的行为 - Tensorflow - casting from int to float strange behavior 将float转换为int会导致错误的值 - Casting float to int results in incorrect value Numpy 妨碍了 int -&gt; float 类型转换 - Numpy getting in the way of int -> float type casting 将一个“整数”浮点数转换为int总是返回最接近的整数? - Will casting an “integer” float to int always return the closest integer? Tensorflow:最小化int64数据上的L2损失而不转换为float32,因为转换给出“无梯度”错误 - Tensorflow: minimize L2 loss on int64 data without casting to float32 because casting gives “no gradients” error 类型错误:&#39;numpy.float64&#39; 对象不能被解释为整数并且转换为 int 失败 - TypeError: 'numpy.float64' object cannot be interpreted as an integer and casting to int fails Pandas 如何用 int64 null 标记 -999999 替换<na>没有铸造浮动?</na> - Pandas how to replace int64 null marker -999999 with <NA> without casting to float? Python - 将浮点数转换为 int 以用作数组索引返回数组的第 0 个元素 - Python - Casting a float as int to be used as array index returns 0th element of the array Python强制转换元组[int,int] - Python casting Tuple[int, int]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM