source: tags/arb_5.5/ARBDB/admath.c

Last change on this file was 5725, checked in by westram, 16 years ago
  • removed useless macros:
    • GB_STRDUP (easily taken for GB_strdup)
    • GB_MEMCPY + GB_MEMSET + GB_FREE
    • GB_DELETE (replaced by freeset(xx,NULL))
  • added macros:
    • freeset (= free + assign)
    • freedup (= free + assign strdup'ed)
    • reassign (= free + assign + clear source var)
    • nulldup (=strdup accepting NULL; replacement for GB_strdup in C++ code)
  • use these macros where applicable
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.3 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3/* #include <malloc.h> */
4#include <string.h>
5#include <math.h>
6#include <time.h>
7
8/*#include <arbdb.h>*/
9#include "adlocal.h"
10
11double GB_log_fak(int n){
12    /* returns log(n!) */
13    static int     max_n = 0;
14    static double *res   = 0;
15   
16    if (n<=1) return 0.0;       /* log 1 = 0 */
17
18    if (n >= max_n){
19        double sum = 0;
20        int i;
21        freeset(res, NULL);
22        max_n = n + 100;
23        res = (double *)GB_calloc(sizeof(double),max_n);
24        for (i=1;i<max_n;i++){
25            sum += log((double)i);
26            res[i] = sum;
27        }
28    }
29    return res[n];
30}
31
32/* ---------------------------------- */
33/*      random number generation      */
34/* ---------------------------------- */
35
36static int randomSeeded = 0;
37
38double GB_frandom() {
39    /* produces a random number in range [0.0 .. 1.0] */
40    if (!randomSeeded) {
41        srand(time(0));
42        randomSeeded = 1;;
43    }
44    return ((double)rand())/RAND_MAX;
45}
46
47int GB_random(int range) {
48    /* produces a random number in range [0 .. range-1] */
49    if (!randomSeeded) {
50        srand(time(0));
51        randomSeeded = 1;
52    }
53
54#if defined(DEBUG)
55    if (range>RAND_MAX) {
56        printf("Warning: range to big for random granularity (%i > %i)\n", range, RAND_MAX);
57    }
58#endif /* DEBUG */
59
60    return (int)(rand()*((double)range) / (RAND_MAX+1.0));
61}
Note: See TracBrowser for help on using the repository browser.