Menu

Convert minimum integer value of signed type to half

Rui Wang
2025-01-06
2025-02-18
  • Rui Wang

    Rui Wang - 2025-01-06

    Hi,

    Thank you for making this library, it really helps a lot in my work.

    But recently, I have found that when I use half_cast() to convert the minimum value of short type (-32768) to half, I get -1, but it should be -32768. This problem also occurs when converting the minimum value of int type. I'm not sure, but it may be related to the overflow in half. hpp: 1082 line, or did I miss something?

    The following code can reproduce this issue.

    short x = std::numeric_limits<short>::min();
    std::cout << x << '\n';    //  -32768
    std::cout << half_float::half_cast<half_float::half>(x) << '\n'; //    -1
    
     
    • Christian Rau

      Christian Rau - 2025-02-18

      Yes, there is indeed a bug in the integer to half conversion when calling it with the type's minimum negative value, since on two's complement platforms (which is virtually everywhere) that doesn't have a positive equivalent inside the type's own domain. I will look into fixing it.

      Thank you for bringing this up.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.