Tuesday, October 10, 2017

C++17: Hexadecimal Floating-Point Literals

C++17 added hexadecimail floating-point literals. They are of the form 0x<hex_wholenumber>.<hex_fraction>p<power_of_2>. Here is an example:

#include <iostream>

int main()
{
  std::cout << 0x0p0     << " "; // ( 0.0/16.0 + 0.0/16     )*(2.0 ** 0.0)
  std::cout << 0x0.p0    << " "; // ( 0.0/16.0 + 0.0/16     )*(2.0 ** 0.0)
  std::cout << 0x.1p0    << " "; // ( 0.0/16.0 + 1.0/16     )*(2.0 ** 0.0)
  std::cout << 0x0.1p0   << " "; // ( 0.0/16.0 + 1.0/16     )*(2.0 ** 0.0)
  std::cout << 0x1.1p0   << " "; // ( 1.0/16.0 + 1.0/16     )*(2.0 ** 0.0)
  std::cout << 0x1.2p0   << " "; // ( 1.0/16.0 + 2.0/16     )*(2.0 ** 0.0)
  std::cout << 0x2.2p0   << " "; // ( 2.0/16.0 + 2.0/16     )*(2.0 ** 0.0)
  std::cout << 0x2.2p1   << " "; // ( 2.0/16.0 + 2.0/16     )*(2.0 ** 1.0)
  std::cout << 0x2.2p2   << " "; // ( 2.0/16.0 + 2.0/16     )*(2.0 ** 2.0)
  std::cout << 0x10.01p0 << " "; // (16.0/16.0 + 1.0/(16*16))*(2.0 ** 0.0)
  std::cout << 0x0.001p0 << " "; // (16.0/16.0 + 1.0/(16**3))*(2.0 ** 0.0)

  std::cout << std::endl;
  return 0;
}
// Output: 0 0 0.0625 0.0625 1.0625 1.125 2.125 4.25 8.5 16.0039 0.000244141
References:

No comments:

Post a Comment