forked from snowyu/idb.c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisdk_math.h
69 lines (55 loc) · 1.58 KB
/
isdk_math.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* =====================================================================================
*
* Filename: isdk_math.h
*
* Description: the math funcs
*
* Version: 0.1
* Created: 2013/03/08 10时52分15秒
* Revision: none
* Compiler: gcc
*
* Author: Riceball LEE([email protected])
*
* =====================================================================================
*/
#ifndef __ISDK_MATH_H
#define __ISDK_MATH_H
#include <assert.h>
#include <sys/time.h>
#include <limits.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* Get the larger value of two integers. */
static inline long max(long a, long b){
return (a > b) ? a : b;
}
/* Get the lesser value of two integers. */
static inline long min(long a, long b){
return (a < b) ? a : b;
}
/* Get the time of day in seconds. */
static inline double now(void){
struct timeval tv;
if(gettimeofday(&tv, NULL) == -1) return 0.0;
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
}
/* Get a random number as long integer based on uniform distribution. */
unsigned long randLong(void);
/* Get a random number as double decimal based on uniform distribution. */
static inline double randDouble(void){
double val = randLong() / (double)ULONG_MAX;
return val < 1.0 ? val : 0.0;
}
/* Get a random number as double decimal based on normal distribution. */
static inline double randDoubleNd(double avg, double sd){
assert(sd >= 0.0);
return sqrt(-2.0 * log(randDouble())) * cos(2 * 3.141592653589793 * randDouble()) * sd + avg;
}
#ifdef __cplusplus
}
#endif
#endif