aboutsummaryrefslogtreecommitdiffstats
path: root/random_float.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-01-29 07:32:29 +0100
committerMattias Andrée <maandree@kth.se>2024-01-29 17:38:32 +0100
commitcb4b8137902b7c89872f8face7b47704f932b807 (patch)
treeaff0ef17c94de69cb818b4635338b792cdae534a /random_float.c
parentAdd libsimple_generate_seed and libsimple_srand (diff)
downloadlibsimple-cb4b8137902b7c89872f8face7b47704f932b807.tar.gz
libsimple-cb4b8137902b7c89872f8face7b47704f932b807.tar.bz2
libsimple-cb4b8137902b7c89872f8face7b47704f932b807.tar.xz
Add to*, *diff, *abs, *uabs, and random_{bits,float,signed,unsigned}
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'random_float.c')
-rw-r--r--random_float.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/random_float.c b/random_float.c
new file mode 100644
index 0000000..b4b6557
--- /dev/null
+++ b/random_float.c
@@ -0,0 +1,58 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifndef TEST
+
+#include <math.h>
+
+
+long double
+libsimple_random_float(uintmax_t (*rng)(size_t bits, void *user), void *user, long double min, long double postmax) /* TODO add test */
+{
+ long double range, offset, mantissa, value;
+ uintmax_t r;
+ int exp;
+ size_t bits;
+
+ if (min < postmax) {
+ range = postmax - min;
+ offset = min;
+ } else if (min > postmax) {
+ range = min - postmax;
+ offset = postmax;
+ } else {
+ return min;
+ }
+
+ mantissa = frexpl(range, &exp);
+ do {
+ bits = sizeof(uintmax_t) * CHAR_BIT;
+ r = (*rng)(bits, user);
+ while (bits && (r & 1)) {
+ exp -= 1;
+ bits -= 1;
+ r >>= 1;
+ }
+ } while (!bits);
+
+ bits = sizeof(uintmax_t) * CHAR_BIT;
+ r = (*rng)(bits, user);
+ value = (long double)r;
+ value = fmal(value, 0.5, 0.5);
+ value = ldexpl(value, -(int)bits);
+
+ value = ldexpl(value, exp);
+ value *= mantissa;
+ return value + offset;
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void)
+{
+ return 0;
+}
+
+#endif