BLOG | DOCUMENTATION | GITHUB

Home --> Documentations --> PJLIB Reference

math.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifndef __PJ_MATH_H__
21#define __PJ_MATH_H__
22
28#include <pj/string.h>
29#include <pj/compat/high_precision.h>
30
32
48#define PJ_PI 3.14159265358979323846 /* pi */
50#define PJ_1_PI 0.318309886183790671538 /* 1/pi */
51
56#define PJ_ABS(x) ((x) > 0 ? (x) : -(x))
57
59#define PJ_MAX(x, y) ((x) > (y)? (x) : (y))
60
62#define PJ_MIN(x, y) ((x) < (y)? (x) : (y))
63
67typedef struct pj_math_stat
68{
69 int n;
70 int max;
71 int min;
72 int last;
73 int mean;
75 /* Private members */
76#if PJ_HAS_FLOATING_POINT
77 float fmean_;
78#else
80#endif
81 pj_highprec_t m2_;
83
91PJ_INLINE(unsigned) pj_isqrt(unsigned i)
92{
93 unsigned res = 1, prev;
94
95 /* Rough guess, calculate half bit of input */
96 prev = i >> 2;
97 while (prev) {
98 prev >>= 2;
99 res <<= 1;
100 }
101
102 /* Babilonian method */
103 do {
104 prev = res;
105 res = (prev + i/prev) >> 1;
106 } while ((prev+res)>>1 != res);
107
108 return res;
109}
110
117{
118 pj_bzero(stat, sizeof(pj_math_stat));
119}
120
128{
129#if PJ_HAS_FLOATING_POINT
130 float delta;
131#else
132 int delta;
133#endif
134
135 stat->last = val;
136
137 if (stat->n++) {
138 if (stat->min > val)
139 stat->min = val;
140 if (stat->max < val)
141 stat->max = val;
142 } else {
143 stat->min = stat->max = val;
144 }
145
146#if PJ_HAS_FLOATING_POINT
147 delta = val - stat->fmean_;
148 stat->fmean_ += delta/stat->n;
149
150 /* Return mean value with 'rounding' */
151 stat->mean = (int) (stat->fmean_ + 0.5);
152
153 stat->m2_ += (int)(delta * (val-stat->fmean_));
154#else
155 delta = val - stat->mean;
156 stat->mean += delta/stat->n;
157 stat->mean_res_ += delta % stat->n;
158 if (stat->mean_res_ >= stat->n) {
159 ++stat->mean;
160 stat->mean_res_ -= stat->n;
161 } else if (stat->mean_res_ <= -stat->n) {
162 --stat->mean;
163 stat->mean_res_ += stat->n;
164 }
165
166 stat->m2_ += delta * (val-stat->mean);
167#endif
168}
169
178{
179 if (stat->n == 0) return 0;
180 return (pj_isqrt((unsigned)(stat->m2_/stat->n)));
181}
182
193{
194 if (stat->n == 0)
195 stat->n = 1;
196 stat->m2_ = dev*dev*stat->n;
197}
198
202
203#endif /* __PJ_MATH_H__ */
void pj_bzero(void *dst, pj_size_t size)
Definition: string.h:762
#define PJ_BEGIN_DECL
Definition: config.h:1284
#define PJ_INLINE(type)
Definition: config.h:1178
#define PJ_END_DECL
Definition: config.h:1285
void pj_math_stat_update(pj_math_stat *stat, int val)
Definition: math.h:127
void pj_math_stat_set_stddev(pj_math_stat *stat, unsigned dev)
Definition: math.h:192
void pj_math_stat_init(pj_math_stat *stat)
Definition: math.h:116
unsigned pj_isqrt(unsigned i)
Definition: math.h:91
unsigned pj_math_stat_get_stddev(const pj_math_stat *stat)
Definition: math.h:177
PJLIB String Operations.
Definition: math.h:68
int min
Definition: math.h:71
pj_highprec_t m2_
Definition: math.h:81
int mean
Definition: math.h:73
int max
Definition: math.h:70
int mean_res_
Definition: math.h:79
int last
Definition: math.h:72
int n
Definition: math.h:69

 


PJLIB Open Source, high performance, small footprint, and very very portable framework
Copyright (C) 2006-2009 Teluu Inc.