diff mbox

libstdc++: add uniform on sphere distribution

Message ID 53EB4B26.8070205@oracle.com
State New
Headers show

Commit Message

Paolo Carlini Aug. 13, 2014, 11:25 a.m. UTC
... fixed with the below.

Thanks,
Paolo.

//////////////////////
2014-08-13  Paolo Carlini  <paolo.carlini@oracle.com>

	PR libstdc++/62118
	* include/ext/random.tcc (uniform_on_sphere_helper<2, _RealType>::
	operator()): Use std::hypot only when _GLIBCXX_USE_C99_MATH_TR1.

Comments

Marc Glisse Aug. 13, 2014, 11:32 a.m. UTC | #1
On Wed, 13 Aug 2014, Paolo Carlini wrote:

> ... fixed with the below.

Could you please use __sq instead of recomputing it?
Paolo Carlini Aug. 13, 2014, 11:40 a.m. UTC | #2
On 08/13/2014 01:32 PM, Marc Glisse wrote:
> On Wed, 13 Aug 2014, Paolo Carlini wrote:
>
>> ... fixed with the below.
>
> Could you please use __sq instead of recomputing it?
>
Patch preapproved, thanks!

Paolo.
diff mbox

Patch

Index: include/ext/random.tcc
===================================================================
--- include/ext/random.tcc	(revision 213898)
+++ include/ext/random.tcc	(working copy)
@@ -1547,10 +1547,12 @@ 
     template<std::size_t _Dimen, typename _RealType>
       class uniform_on_sphere_helper
       {
-	typedef typename uniform_on_sphere_distribution<_Dimen, _RealType>::result_type result_type;
+	typedef typename uniform_on_sphere_distribution<_Dimen, _RealType>::
+	  result_type result_type;
 
       public:
-	template<typename _NormalDistribution, typename _UniformRandomNumberGenerator>
+	template<typename _NormalDistribution,
+		 typename _UniformRandomNumberGenerator>
 	result_type operator()(_NormalDistribution& __nd,
 			       _UniformRandomNumberGenerator& __urng)
         {
@@ -1604,9 +1606,13 @@ 
 	    }
 	  while (__sq == _RealType(0) || __sq > _RealType(1));
 
+#if _GLIBCXX_USE_C99_MATH_TR1
 	  // Yes, we do not just use sqrt(__sq) because hypot() is more
 	  // accurate.
 	  auto __norm = std::hypot(__ret[0], __ret[1]);
+#else
+	  auto __norm = std::sqrt(__ret[0] * __ret[0] + __ret[1] * __ret[1]);
+#endif
 	  __ret[0] /= __norm;
 	  __ret[1] /= __norm;