[glib/glib-2-28] GRand: avoid overflow
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/glib-2-28] GRand: avoid overflow
- Date: Sun, 5 Jun 2011 17:43:50 +0000 (UTC)
commit f75eafc246f1deaf6e06601c3bec34a4afc86f02
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]