查找浮点类型可以在不损失精度的情况下处理的最大整数大小

发布于 2024-08-27 09:43:20 字数 1064 浏览 2 评论 0原文

Double 的范围大于 64 位整数,但由于其表示形式,其精度较低(因为 double 也是 64 位,因此无法容纳更多实际值)。因此,当表示较大的整数时,您开始失去整数部分的精度。

#include <boost/cstdint.hpp>
#include <limits>

template<typename T, typename TFloat>
void
maxint_to_double()
{
    T i = std::numeric_limits<T>::max();
    TFloat d = i;
    std::cout
        << std::fixed
        << i << std::endl
        << d << std::endl;
}

int
main()
{
    maxint_to_double<int, double>();
    maxint_to_double<boost::intmax_t, double>();
    maxint_to_double<int, float>();
    return 0;
}

打印:

2147483647
2147483647.000000
9223372036854775807
9223372036854775800.000000
2147483647
2147483648.000000

注意 max int 如何适应 double 而不会损失精度和 boost::intmax_t (在本例中为 64 位)不能。 float 甚至无法容纳 int

现在的问题是:在 C++ 中是否有一种方法可以检查给定整数类型的整个范围是否可以适合浮点类型而不损失精度?

最好

  • 是编译时检查可以在静态断言中使用,
  • 并且不会涉及枚举编译器应该知道或可以计算的常量。

Double has range more than a 64-bit integer, but its precision is less dues to its representation (since double is 64-bit as well, it can't fit more actual values). So, when representing larger integers, you start to lose precision in the integer part.

#include <boost/cstdint.hpp>
#include <limits>

template<typename T, typename TFloat>
void
maxint_to_double()
{
    T i = std::numeric_limits<T>::max();
    TFloat d = i;
    std::cout
        << std::fixed
        << i << std::endl
        << d << std::endl;
}

int
main()
{
    maxint_to_double<int, double>();
    maxint_to_double<boost::intmax_t, double>();
    maxint_to_double<int, float>();
    return 0;
}

This prints:

2147483647
2147483647.000000
9223372036854775807
9223372036854775800.000000
2147483647
2147483648.000000

Note how max int can fit into a double without loss of precision and boost::intmax_t (64-bit in this case) cannot. float can't even hold an int.

Now, the question: is there a way in C++ to check if the entire range of a given integer type can fit into a loating point type without loss of precision?

Preferably,

  • it would be a compile-time check that can be used in a static assertion,
  • and would not involve enumerating the constants the compiler should know or can compute.

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

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

发布评论

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

评论(2

记忆之渊 2024-09-03 09:43:20

只是一个小谓词:

#include <limits>

template <typename T, typename U>
struct can_fit
{
    static const bool value = std::numeric_limits<T>::digits
                            <= std::numeric_limits<U>::digits;
};

#include <iostream>

int main(void)
{
    std::cout << std::boolalpha;

    std::cout << can_fit<short, float>::value << std::endl;
    std::cout << can_fit<int, float>::value << std::endl;

    std::cout << can_fit<int, double>::value << std::endl;
    std::cout << can_fit<long long, double>::value << std::endl;

    std::cout << can_fit<short, int>::value << std::endl;
    std::cout << can_fit<int, short>::value << std::endl;
}

测试 T 中可用的二进制精度是否存在于 U 中。适用于所有类型。


“增强”:

// this is just stuff I use
#include <boost/type_traits/integral_constant.hpp>

template <bool B>
struct bool_type : boost::integral_constant<bool, B>
{
    static const bool value = B;
};

typedef const boost::true_type& true_tag;
typedef const boost::false_type& false_tag;

// can_fit type traits
#include <limits>

namespace detail
{
    template <typename T, typename U>
    struct can_fit
    {
        static const bool value = std::numeric_limits<T>::digits
                                <= std::numeric_limits<U>::digits;
    };
}

template <typename T, typename U>
struct can_fit : bool_type<detail::can_fit<T, U>::value>
{
    typedef T type1;
    typedef U type2;

    static const bool value = detail::can_fit<T, U>::value;
};

// test
#include <iostream>

namespace detail
{
    void foo(true_tag)
    {
        std::cout << "T fits in U" << std::endl;
    }

    void foo(false_tag)
    {
        std::cout << "T does not fit in U" << std::endl;
    }
}

// just an example
template <typename T, typename U>
void foo(void)
{
    detail::foo(can_fit<T, U>());
}

int main(void)
{
    foo<int, double>();
}

Just a little predicate:

#include <limits>

template <typename T, typename U>
struct can_fit
{
    static const bool value = std::numeric_limits<T>::digits
                            <= std::numeric_limits<U>::digits;
};

#include <iostream>

int main(void)
{
    std::cout << std::boolalpha;

    std::cout << can_fit<short, float>::value << std::endl;
    std::cout << can_fit<int, float>::value << std::endl;

    std::cout << can_fit<int, double>::value << std::endl;
    std::cout << can_fit<long long, double>::value << std::endl;

    std::cout << can_fit<short, int>::value << std::endl;
    std::cout << can_fit<int, short>::value << std::endl;
}

Tests if the binary precision available in a T exists in a U. Works on all types.


"Boostified":

// this is just stuff I use
#include <boost/type_traits/integral_constant.hpp>

template <bool B>
struct bool_type : boost::integral_constant<bool, B>
{
    static const bool value = B;
};

typedef const boost::true_type& true_tag;
typedef const boost::false_type& false_tag;

// can_fit type traits
#include <limits>

namespace detail
{
    template <typename T, typename U>
    struct can_fit
    {
        static const bool value = std::numeric_limits<T>::digits
                                <= std::numeric_limits<U>::digits;
    };
}

template <typename T, typename U>
struct can_fit : bool_type<detail::can_fit<T, U>::value>
{
    typedef T type1;
    typedef U type2;

    static const bool value = detail::can_fit<T, U>::value;
};

// test
#include <iostream>

namespace detail
{
    void foo(true_tag)
    {
        std::cout << "T fits in U" << std::endl;
    }

    void foo(false_tag)
    {
        std::cout << "T does not fit in U" << std::endl;
    }
}

// just an example
template <typename T, typename U>
void foo(void)
{
    detail::foo(can_fit<T, U>());
}

int main(void)
{
    foo<int, double>();
}
执笏见 2024-09-03 09:43:20

您可以使用 std::numeric_limits::digits 来了解您的二进制精度。例如:

int binary_digits_double = numeric_limits<double>::digits;       // 53
int binary_digits_long_long = numeric_limits<long long>::digits; // 63
int binary_digits_uint = numeric_limits<unsigned int>::digits;   // 32

You can use std::numeric_limits<T>::digits to know how much binary precision you have. e.g:

int binary_digits_double = numeric_limits<double>::digits;       // 53
int binary_digits_long_long = numeric_limits<long long>::digits; // 63
int binary_digits_uint = numeric_limits<unsigned int>::digits;   // 32
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文