简体   繁体   English

从“size_t”到“const double”的转换,可能会丢失数据

[英]conversion from 'size_t' to 'const double', possible loss of data

Given a variable size_t idx , an assignment like double value = idx;给定一个变量size_t idx ,一个像double value = idx;这样的赋值double value = idx; produces the following warning when compiling under 64 bit:在 64 位下编译时产生以下警告:

'initializing': conversion from 'size_t' to 'double', possible loss of data

It seems that it has to do with the fact that a size_t value takes up 8 bytes when compiled under 64 bit.这似乎与在 64 位下编译时size_t值占用 8 个字节有关。 However, the assignment double value = static_cast<double>(idx) does not produce any warnings.但是,赋值double value = static_cast<double>(idx)不会产生任何警告。 Can somebody explain to me why the former assignment does not work while the latter does?有人可以向我解释为什么前一个任务不起作用而后者起作用吗?

Thanks in advance!提前致谢!

When you do当你做

double value = idx;

The compiler looks at it and goes: "Hey, idx might not be able to fit in value , lets tell the programmer in case it was a mistake.".编译器查看它并说:“嘿, idx可能无法放入value ,让我们告诉程序员,以防它出错。”。 This is why you get the warning.这就是您收到警告的原因。

When you do当你做

double value = static_cast<double>(idx)

The compiler looks at it and goes: "Oh, the programmer is explicitly casting here, this must be what they want.".编译器看着它,然后说:“哦,程序员在这里明确地进行了转换,这一定是他们想要的。”。 That means it wont surface a warning.这意味着它不会出现警告。

static_cast<double> is how you tell the compiler that you are consciously converting to double and are aware of all the implications.static_cast<double>是您告诉编译器您有意识地转换为double并了解所有含义的方式。 This means the compiler should not to warn you of the risks because you have explicitly told it you are already aware of them.这意味着编译器不应该警告您风险,因为您已经明确告诉它您已经意识到它们。

In the first case, where you do not use static_cast , the conversion is legal but carries the risk of losing precision.在第一种情况下,您不使用static_cast ,转换是合法的,但有失去精度的风险。 Since it's possible you didn't notice that this conversion was happening, it makes sense to warn you about it.由于您可能没有注意到这种转换正在发生,因此向您发出警告是有意义的。

Both assignments will 'work' and, in fact, do exactly the same thing!这两项任务都将“有效”,而且实际上,做的事情完全一样! The only difference is that in the second case, double value = static_cast<double>(idx) , your explicit cast makes it clear to the compiler that you "know what you're doing" and that you may lose significant digits in the conversion.唯一的区别是,在第二种情况下, double value = static_cast<double>(idx) ,您的显式转换向编译器表明您“知道自己在做什么”并且您可能会在转换中丢失有效数字.

In the first case, the compiler is simply warning you that your conversion may (inadvertently) cause loss of data (or precision, in this case).在第一种情况下,编译器只是警告您您的转换可能(无意中)导致数据丢失(或精度,在这种情况下)。

It is a warning because the programmer may not realize they are losing bits during assignment.这是一个警告,因为程序员可能没有意识到他们在赋值期间丢失了位。

But if you have an explicit cast there, the compiler assumes that the programmer knows what they're doing because the cast is right there and very in your face.但是如果你在那里有一个显式的演员,编译器假设程序员知道他们在做什么,因为演员就在那里并且非常在你面前。

A 64-bit double uses only 52 bits to store the number and the rest for storing the sign( + or - ) and the exponent. 64 位 double 仅使用 52 位来存储数字,其余部分用于存储符号( +- )和指数。 ( Note: This is the IEEE754 implementation, the implementation of double may differ in other standards of the language) 注意:这是IEEE754的实现, double的实现可能在其他语言标准中有所不同)

An unsigned int (aka size_t ) uses all of the 64-bits to store the number but there are no spaces left for exponents and hence there is no floating-point precision in integers like size_t unsigned int (又名size_t )使用所有 64 位来存储数字,但没有为指数留下空格,因此像size_t这样的整数没有浮点精度

Therefore, the compiler is telling you that if your 64-bit unsigned integer is greater than how much the mantissa of the 64-bit double -type can store (ie, larger than 52 bits ), it can cause loss of data, and possibly Undefined Behavior in the rest of the code afterwards.因此,编译器会告诉您,如果您的 64 位无符号整数大于 64 位double类型的尾数可以存储的数量(即大于52 位),则可能会导致数据丢失,并且可能导致之后其余代码中的未定义行为。

Doing static_cast<double>(idx) is an assertion by the programmer telling the compiler to not worry and that you will keep in mind the aforementioned precautions.执行static_cast<double>(idx)是程序员的断言,告诉编译器不要担心,并且您将牢记上述预防措施。

Source: https://en.wikipedia.org/wiki/Double-precision_floating-point_format来源: https : //en.wikipedia.org/wiki/Double-precision_floating-point_format

暂无
暂无

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

相关问题 C4267:“返回”:从“ size_t”转换为“ const UINT”,可能丢失数据 - C4267: 'return' : conversion from 'size_t' to 'const UINT', possible loss of data 从size_t转换为DWORD,可能会丢失数据 - Conversion from size_t to DWORD, possible loss of data 警告-从size_t转换为DWORD,可能会丢失数据 - Warning - Conversion from size_t to DWORD, possible loss of data Visual Studio 中的警告 C4267:“参数”:从“size_t”转换为“const _Elem”,可能丢失数据 - Warning C4267 in Visual Studio: 'argument': conversion from 'size_t' to 'const _Elem', possible loss of data 警告C4267“正在初始化”:从“ size_t”到“ DWORD”的转换,可能丢失数据 - Warning C4267 'initializing': conversion from 'size_t' to 'DWORD', possible loss of data C4244:“+=”:从“std::streamsize”到“size_t”的转换,可能丢失数据 - C4244: '+=' : conversion from 'std::streamsize' to 'size_t', possible loss of data 警告C4244:&#39;参数&#39;:从&#39;SIZE_T&#39;转换为&#39;DWORD&#39;,可能会丢失数据 - warning C4244: 'argument' : conversion from 'SIZE_T' to 'DWORD', possible loss of data 从 'wchar_t' 转换为 'const _Elem',可能会丢失数据 - Conversion from 'wchar_t' to 'const _Elem', possible loss of data 在 &quot;&quot; 运算符中从字符串转换为 const char* + size_t - Conversion from string to const char* + size_t in ""operator 常量size_t在mex中的转换 - Conversion of const size_t in mex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM