This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] libstdc++: add uniform on sphere distribution
- From: Marc Glisse <marc dot glisse at inria dot fr>
- To: Ulrich Drepper <drepper at gmail dot com>
- Cc: libstdc++ at gcc dot gnu dot org, Jonathan Wakely <jwakely at redhat dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Sat, 9 Aug 2014 19:40:32 +0200 (CEST)
- Subject: Re: [PATCH] libstdc++: add uniform on sphere distribution
- Authentication-results: sourceware.org; auth=none
- References: <87a98eow4m dot fsf at x240 dot local dot i-did-not-set--mail-host-address--so-tickle-me> <alpine dot DEB dot 2 dot 11 dot 1407231149300 dot 1768 at laptop-mg dot saclay dot inria dot fr> <20140723102908 dot GM2361 at redhat dot com> <87d2catvi3 dot fsf at x240 dot local dot i-did-not-set--mail-host-address--so-tickle-me> <alpine dot DEB dot 2 dot 11 dot 1408090904200 dot 10998 at stedding dot saclay dot inria dot fr> <CAOPLpQePn5FO_5PsazROOi3PfmRXx6y7mwoYo5h2+h3TZnX-gQ at mail dot gmail dot com> <alpine dot DEB dot 2 dot 11 dot 1408091413520 dot 10998 at stedding dot saclay dot inria dot fr> <CAOPLpQc7EZ0JwsqOtu3==EOVskKchYswZx7iyhcMgER6oz_v3w at mail dot gmail dot com> <alpine dot DEB dot 2 dot 11 dot 1408091727040 dot 10998 at stedding dot saclay dot inria dot fr> <878umxtxei dot fsf at x240 dot local dot i-did-not-set--mail-host-address--so-tickle-me>
- Reply-to: libstdc++ at gcc dot gnu dot org
On Sat, 9 Aug 2014, Ulrich Drepper wrote:
How about the patch below?
Looks good, with two details:
+ template<typename _RealType>
+ class uniform_on_sphere_helper<2, _RealType>
+ {
+ typedef typename uniform_on_sphere_distribution<2, _RealType>::
+ result_type result_type;
+
+ public:
+ template<typename _NormalDistribution,
+ typename _UniformRandomNumberGenerator>
+ result_type operator()(_NormalDistribution&,
+ _UniformRandomNumberGenerator& __urng)
+ {
+ result_type __ret;
+ _RealType __sq;
+ std::__detail::_Adaptor<_UniformRandomNumberGenerator,
+ _RealType> __aurng(__urng);
+
+ do
+ {
+ __ret[0] = __aurng();
I must be missing something obvious, but normal_distribution uses:
__x = result_type(2.0) * __aurng() - 1.0;
to get a number between -1 and 1, and I don't see where you do this
rescaling. Does __aurng() already return a number in the right interval in
this context? If so we may want to update our naming of variables to make
that clearer.
+ __ret[1] = __aurng();
+
+ __sq = __ret[0] * __ret[0] + __ret[1] * __ret[1];
+ }
+ while (__sq == _RealType(0) || __sq > _RealType(1));
+
+ // Yes, we do not just use sqrt(__sq) because hypot() is more
+ // accurate.
+ auto __norm = std::hypot(__ret[0], __ret[1]);
Assuming the 2 coordinates are obtained through a rescaling x->2*x-1, if
__sq is not exactly 0, it must be between 2^-103 and 1 (for ieee
double), so I am not sure hypot gains that much (at least in my mind
hypot was mostly a gain close to 0 or infinity, but maybe it has more
advantages). It can only hurt speed though, so not a big issue.
--
Marc Glisse