math.hpp 23 KB

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