result_type_calc.qbk 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. [section:result_type Calculation of the Type of the Result]
  2. The functions in this library are all overloaded to accept
  3. mixed floating point (or mixed integer and floating point type)
  4. arguments. So for example:
  5. foo(1.0, 2.0);
  6. foo(1.0f, 2);
  7. foo(1.0, 2L);
  8. etc, are all valid calls, as long as "foo" is a function taking two
  9. floating-point arguments. But that leaves the question:
  10. [blurb ['"Given a special function with N arguments of
  11. types T1, T2, T3 ... TN, then what type is the result?"]]
  12. [*If all the arguments are of the same (floating point) type then the
  13. result is the same type as the arguments.]
  14. Otherwise, the type of the result
  15. is computed using the following logic:
  16. # Any arguments that are not template arguments are disregarded from
  17. further analysis.
  18. # For each type in the argument list, if that type is an integer type
  19. then it is treated as if it were of type `double` for the purposes of
  20. further analysis.
  21. # If any of the arguments is a user-defined class type, then the result type
  22. is the first such class type that is constructible from all of the other
  23. argument types.
  24. # If any of the arguments is of type `long double`, then the result is of type
  25. `long double`.
  26. # If any of the arguments is of type `double`, then the result is of type
  27. `double`.
  28. # Otherwise the result is of type `float`.
  29. For example:
  30. cyl_bessel(2, 3.0);
  31. Returns a `double` result, as does:
  32. cyl_bessel(2, 3.0f);
  33. as in this case the integer first argument is treated as a `double` and takes
  34. precedence over the `float` second argument. To get a `float` result we would need
  35. all the arguments to be of type float:
  36. cyl_bessel_j(2.0f, 3.0f);
  37. When one or more of the arguments is not a template argument then it
  38. doesn't effect the return type at all, for example:
  39. sph_bessel(2, 3.0f);
  40. returns a `float`, since the first argument is not a template argument and
  41. so doesn't effect the result: without this rule functions that take
  42. explicitly integer arguments could never return `float`.
  43. And for user-defined types, typically __multiprecision,
  44. All of the following return a `boost::multiprecision::cpp_bin_quad_float` result:
  45. cyl_bessel_j(0, boost::multiprecision::cpp_bin_quad_float(2));
  46. cyl_bessel_j(boost::multiprecision::cpp_bin_quad_float(2), 3);
  47. cyl_bessel_j(boost::multiprecision::cpp_bin_quad_float(2), boost::multiprecision::cpp_bin_quad_float(3));
  48. but rely on the parameters provided being exactly representable, avoiding loss of precision from construction from `double`.
  49. [tip All new projects should use Boost.Multiprecision.]
  50. During development of Boost.Math, __NTL was invaluable to create highly precise tables.
  51. All of the following return an `NTL::RR` result:
  52. cyl_bessel_j(0, NTL::RR(2));
  53. cyl_bessel_j(NTL::RR(2), 3);
  54. cyl_bessel_j(NTL::quad_float(2), NTL::RR(3));
  55. In the last case, `quad_float` is convertible to `RR`, but not vice-versa, so
  56. the result will be an `NTL::RR`. Note that this assumes that you are using
  57. a [link math_toolkit.high_precision.use_ntl patched NTL library].
  58. These rules are chosen to be compatible with the behaviour of
  59. ['ISO/IEC 9899:1999 Programming languages - C]
  60. and with the
  61. [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf Draft Technical Report on C++ Library Extensions, 2005-06-24, section 5.2.1, paragraph 5].
  62. [endsect] [/section:result_type Calculation of the Type of the Result]
  63. [/
  64. Copyright 2006, 2012 John Maddock and Paul A. Bristow.
  65. Distributed under the Boost Software License, Version 1.0.
  66. (See accompanying file LICENSE_1_0.txt or copy at
  67. http://www.boost.org/LICENSE_1_0.txt).
  68. ]