sign.qbk 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. [section:sign_functions Sign Manipulation Functions]
  2. [h4 Synopsis]
  3. ``
  4. #include <boost/math/special_functions/sign.hpp>
  5. ``
  6. namespace boost{ namespace math{
  7. template<class T>
  8. int signbit(T x);
  9. template <class T>
  10. int sign (const T& z);
  11. template <class T, class U>
  12. T copysign (const T& x, const U& y);
  13. template <class T>
  14. ``__sf_result`` changesign (const T& z);
  15. }} // namespaces
  16. [h4 Description]
  17. template<class T>
  18. int signbit(T x);
  19. Returns a non-zero value if the sign bit is set in variable /x/, otherwise `0`.
  20. [important The return value from this function is zero or /not-zero/ and [*not] zero or one.]
  21. template <class T>
  22. int sign (const T& z);
  23. Returns `1` if /x/ `> 0`, `-1` if /x/ `< 0`, and `0` if /x/ is zero.
  24. template <class T, class U>
  25. ``__sf_result`` copysign (const T& x, const U& y);
  26. Sets the sign of /x/ to be the same as the sign of /y/.
  27. See [@http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf C99 7.12.11.1 The copysign functions]
  28. for more detail.
  29. template <class T>
  30. T changesign (const T& z);
  31. Returns a floating-point number with a binary representation
  32. where the signbit is the opposite of the sign bit in /x/,
  33. and where the other bits are the same as in /x/.
  34. This function is widely available, but not specified in any standards.
  35. Rationale: Not specified by TR1, but `changesign(x)`
  36. is both easier to read and more efficient than
  37. copysign(x, signbit(x) ? 1.0 : -1.0);
  38. For finite values, this function has the same effect as simple negation,
  39. the assignment z = -z, but for nonfinite values,
  40. [@http://en.wikipedia.org/wiki/Infinity#Computing infinities]
  41. and [@http://en.wikipedia.org/wiki/NaN NaNs],
  42. the `changesign(x)` function may be the only portable way
  43. to ensure that the sign bit is changed.
  44. [h5 Sign bits]
  45. One of the bits in the binary representation of a floating-point number gives the sign,
  46. and the remaining bits give the absolute value.
  47. That bit is known as the sign bit.
  48. The sign bit is set = 1 for negative numbers, and is not set = 0 for positive numbers.
  49. (This is true for all binary representations of floating-point numbers
  50. that are used by modern microprocessors.)
  51. [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf C++ TR1]
  52. specifies `copysign` functions and function templates for accessing the sign bit.
  53. For user-defined types (UDT), the sign may be stored in some other way.
  54. They may also not provide infinity or NaNs.
  55. To use these functions with a UDT,
  56. it may be necessary to explicitly specialize them for UDT type T.
  57. [h5 Examples]
  58. signbit(3.5) is zero (or false)
  59. signbit(-7.1) is 1 (or true)
  60. copysign(4.2, 7.9) is 4.2
  61. copysign(3.5 -1.4) is -3.5
  62. copysign(-4.2, 1.0) is 4.2
  63. copysign(-8.6, -3.3) is -8.6
  64. changesign(6.9) is -6.9
  65. changesign(-1.8) is 1.8
  66. [h5 Portability]
  67. The library supports the following binary floating-point formats:
  68. * IEEE 754 single precision
  69. * IEEE 754 double precision
  70. * IEEE 754 extended double precision with 15 exponent bits
  71. * Intel extended double precision
  72. * PowerPC extended double precision
  73. * Motorola 68K extended double precision
  74. The library does not support the VAX floating-point formats.
  75. (These are available on VMS, but the default on VMS is the IEEE 754 floating-point format.)
  76. The main portability issues are:
  77. * Unsupported floating-point formats.
  78. * The library depends on the header `boost/detail/endian.hpp` to detemine endianness.
  79. * Code such as `#if defined(__ia64) || defined(__ia64__) || defined(_M_IA64)`
  80. is used to determine the processor type.
  81. The library has passed all tests on the following platforms:
  82. * Win32 / MSVC 7.1 / 10.0 / x86 32 and 64-bit, and later Win32
  83. * Win32 / Intel C++ 7.1, 8.1, 9.1 / x86
  84. * Mac OS X / GCC 3.3, 4.0 / ppc
  85. * Linux / Intel C++ 9.1 / x86, ia64
  86. * Linux / GCC 3.3 / x86, x64, ia64, ppc, hppa, mips, m68k
  87. * Linux / GCC 3.4 / x64
  88. * HP-UX / aCC, GCC 4.1 / ia64
  89. * HP-UX / aCC / hppa
  90. * Tru64 / Compaq C++ 7.1 / alpha
  91. * VMS / HP C++ 7.1 / alpha (in IEEE floating-point mode)
  92. * VMS / HP C++ 7.2 / ia64 (in IEEE floating-point mode)
  93. [endsect] [/section:sign_functions Sign Manipulation Functions]
  94. [/
  95. Copyright 2006 John Maddock and Paul A. Bristow 2011.
  96. Distributed under the Boost Software License, Version 1.0.
  97. (See accompanying file LICENSE_1_0.txt or copy at
  98. http://www.boost.org/LICENSE_1_0.txt).
  99. ]