[glib] GRand: avoid overflow
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] GRand: avoid overflow
- Date: Sat, 4 Jun 2011 03:39:09 +0000 (UTC)
commit 77be95cdce7f875a10576ac72624024b04b86a65
Author: Matthias Clasen <mclasen redhat com>
Date: Fri Jun 3 23:37:23 2011 -0400
GRand: avoid overflow
Otherwise, g_random_double_range (-G_MAXDOUBLE, G_MAXDOUBLE)
doesn't work as expected.
https://bugzilla.gnome.org/show_bug.cgi?id=502560
glib/grand.c | 6 +++++-
1 files changed, 5 insertions(+), 1 deletions(-)
---
diff --git a/glib/grand.c b/glib/grand.c
index b97617b..bb1bffe 100644
--- a/glib/grand.c
+++ b/glib/grand.c
@@ -586,7 +586,11 @@ g_rand_double (GRand* rand)
gdouble
g_rand_double_range (GRand* rand, gdouble begin, gdouble end)
{
- return g_rand_double (rand) * (end - begin) + begin;
+ gdouble r;
+
+ r = g_rand_double (rand);
+
+ return r * end - (r - 1) * begin;
}
/**
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]