1 /////////////////////////////////////////////////////////////// 2 // Copyright 2015 John Maddock. Distributed under the Boost 3 // Software License, Version 1.0. (See accompanying file 4 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt 5 6 #include <boost/multiprecision/cpp_bin_float.hpp> 7 #include <iostream> 8 #include <iomanip> 9 #include <vector> 10 #include <iterator> 11 12 // Contains Quickbook snippets in comments. 13 14 //[IE2 15 16 /*` 17 Importing or exporting cpp_bin_float is similar, but we must proceed via an intermediate integer: 18 */ 19 /*= 20 #include <boost/multiprecision/cpp_bin_float.hpp> 21 #include <iostream> 22 #include <iomanip> 23 #include <vector> 24 #include <iterator> 25 */ 26 main()27int main() 28 { 29 using boost::multiprecision::cpp_bin_float_100; 30 using boost::multiprecision::cpp_int; 31 // Create a cpp_bin_float to import/export: 32 cpp_bin_float_100 f(1); 33 f /= 3; 34 // export into 8-bit unsigned values, most significant bit first: 35 std::vector<unsigned char> v; 36 export_bits(cpp_int(f.backend().bits()), std::back_inserter(v), 8); 37 // Grab the exponent as well: 38 int e = f.backend().exponent(); 39 // Import back again, and check for equality, we have to proceed via 40 // an intermediate integer: 41 cpp_int i; 42 import_bits(i, v.begin(), v.end()); 43 cpp_bin_float_100 g(i); 44 g.backend().exponent() = e; 45 BOOST_ASSERT(f == g); 46 } 47 48 //] 49 50