math.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2014, 2015, 2018, 2019.
  6. // Modifications copyright (c) 2014-2019, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Contributed and/or modified by Adeel Ahmad, as part of Google Summer of Code 2018 program
  10. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  11. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_UTIL_MATH_HPP
  16. #define BOOST_GEOMETRY_UTIL_MATH_HPP
  17. #include <cmath>
  18. #include <limits>
  19. #include <boost/core/ignore_unused.hpp>
  20. #include <boost/math/constants/constants.hpp>
  21. #include <boost/math/special_functions/fpclassify.hpp>
  22. //#include <boost/math/special_functions/round.hpp>
  23. #include <boost/numeric/conversion/cast.hpp>
  24. #include <boost/type_traits/is_fundamental.hpp>
  25. #include <boost/type_traits/is_integral.hpp>
  26. #include <boost/geometry/core/cs.hpp>
  27. #include <boost/geometry/util/select_most_precise.hpp>
  28. namespace boost { namespace geometry
  29. {
  30. namespace math
  31. {
  32. #ifndef DOXYGEN_NO_DETAIL
  33. namespace detail
  34. {
  35. template <typename T>
  36. inline T const& greatest(T const& v1, T const& v2)
  37. {
  38. return (std::max)(v1, v2);
  39. }
  40. template <typename T>
  41. inline T const& greatest(T const& v1, T const& v2, T const& v3)
  42. {
  43. return (std::max)(greatest(v1, v2), v3);
  44. }
  45. template <typename T>
  46. inline T const& greatest(T const& v1, T const& v2, T const& v3, T const& v4)
  47. {
  48. return (std::max)(greatest(v1, v2, v3), v4);
  49. }
  50. template <typename T>
  51. inline T const& greatest(T const& v1, T const& v2, T const& v3, T const& v4, T const& v5)
  52. {
  53. return (std::max)(greatest(v1, v2, v3, v4), v5);
  54. }
  55. template <typename T>
  56. inline T bounded(T const& v, T const& lower, T const& upper)
  57. {
  58. return (std::min)((std::max)(v, lower), upper);
  59. }
  60. template <typename T>
  61. inline T bounded(T const& v, T const& lower)
  62. {
  63. return (std::max)(v, lower);
  64. }
  65. template <typename T,
  66. bool IsFloatingPoint = boost::is_floating_point<T>::value>
  67. struct abs
  68. {
  69. static inline T apply(T const& value)
  70. {
  71. T const zero = T();
  72. return value < zero ? -value : value;
  73. }
  74. };
  75. template <typename T>
  76. struct abs<T, true>
  77. {
  78. static inline T apply(T const& value)
  79. {
  80. using ::fabs;
  81. using std::fabs; // for long double
  82. return fabs(value);
  83. }
  84. };
  85. struct equals_default_policy
  86. {
  87. template <typename T>
  88. static inline T apply(T const& a, T const& b)
  89. {
  90. // See http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.17
  91. return greatest(abs<T>::apply(a), abs<T>::apply(b), T(1));
  92. }
  93. };
  94. template <typename T,
  95. bool IsFloatingPoint = boost::is_floating_point<T>::value>
  96. struct equals_factor_policy
  97. {
  98. equals_factor_policy()
  99. : factor(1) {}
  100. explicit equals_factor_policy(T const& v)
  101. : factor(greatest(abs<T>::apply(v), T(1)))
  102. {}
  103. equals_factor_policy(T const& v0, T const& v1, T const& v2, T const& v3)
  104. : factor(greatest(abs<T>::apply(v0), abs<T>::apply(v1),
  105. abs<T>::apply(v2), abs<T>::apply(v3),
  106. T(1)))
  107. {}
  108. T const& apply(T const&, T const&) const
  109. {
  110. return factor;
  111. }
  112. T factor;
  113. };
  114. template <typename T>
  115. struct equals_factor_policy<T, false>
  116. {
  117. equals_factor_policy() {}
  118. explicit equals_factor_policy(T const&) {}
  119. equals_factor_policy(T const& , T const& , T const& , T const& ) {}
  120. static inline T apply(T const&, T const&)
  121. {
  122. return T(1);
  123. }
  124. };
  125. template <typename Type,
  126. bool IsFloatingPoint = boost::is_floating_point<Type>::value>
  127. struct equals
  128. {
  129. template <typename Policy>
  130. static inline bool apply(Type const& a, Type const& b, Policy const&)
  131. {
  132. return a == b;
  133. }
  134. };
  135. template <typename Type>
  136. struct equals<Type, true>
  137. {
  138. template <typename Policy>
  139. static inline bool apply(Type const& a, Type const& b, Policy const& policy)
  140. {
  141. boost::ignore_unused(policy);
  142. if (a == b)
  143. {
  144. return true;
  145. }
  146. if (boost::math::isfinite(a) && boost::math::isfinite(b))
  147. {
  148. // If a is INF and b is e.g. 0, the expression below returns true
  149. // but the values are obviously not equal, hence the condition
  150. return abs<Type>::apply(a - b)
  151. <= std::numeric_limits<Type>::epsilon() * policy.apply(a, b);
  152. }
  153. else
  154. {
  155. return a == b;
  156. }
  157. }
  158. };
  159. template <typename T1, typename T2, typename Policy>
  160. inline bool equals_by_policy(T1 const& a, T2 const& b, Policy const& policy)
  161. {
  162. return detail::equals
  163. <
  164. typename select_most_precise<T1, T2>::type
  165. >::apply(a, b, policy);
  166. }
  167. template <typename Type,
  168. bool IsFloatingPoint = boost::is_floating_point<Type>::value>
  169. struct smaller
  170. {
  171. static inline bool apply(Type const& a, Type const& b)
  172. {
  173. return a < b;
  174. }
  175. };
  176. template <typename Type>
  177. struct smaller<Type, true>
  178. {
  179. static inline bool apply(Type const& a, Type const& b)
  180. {
  181. if (!(a < b)) // a >= b
  182. {
  183. return false;
  184. }
  185. return ! equals<Type, true>::apply(b, a, equals_default_policy());
  186. }
  187. };
  188. template <typename Type,
  189. bool IsFloatingPoint = boost::is_floating_point<Type>::value>
  190. struct smaller_or_equals
  191. {
  192. static inline bool apply(Type const& a, Type const& b)
  193. {
  194. return a <= b;
  195. }
  196. };
  197. template <typename Type>
  198. struct smaller_or_equals<Type, true>
  199. {
  200. static inline bool apply(Type const& a, Type const& b)
  201. {
  202. if (a <= b)
  203. {
  204. return true;
  205. }
  206. return equals<Type, true>::apply(a, b, equals_default_policy());
  207. }
  208. };
  209. template <typename Type,
  210. bool IsFloatingPoint = boost::is_floating_point<Type>::value>
  211. struct equals_with_epsilon
  212. : public equals<Type, IsFloatingPoint>
  213. {};
  214. template
  215. <
  216. typename T,
  217. bool IsFundemantal = boost::is_fundamental<T>::value /* false */
  218. >
  219. struct square_root
  220. {
  221. typedef T return_type;
  222. static inline T apply(T const& value)
  223. {
  224. // for non-fundamental number types assume that sqrt is
  225. // defined either:
  226. // 1) at T's scope, or
  227. // 2) at global scope, or
  228. // 3) in namespace std
  229. using ::sqrt;
  230. using std::sqrt;
  231. return sqrt(value);
  232. }
  233. };
  234. template <typename FundamentalFP>
  235. struct square_root_for_fundamental_fp
  236. {
  237. typedef FundamentalFP return_type;
  238. static inline FundamentalFP apply(FundamentalFP const& value)
  239. {
  240. #ifdef BOOST_GEOMETRY_SQRT_CHECK_FINITENESS
  241. // This is a workaround for some 32-bit platforms.
  242. // For some of those platforms it has been reported that
  243. // std::sqrt(nan) and/or std::sqrt(-nan) returns a finite value.
  244. // For those platforms we need to define the macro
  245. // BOOST_GEOMETRY_SQRT_CHECK_FINITENESS so that the argument
  246. // to std::sqrt is checked appropriately before passed to std::sqrt
  247. if (boost::math::isfinite(value))
  248. {
  249. return std::sqrt(value);
  250. }
  251. else if (boost::math::isinf(value) && value < 0)
  252. {
  253. return -std::numeric_limits<FundamentalFP>::quiet_NaN();
  254. }
  255. return value;
  256. #else
  257. // for fundamental floating point numbers use std::sqrt
  258. return std::sqrt(value);
  259. #endif // BOOST_GEOMETRY_SQRT_CHECK_FINITENESS
  260. }
  261. };
  262. template <>
  263. struct square_root<float, true>
  264. : square_root_for_fundamental_fp<float>
  265. {
  266. };
  267. template <>
  268. struct square_root<double, true>
  269. : square_root_for_fundamental_fp<double>
  270. {
  271. };
  272. template <>
  273. struct square_root<long double, true>
  274. : square_root_for_fundamental_fp<long double>
  275. {
  276. };
  277. template <typename T>
  278. struct square_root<T, true>
  279. {
  280. typedef double return_type;
  281. static inline double apply(T const& value)
  282. {
  283. // for all other fundamental number types use also std::sqrt
  284. //
  285. // Note: in C++98 the only other possibility is double;
  286. // in C++11 there are also overloads for integral types;
  287. // this specialization works for those as well.
  288. return square_root_for_fundamental_fp
  289. <
  290. double
  291. >::apply(boost::numeric_cast<double>(value));
  292. }
  293. };
  294. template
  295. <
  296. typename T,
  297. bool IsFundemantal = boost::is_fundamental<T>::value /* false */
  298. >
  299. struct modulo
  300. {
  301. typedef T return_type;
  302. static inline T apply(T const& value1, T const& value2)
  303. {
  304. // for non-fundamental number types assume that a free
  305. // function mod() is defined either:
  306. // 1) at T's scope, or
  307. // 2) at global scope
  308. return mod(value1, value2);
  309. }
  310. };
  311. template
  312. <
  313. typename Fundamental,
  314. bool IsIntegral = boost::is_integral<Fundamental>::value
  315. >
  316. struct modulo_for_fundamental
  317. {
  318. typedef Fundamental return_type;
  319. static inline Fundamental apply(Fundamental const& value1,
  320. Fundamental const& value2)
  321. {
  322. return value1 % value2;
  323. }
  324. };
  325. // specialization for floating-point numbers
  326. template <typename Fundamental>
  327. struct modulo_for_fundamental<Fundamental, false>
  328. {
  329. typedef Fundamental return_type;
  330. static inline Fundamental apply(Fundamental const& value1,
  331. Fundamental const& value2)
  332. {
  333. return std::fmod(value1, value2);
  334. }
  335. };
  336. // specialization for fundamental number type
  337. template <typename Fundamental>
  338. struct modulo<Fundamental, true>
  339. : modulo_for_fundamental<Fundamental>
  340. {};
  341. /*!
  342. \brief Short constructs to enable partial specialization for PI, 2*PI
  343. and PI/2, currently not possible in Math.
  344. */
  345. template <typename T>
  346. struct define_pi
  347. {
  348. static inline T apply()
  349. {
  350. // Default calls Boost.Math
  351. return boost::math::constants::pi<T>();
  352. }
  353. };
  354. template <typename T>
  355. struct define_two_pi
  356. {
  357. static inline T apply()
  358. {
  359. // Default calls Boost.Math
  360. return boost::math::constants::two_pi<T>();
  361. }
  362. };
  363. template <typename T>
  364. struct define_half_pi
  365. {
  366. static inline T apply()
  367. {
  368. // Default calls Boost.Math
  369. return boost::math::constants::half_pi<T>();
  370. }
  371. };
  372. template <typename T>
  373. struct relaxed_epsilon
  374. {
  375. static inline T apply(const T& factor)
  376. {
  377. return factor * std::numeric_limits<T>::epsilon();
  378. }
  379. };
  380. // This must be consistent with math::equals.
  381. // By default math::equals() scales the error by epsilon using the greater of
  382. // compared values but here is only one value, though it should work the same way.
  383. // (a-a) <= max(a, a) * EPS -> 0 <= a*EPS
  384. // (a+da-a) <= max(a+da, a) * EPS -> da <= (a+da)*EPS
  385. template <typename T, bool IsIntegral = boost::is_integral<T>::value>
  386. struct scaled_epsilon
  387. {
  388. static inline T apply(T const& val)
  389. {
  390. return (std::max)(abs<T>::apply(val), T(1))
  391. * std::numeric_limits<T>::epsilon();
  392. }
  393. static inline T apply(T const& val, T const& eps)
  394. {
  395. return (std::max)(abs<T>::apply(val), T(1))
  396. * eps;
  397. }
  398. };
  399. template <typename T>
  400. struct scaled_epsilon<T, true>
  401. {
  402. static inline T apply(T const&)
  403. {
  404. return T(0);
  405. }
  406. static inline T apply(T const&, T const&)
  407. {
  408. return T(0);
  409. }
  410. };
  411. // ItoF ItoI FtoF
  412. template <typename Result, typename Source,
  413. bool ResultIsInteger = std::numeric_limits<Result>::is_integer,
  414. bool SourceIsInteger = std::numeric_limits<Source>::is_integer>
  415. struct rounding_cast
  416. {
  417. static inline Result apply(Source const& v)
  418. {
  419. return boost::numeric_cast<Result>(v);
  420. }
  421. };
  422. // TtoT
  423. template <typename Source, bool ResultIsInteger, bool SourceIsInteger>
  424. struct rounding_cast<Source, Source, ResultIsInteger, SourceIsInteger>
  425. {
  426. static inline Source apply(Source const& v)
  427. {
  428. return v;
  429. }
  430. };
  431. // FtoI
  432. template <typename Result, typename Source>
  433. struct rounding_cast<Result, Source, true, false>
  434. {
  435. static inline Result apply(Source const& v)
  436. {
  437. return boost::numeric_cast<Result>(v < Source(0) ?
  438. v - Source(0.5) :
  439. v + Source(0.5));
  440. }
  441. };
  442. } // namespace detail
  443. #endif
  444. template <typename T>
  445. inline T pi() { return detail::define_pi<T>::apply(); }
  446. template <typename T>
  447. inline T two_pi() { return detail::define_two_pi<T>::apply(); }
  448. template <typename T>
  449. inline T half_pi() { return detail::define_half_pi<T>::apply(); }
  450. template <typename T>
  451. inline T relaxed_epsilon(T const& factor)
  452. {
  453. return detail::relaxed_epsilon<T>::apply(factor);
  454. }
  455. template <typename T>
  456. inline T scaled_epsilon(T const& value)
  457. {
  458. return detail::scaled_epsilon<T>::apply(value);
  459. }
  460. template <typename T>
  461. inline T scaled_epsilon(T const& value, T const& eps)
  462. {
  463. return detail::scaled_epsilon<T>::apply(value, eps);
  464. }
  465. // Maybe replace this by boost equals or so
  466. /*!
  467. \brief returns true if both arguments are equal.
  468. \ingroup utility
  469. \param a first argument
  470. \param b second argument
  471. \return true if a == b
  472. \note If both a and b are of an integral type, comparison is done by ==.
  473. If one of the types is floating point, comparison is done by abs and
  474. comparing with epsilon. If one of the types is non-fundamental, it might
  475. be a high-precision number and comparison is done using the == operator
  476. of that class.
  477. */
  478. template <typename T1, typename T2>
  479. inline bool equals(T1 const& a, T2 const& b)
  480. {
  481. return detail::equals
  482. <
  483. typename select_most_precise<T1, T2>::type
  484. >::apply(a, b, detail::equals_default_policy());
  485. }
  486. template <typename T1, typename T2>
  487. inline bool equals_with_epsilon(T1 const& a, T2 const& b)
  488. {
  489. return detail::equals_with_epsilon
  490. <
  491. typename select_most_precise<T1, T2>::type
  492. >::apply(a, b, detail::equals_default_policy());
  493. }
  494. template <typename T1, typename T2>
  495. inline bool smaller(T1 const& a, T2 const& b)
  496. {
  497. return detail::smaller
  498. <
  499. typename select_most_precise<T1, T2>::type
  500. >::apply(a, b);
  501. }
  502. template <typename T1, typename T2>
  503. inline bool larger(T1 const& a, T2 const& b)
  504. {
  505. return detail::smaller
  506. <
  507. typename select_most_precise<T1, T2>::type
  508. >::apply(b, a);
  509. }
  510. template <typename T1, typename T2>
  511. inline bool smaller_or_equals(T1 const& a, T2 const& b)
  512. {
  513. return detail::smaller_or_equals
  514. <
  515. typename select_most_precise<T1, T2>::type
  516. >::apply(a, b);
  517. }
  518. template <typename T1, typename T2>
  519. inline bool larger_or_equals(T1 const& a, T2 const& b)
  520. {
  521. return detail::smaller_or_equals
  522. <
  523. typename select_most_precise<T1, T2>::type
  524. >::apply(b, a);
  525. }
  526. template <typename T>
  527. inline T d2r()
  528. {
  529. static T const conversion_coefficient = geometry::math::pi<T>() / T(180.0);
  530. return conversion_coefficient;
  531. }
  532. template <typename T>
  533. inline T r2d()
  534. {
  535. static T const conversion_coefficient = T(180.0) / geometry::math::pi<T>();
  536. return conversion_coefficient;
  537. }
  538. #ifndef DOXYGEN_NO_DETAIL
  539. namespace detail {
  540. template <typename DegreeOrRadian>
  541. struct as_radian
  542. {
  543. template <typename T>
  544. static inline T apply(T const& value)
  545. {
  546. return value;
  547. }
  548. };
  549. template <>
  550. struct as_radian<degree>
  551. {
  552. template <typename T>
  553. static inline T apply(T const& value)
  554. {
  555. return value * d2r<T>();
  556. }
  557. };
  558. template <typename DegreeOrRadian>
  559. struct from_radian
  560. {
  561. template <typename T>
  562. static inline T apply(T const& value)
  563. {
  564. return value;
  565. }
  566. };
  567. template <>
  568. struct from_radian<degree>
  569. {
  570. template <typename T>
  571. static inline T apply(T const& value)
  572. {
  573. return value * r2d<T>();
  574. }
  575. };
  576. } // namespace detail
  577. #endif
  578. template <typename DegreeOrRadian, typename T>
  579. inline T as_radian(T const& value)
  580. {
  581. return detail::as_radian<DegreeOrRadian>::apply(value);
  582. }
  583. template <typename DegreeOrRadian, typename T>
  584. inline T from_radian(T const& value)
  585. {
  586. return detail::from_radian<DegreeOrRadian>::apply(value);
  587. }
  588. /*!
  589. \brief Calculates the haversine of an angle
  590. \ingroup utility
  591. \note See http://en.wikipedia.org/wiki/Haversine_formula
  592. haversin(alpha) = sin2(alpha/2)
  593. */
  594. template <typename T>
  595. inline T hav(T const& theta)
  596. {
  597. T const half = T(0.5);
  598. T const sn = sin(half * theta);
  599. return sn * sn;
  600. }
  601. /*!
  602. \brief Short utility to return the square
  603. \ingroup utility
  604. \param value Value to calculate the square from
  605. \return The squared value
  606. */
  607. template <typename T>
  608. inline T sqr(T const& value)
  609. {
  610. return value * value;
  611. }
  612. /*!
  613. \brief Short utility to return the square root
  614. \ingroup utility
  615. \param value Value to calculate the square root from
  616. \return The square root value
  617. */
  618. template <typename T>
  619. inline typename detail::square_root<T>::return_type
  620. sqrt(T const& value)
  621. {
  622. return detail::square_root
  623. <
  624. T, boost::is_fundamental<T>::value
  625. >::apply(value);
  626. }
  627. /*!
  628. \brief Short utility to return the modulo of two values
  629. \ingroup utility
  630. \param value1 First value
  631. \param value2 Second value
  632. \return The result of the modulo operation on the (ordered) pair
  633. (value1, value2)
  634. */
  635. template <typename T>
  636. inline typename detail::modulo<T>::return_type
  637. mod(T const& value1, T const& value2)
  638. {
  639. return detail::modulo
  640. <
  641. T, boost::is_fundamental<T>::value
  642. >::apply(value1, value2);
  643. }
  644. /*!
  645. \brief Short utility to workaround gcc/clang problem that abs is converting to integer
  646. and that older versions of MSVC does not support abs of long long...
  647. \ingroup utility
  648. */
  649. template<typename T>
  650. inline T abs(T const& value)
  651. {
  652. return detail::abs<T>::apply(value);
  653. }
  654. /*!
  655. \brief Short utility to calculate the sign of a number: -1 (negative), 0 (zero), 1 (positive)
  656. \ingroup utility
  657. */
  658. template <typename T>
  659. inline int sign(T const& value)
  660. {
  661. T const zero = T();
  662. return value > zero ? 1 : value < zero ? -1 : 0;
  663. }
  664. /*!
  665. \brief Short utility to cast a value possibly rounding it to the nearest
  666. integral value.
  667. \ingroup utility
  668. \note If the source T is NOT an integral type and Result is an integral type
  669. the value is rounded towards the closest integral value. Otherwise it's
  670. casted without rounding.
  671. */
  672. template <typename Result, typename T>
  673. inline Result rounding_cast(T const& v)
  674. {
  675. return detail::rounding_cast<Result, T>::apply(v);
  676. }
  677. /*!
  678. \brief Evaluate the sine and cosine function with the argument in degrees
  679. \note The results obey exactly the elementary properties of the trigonometric
  680. functions, e.g., sin 9&deg; = cos 81&deg; = &minus; sin 123456789&deg;.
  681. If x = &minus;0, then \e sinx = &minus;0; this is the only case where
  682. &minus;0 is returned.
  683. */
  684. template<typename T>
  685. inline void sin_cos_degrees(T const& x,
  686. T & sinx,
  687. T & cosx)
  688. {
  689. // In order to minimize round-off errors, this function exactly reduces
  690. // the argument to the range [-45, 45] before converting it to radians.
  691. T remainder; int quotient;
  692. remainder = math::mod(x, T(360));
  693. quotient = floor(remainder / 90 + T(0.5));
  694. remainder -= 90 * quotient;
  695. // Convert to radians.
  696. remainder *= d2r<T>();
  697. T s = sin(remainder), c = cos(remainder);
  698. switch (unsigned(quotient) & 3U)
  699. {
  700. case 0U: sinx = s; cosx = c; break;
  701. case 1U: sinx = c; cosx = -s; break;
  702. case 2U: sinx = -s; cosx = -c; break;
  703. default: sinx = -c; cosx = s; break; // case 3U
  704. }
  705. // Set sign of 0 results. -0 only produced for sin(-0).
  706. if (x != 0)
  707. {
  708. sinx += T(0); cosx += T(0);
  709. }
  710. }
  711. /*!
  712. \brief Round off a given angle
  713. */
  714. template<typename T>
  715. inline T round_angle(T const& x) {
  716. static const T z = 1/T(16);
  717. if (x == 0)
  718. {
  719. return 0;
  720. }
  721. T y = math::abs(x);
  722. // z - (z - y) must not be simplified to y.
  723. y = y < z ? z - (z - y) : y;
  724. return x < 0 ? -y : y;
  725. }
  726. /*!
  727. \brief The error-free sum of two numbers.
  728. */
  729. template<typename T>
  730. inline T sum_error(T const& u, T const& v, T& t)
  731. {
  732. volatile T s = u + v;
  733. volatile T up = s - v;
  734. volatile T vpp = s - up;
  735. up -= u;
  736. vpp -= v;
  737. t = -(up + vpp);
  738. return s;
  739. }
  740. /*!
  741. \brief Evaluate the polynomial in x using Horner's method.
  742. */
  743. // TODO: adl1995 - Merge these functions with formulas/area_formulas.hpp
  744. // i.e. place them in one file.
  745. template <typename NT, typename IteratorType>
  746. inline NT horner_evaluate(NT const& x,
  747. IteratorType begin,
  748. IteratorType end)
  749. {
  750. NT result(0);
  751. IteratorType it = end;
  752. do
  753. {
  754. result = result * x + *--it;
  755. }
  756. while (it != begin);
  757. return result;
  758. }
  759. /*!
  760. \brief Evaluate the polynomial.
  761. */
  762. template<typename IteratorType, typename CT>
  763. inline CT polyval(IteratorType first,
  764. IteratorType last,
  765. CT const& eps)
  766. {
  767. int N = std::distance(first, last) - 1;
  768. int index = 0;
  769. CT y = N < 0 ? 0 : *(first + (index++));
  770. while (--N >= 0)
  771. {
  772. y = y * eps + *(first + (index++));
  773. }
  774. return y;
  775. }
  776. /*
  777. \brief Short utility to calculate the power
  778. \ingroup utility
  779. */
  780. template <typename T1, typename T2>
  781. inline T1 pow(T1 const& a, T2 const& b)
  782. {
  783. using std::pow;
  784. return pow(a, b);
  785. }
  786. } // namespace math
  787. }} // namespace boost::geometry
  788. #endif // BOOST_GEOMETRY_UTIL_MATH_HPP