Home > database >  Maximal value of the Boost Multiprecision type 'float128' gives me a compilation error wit
Maximal value of the Boost Multiprecision type 'float128' gives me a compilation error wit

Time:07-25

This simple code can't be compiled with the -std=c 20 option:

#include <limits>
#include <boost/multiprecision/float128.hpp>

namespace bm = boost::multiprecision;

int main()
{
  auto const m = std::numeric_limits<bm::float128>::max();
}

The compilation command and its error output:

hekto@ubuntu:~$ g   -std=c  20 test.cpp
In file included from test.cpp:2:
/usr/include/boost/multiprecision/float128.hpp: In instantiation of ‘static std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::number_type std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::max() [with boost::multiprecision::expression_template_option ExpressionTemplates = boost::multiprecision::et_off; std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ExpressionTemplates> >::number_type = boost::multiprecision::number<boost::multiprecision::backends::float128_backend, boost::multiprecision::et_off>]’:
test.cpp:8:53:   required from here
/usr/include/boost/multiprecision/float128.hpp:728:55: error: could not convert ‘boost::multiprecision::quad_constants::quad_max’ from ‘const __float128’ to ‘std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, boost::multiprecision::et_off> >::number_type’ {aka ‘boost::multiprecision::number<boost::multiprecision::backends::float128_backend, boost::multiprecision::et_off>’}
  728 |    static number_type (max)() BOOST_NOEXCEPT { return BOOST_MP_QUAD_MAX; }
      |                                                       ^~~~~~~~~~~~~~~~~
      |                                                       |
      |                                                       const __float128

Why is that?

  • OS: Xubuntu 20.04.4 LTS
  • Compiler: g (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0
  • Boost: 1.71.0

CodePudding user response:

The documentation states:

When compiling with gcc, you need to use the flag --std=gnu 11/14/17, as the suffix 'Q' is a GNU extension. Compilation fails with the flag --std=c 11/14/17 unless you also use -fext-numeric-literals.

So you need to specify --std=gnu 20 instead of --std=c 20. The boost documentation is not up-to-date. The flag enables various GNU extensions, one of it being __float128.

See example on godbolt.

The underlying reason for the compiler error is that without the --std=gnu 17 or --std=gnu 20 flag, _GLIBCXX_USE_FLOAT128 is not defined, meaning BOOST_HAS_FLOAT128 is not defined, meaning __float128 is not recognized as number_kind_floating_point (but as number_kind_unknown). This means that boost::multiprecision::number cannot be implicitly constructed from a __float128 because is_restricted_conversion<__float128, boost::multiprecision::float128_backend>::value is true because bm::detail::is_lossy_conversion<__float128, boost::multiprecision::float128_backend>::value is true, because __float128 is number_kind_unknown instead of number_kind_floating_point.

I.e. in short, without gnu 20, the boost::multiprecision::float128 type is not supported.

CodePudding user response:

Directly from the documentation:

When compiling with gcc, you need to use the flag --std=gnu 11/14/17, as the suffix Q is a GNU extension. Compilation fails with the flag --std=c 11/14/17 unless you also use -fext-numeric-literals.

The same applies for c 20/gnu 20, the docs merely haven't yet been updated for that revision.

  • Related