Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 708923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:26:46+00:00 2026-05-14T04:26:46+00:00

Double has range more than a 64-bit integer, but its precision is less dues

  • 0

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:26:46+00:00Added an answer on May 14, 2026 at 4:26 am

    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>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The button is optional. You can link the TextField's "Value… May 15, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer Have you considered using the information_schema views instead. E.g. Select… May 15, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer The following does work: <chmod file="${basedir}/foo/**" perm="g+w" type="both"/> Credits shared… May 15, 2026 at 4:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.