如果变体具有 INF 或 NAN 值,如何使变体的 ChangeType 浮动?

发布于 2024-10-06 19:36:15 字数 837 浏览 8 评论 0原文

我在 Variant 中有浮点值,最初这个变体是 bstr 类型。 我正在使用 VariantChangeType 更改为浮动类型。然后我从中获取浮点值,然后转换为字节向量格式。

如果变量具有浮点值,则效果很好。 如果变体具有 INF 值,则 VariantChangeType 会失败。所以我从variant.fltVal得到0。

如果变量具有 INF 或 NAN 值,如何将类型更改为浮动?我想获取浮点型 INF 或 NAN 值。

Convert(_variant_t varVal,vector<byte> dataArr)
{
    HRESULT hr = VariantChangeType( varVal&, &varVal, 0 , VT_R4 ); 

   // Above code is failing if varVal.bstr contains 1.#INF" values 

    UINT size = 4;
    if ( data && dataItem.vt != VT_EMPTY )
    {
     float value = dataItem.fltVal;
     BYTE tmpBuf[4];
     memcpy(tmpBuf, &value, sizeof(value));
     for( int i = size - 1 ; i >= 0 ; i-- )
     {    
         data->push_back(tmpBuf[i]);
     }    
     retVal = TRUE;
   }
}

I am having float value in Variant, Initially this variant is a bstr type.
I am using VariantChangeType to change to float type. and then I am getting float value from it and then i am converting into byte vector format.

If variant has float values this works fine.
If variant is having INF values, VariantChangeType gets failed. So i am getting 0 from variant.fltVal.

How to Changetype to float if variant having INF or NAN values? I want to get INF or NAN values in float.

Convert(_variant_t varVal,vector<byte> dataArr)
{
    HRESULT hr = VariantChangeType( varVal&, &varVal, 0 , VT_R4 ); 

   // Above code is failing if varVal.bstr contains 1.#INF" values 

    UINT size = 4;
    if ( data && dataItem.vt != VT_EMPTY )
    {
     float value = dataItem.fltVal;
     BYTE tmpBuf[4];
     memcpy(tmpBuf, &value, sizeof(value));
     for( int i = size - 1 ; i >= 0 ; i-- )
     {    
         data->push_back(tmpBuf[i]);
     }    
     retVal = TRUE;
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靑春怀旧 2024-10-13 19:36:15

用伪语言:

 if ( varVal.bstr.contains("1.#INF" ) ) {
   // assign plus infinity to your variant see http://stackoverflow.com/questions/2538339/infinity-in-msvc 
 } else if ( varVal.bstr.contains("-1.#INF" ) ) {
   // assign minus infinity to your variant see http://stackoverflow.com/questions/2538339/infinity-in-msvc 
 } else if ( varVal.bstr.contains("NaN" ) {
   // assign NaN to your variant see http://stackoverflow.com/questions/235386/using-nan-in-c
 } else {
   // your original working code for regular float values
 }

请注意,因为这是一个脆弱的解决方案:它依赖于无穷大和 NaN 的字符串表示形式,也许微软可以在没有警告的情况下更改它们。

这是Raymond Chen的非官方但权威的信息:
-1.#IND 是什么意思?:关于如何Visual C 运行时库会打印特殊的浮点值,即使它与 VARIANT 无关。

Output  Meaning
1#INF   Positive infinity
-1#INF  Negative infinity
1#SNAN  Positive signaling NaN
-1#SNAN Negative signaling NaN
1#QNAN  Positive quiet NaN
-1#QNAN Negative quiet NaN
1#IND   Positive indefinite NaN
-1#IND  Negative indefinite NaN

官方信息是printf类型字段字符

in pseudo-language:

 if ( varVal.bstr.contains("1.#INF" ) ) {
   // assign plus infinity to your variant see http://stackoverflow.com/questions/2538339/infinity-in-msvc 
 } else if ( varVal.bstr.contains("-1.#INF" ) ) {
   // assign minus infinity to your variant see http://stackoverflow.com/questions/2538339/infinity-in-msvc 
 } else if ( varVal.bstr.contains("NaN" ) {
   // assign NaN to your variant see http://stackoverflow.com/questions/235386/using-nan-in-c
 } else {
   // your original working code for regular float values
 }

Please pay attention because it is a fragile solution: it relies on the string representations of infinity and NaN and maybe Microsoft can change them without warning.

This is an unofficial yet authoritative information by Raymond Chen:
What does -1.#IND mean?: A survey of how the Visual C runtime library prints special floating point values, even if it is not related to VARIANT.

Output  Meaning
1#INF   Positive infinity
-1#INF  Negative infinity
1#SNAN  Positive signaling NaN
-1#SNAN Negative signaling NaN
1#QNAN  Positive quiet NaN
-1#QNAN Negative quiet NaN
1#IND   Positive indefinite NaN
-1#IND  Negative indefinite NaN

An official information is printf Type Field Characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文