math.h Source File

AVR Libc Home Page |  | AVR Libc Development Pages
Main Page | User Manual | Library Reference | FAQ | Alphabetical Index | Example Projects

math.h
Go to the documentation of this file.
1 /* Copyright (c) 2002,2007-2009 Michael Stumpf
2
3 Portions of documentation Copyright (c) 1990 - 1994
4 The Regents of the University of California.
5
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13
14 * Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in
16 the documentation and/or other materials provided with the
17 distribution.
18
19 * Neither the name of the copyright holders nor the names of
20 contributors may be used to endorse or promote products derived
21 from this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 POSSIBILITY OF SUCH DAMAGE. */
34
35 /* $Id: math.h 2264 2011-12-25 01:16:55Z dmix $ */
36
37 /*
38 math.h - mathematical functions
39
40 Author : Michael Stumpf
41 Michael.Stumpf@t-online.de
42
43 __ATTR_CONST__ added by marekm@linux.org.pl for functions
44 that "do not examine any values except their arguments, and have
45 no effects except the return value", for better optimization by gcc.
46 */
47
48 #ifndef __MATH_H
49 #define __MATH_H
50
51 /** \file */
52 /** \defgroup avr_math <math.h>: Mathematics
53 \code #include <math.h> \endcode
54
55 This header file declares basic mathematics constants and
56 functions.
57
58 \par Notes:
59 - In order to access the functions declared herein, it is usually
60 also required to additionally link against the library \c libm.a.
61 See also the related \ref faq_libm "FAQ entry".
62 - Math functions do not raise exceptions and do not change the
63 \c errno variable. Therefore the majority of them are declared
64 with const attribute, for better optimization by GCC. */
65
66
67 /** \ingroup avr_math */
68 /*@{*/
69
70 /** The constant \a e. */
71 #define M_E 2.7182818284590452354
72
73 /** The logarithm of the \a e to base 2. */
74 #define M_LOG2E 1.4426950408889634074 /* log_2 e */
75
76 /** The logarithm of the \a e to base 10. */
77 #define M_LOG10E 0.43429448190325182765 /* log_10 e */
78
79 /** The natural logarithm of the 2. */
80 #define M_LN2 0.69314718055994530942 /* log_e 2 */
81
82 /** The natural logarithm of the 10. */
83 #define M_LN10 2.30258509299404568402 /* log_e 10 */
84
85 /** The constant \a pi. */
86 #define M_PI 3.14159265358979323846 /* pi */
87
88 /** The constant \a pi/2. */
89 #define M_PI_2 1.57079632679489661923 /* pi/2 */
90
91 /** The constant \a pi/4. */
92 #define M_PI_4 0.78539816339744830962 /* pi/4 */
93
94 /** The constant \a 1/pi. */
95 #define M_1_PI 0.31830988618379067154 /* 1/pi */
96
97 /** The constant \a 2/pi. */
98 #define M_2_PI 0.63661977236758134308 /* 2/pi */
99
100 /** The constant \a 2/sqrt(pi). */
101 #define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
102
103 /** The square root of 2. */
104 #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
105
106 /** The constant \a 1/sqrt(2). */
107 #define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
108
109 /** NAN constant. */
110 #define NAN __builtin_nan("")
111
112 /** INFINITY constant. */
113 #define INFINITY __builtin_inf()
114
115
116 #ifndef __ATTR_CONST__
117 # define __ATTR_CONST__ __attribute__((__const__))
118 #endif
119
120 #ifdef __cplusplus
121 extern "C" {
122 #endif
123
124 /**
125 The cos() function returns the cosine of \a __x, measured in radians.
126 */
127 extern double cos(double __x) __ATTR_CONST__;
128 #define cosf cos /**< The alias for cos(). */
129
130 /**
131 The sin() function returns the sine of \a __x, measured in radians.
132 */
133 extern double sin(double __x) __ATTR_CONST__;
134 #define sinf sin /**< The alias for sin(). */
135
136 /**
137 The tan() function returns the tangent of \a __x, measured in radians.
138 */
139 extern double tan(double __x) __ATTR_CONST__;
140 #define tanf tan /**< The alias for tan(). */
141
142 /**
143 The fabs() function computes the absolute value of a floating-point
144 number \a __x.
145 */
146 extern double fabs(double __x) __ATTR_CONST__;
147 #define fabsf fabs /**< The alias for fabs(). */
148
149 /**
150 The function fmod() returns the floating-point remainder of <em>__x /
151 __y</em>.
152 */
153 extern double fmod(double __x, double __y) __ATTR_CONST__;
154 #define fmodf fmod /**< The alias for fmod(). */
155
156 /**
157 The modf() function breaks the argument \a __x into integral and
158 fractional parts, each of which has the same sign as the argument.
159 It stores the integral part as a double in the object pointed to by
160 \a __iptr.
161
162 The modf() function returns the signed fractional part of \a __x.
163
164 \note This implementation skips writing by zero pointer. However,
165 the GCC 4.3 can replace this function with inline code that does not
166 permit to use NULL address for the avoiding of storing.
167 */
168 extern double modf(double __x, double *__iptr);
169
170 /** The alias for modf().
171 */
172 extern float modff (float __x, float *__iptr);
173
174 /**
175 The sqrt() function returns the non-negative square root of \a __x.
176 */
177 extern double sqrt(double __x) __ATTR_CONST__;
178 #define sqrtf sqrt /**< The alias for sqrt(). */
179
180 /**
181 The cbrt() function returns the cube root of \a __x.
182 */
183 extern double cbrt(double __x) __ATTR_CONST__;
184 #define cbrtf cbrt /**< The alias for cbrt(). */
185
186 /**
187 The hypot() function returns <em>sqrt(__x*__x + __y*__y)</em>. This
188 is the length of the hypotenuse of a right triangle with sides of
189 length \a __x and \a __y, or the distance of the point (\a __x, \a
190 __y) from the origin. Using this function instead of the direct
191 formula is wise, since the error is much smaller. No underflow with
192 small \a __x and \a __y. No overflow if result is in range.
193 */
194 extern double hypot (double __x, double __y) __ATTR_CONST__;
195 #define hypotf hypot /**< The alias for hypot(). */
196
197 /**
198 The function square() returns <em>__x * __x</em>.
199
200 \note This function does not belong to the C standard definition.
201 */
202 extern double square(double __x) __ATTR_CONST__;
203 #define squaref square /**< The alias for square(). */
204
205 /**
206 The floor() function returns the largest integral value less than or
207 equal to \a __x, expressed as a floating-point number.
208 */
209 extern double floor(double __x) __ATTR_CONST__;
210 #define floorf floor /**< The alias for floor(). */
211
212 /**
213 The ceil() function returns the smallest integral value greater than
214 or equal to \a __x, expressed as a floating-point number.
215 */
216 extern double ceil(double __x) __ATTR_CONST__;
217 #define ceilf ceil /**< The alias for ceil(). */
218
219 /**
220 The frexp() function breaks a floating-point number into a normalized
221 fraction and an integral power of 2. It stores the integer in the \c
222 int object pointed to by \a __pexp.
223
224 If \a __x is a normal float point number, the frexp() function
225 returns the value \c v, such that \c v has a magnitude in the
226 interval [1/2, 1) or zero, and \a __x equals \c v times 2 raised to
227 the power \a __pexp. If \a __x is zero, both parts of the result are
228 zero. If \a __x is not a finite number, the frexp() returns \a __x as
229 is and stores 0 by \a __pexp.
230
231 \note This implementation permits a zero pointer as a directive to
232 skip a storing the exponent.
233 */
234 extern double frexp(double __x, int *__pexp);
235 #define frexpf frexp /**< The alias for frexp(). */
236
237 /**
238 The ldexp() function multiplies a floating-point number by an integral
239 power of 2. It returns the value of \a __x times 2 raised to the power
240 \a __exp.
241 */
242 extern double ldexp(double __x, int __exp) __ATTR_CONST__;
243 #define ldexpf ldexp /**< The alias for ldexp(). */
244
245 /**
246 The exp() function returns the exponential value of \a __x.
247 */
248 extern double exp(double __x) __ATTR_CONST__;
249 #define expf exp /**< The alias for exp(). */
250
251 /**
252 The cosh() function returns the hyperbolic cosine of \a __x.
253 */
254 extern double cosh(double __x) __ATTR_CONST__;
255 #define coshf cosh /**< The alias for cosh(). */
256
257 /**
258 The sinh() function returns the hyperbolic sine of \a __x.
259 */
260 extern double sinh(double __x) __ATTR_CONST__;
261 #define sinhf sinh /**< The alias for sinh(). */
262
263 /**
264 The tanh() function returns the hyperbolic tangent of \a __x.
265 */
266 extern double tanh(double __x) __ATTR_CONST__;
267 #define tanhf tanh /**< The alias for tanh(). */
268
269 /**
270 The acos() function computes the principal value of the arc cosine of
271 \a __x. The returned value is in the range [0, pi] radians. A domain
272 error occurs for arguments not in the range [-1, +1].
273 */
274 extern double acos(double __x) __ATTR_CONST__;
275 #define acosf acos /**< The alias for acos(). */
276
277 /**
278 The asin() function computes the principal value of the arc sine of
279 \a __x. The returned value is in the range [-pi/2, pi/2] radians. A
280 domain error occurs for arguments not in the range [-1, +1].
281 */
282 extern double asin(double __x) __ATTR_CONST__;
283 #define asinf asin /**< The alias for asin(). */
284
285 /**
286 The atan() function computes the principal value of the arc tangent
287 of \a __x. The returned value is in the range [-pi/2, pi/2] radians.
288 */
289 extern double atan(double __x) __ATTR_CONST__;
290 #define atanf atan /**< The alias for atan(). */
291
292 /**
293 The atan2() function computes the principal value of the arc tangent
294 of <em>__y / __x</em>, using the signs of both arguments to determine
295 the quadrant of the return value. The returned value is in the range
296 [-pi, +pi] radians.
297 */
298 extern double atan2(double __y, double __x) __ATTR_CONST__;
299 #define atan2f atan2 /**< The alias for atan2(). */
300
301 /**
302 The log() function returns the natural logarithm of argument \a __x.
303 */
304 extern double log(double __x) __ATTR_CONST__;
305 #define logf log /**< The alias for log(). */
306
307 /**
308 The log10() function returns the logarithm of argument \a __x to base 10.
309 */
310 extern double log10(double __x) __ATTR_CONST__;
311 #define log10f log10 /**< The alias for log10(). */
312
313 /**
314 The function pow() returns the value of \a __x to the exponent \a __y.
315 */
316 extern double pow(double __x, double __y) __ATTR_CONST__;
317 #define powf pow /**< The alias for pow(). */
318
319 /**
320 The function isnan() returns 1 if the argument \a __x represents a
321 "not-a-number" (NaN) object, otherwise 0.
322 */
323 extern int isnan(double __x) __ATTR_CONST__;
324 #define isnanf isnan /**< The alias for isnan(). */
325
326 /**
327 The function isinf() returns 1 if the argument \a __x is positive
328 infinity, -1 if \a __x is negative infinity, and 0 otherwise.
329
330 \note The GCC 4.3 can replace this function with inline code that
331 returns the 1 value for both infinities (gcc bug #35509).
332 */
333 extern int isinf(double __x) __ATTR_CONST__;
334 #define isinff isinf /**< The alias for isinf(). */
335
336 /**
337 The isfinite() function returns a nonzero value if \a __x is finite:
338 not plus or minus infinity, and not NaN.
339 */
340 __ATTR_CONST__ static inline int isfinite (double __x)
341 {
342 unsigned char __exp;
343 __asm__ (
344 "mov %0, %C1 \n\t"
345 "lsl %0 \n\t"
346 "mov %0, %D1 \n\t"
347 "rol %0 "
348 : "=r" (__exp)
349 : "r" (__x) );
350 return __exp != 0xff;
351 }
352 #define isfinitef isfinite /**< The alias for isfinite(). */
353
354 /**
355 The copysign() function returns \a __x but with the sign of \a __y.
356 They work even if \a __x or \a __y are NaN or zero.
357 */
358 __ATTR_CONST__ static inline double copysign (double __x, double __y)
359 {
360 __asm__ (
361 "bst %D2, 7 \n\t"
362 "bld %D0, 7 "
363 : "=r" (__x)
364 : "0" (__x), "r" (__y) );
365 return __x;
366 }
367 #define copysignf copysign /**< The alias for copysign(). */
368
369 /**
370 The signbit() function returns a nonzero value if the value of \a __x
371 has its sign bit set. This is not the same as `\a __x < 0.0',
372 because IEEE 754 floating point allows zero to be signed. The
373 comparison `-0.0 < 0.0' is false, but `signbit (-0.0)' will return a
374 nonzero value.
375 */
376 extern int signbit (double __x) __ATTR_CONST__;
377 #define signbitf signbit /**< The alias for signbit(). */
378
379 /**
380 The fdim() function returns <em>max(__x - __y, 0)</em>. If \a __x or
381 \a __y or both are NaN, NaN is returned.
382 */
383 extern double fdim (double __x, double __y) __ATTR_CONST__;
384 #define fdimf fdim /**< The alias for fdim(). */
385
386 /**
387 The fma() function performs floating-point multiply-add. This is the
388 operation <em>(__x * __y) + __z</em>, but the intermediate result is
389 not rounded to the destination type. This can sometimes improve the
390 precision of a calculation.
391 */
392 extern double fma (double __x, double __y, double __z) __ATTR_CONST__;
393 #define fmaf fma /**< The alias for fma(). */
394
395 /**
396 The fmax() function returns the greater of the two values \a __x and
397 \a __y. If an argument is NaN, the other argument is returned. If
398 both arguments are NaN, NaN is returned.
399 */
400 extern double fmax (double __x, double __y) __ATTR_CONST__;
401 #define fmaxf fmax /**< The alias for fmax(). */
402
403 /**
404 The fmin() function returns the lesser of the two values \a __x and
405 \a __y. If an argument is NaN, the other argument is returned. If
406 both arguments are NaN, NaN is returned.
407 */
408 extern double fmin (double __x, double __y) __ATTR_CONST__;
409 #define fminf fmin /**< The alias for fmin(). */
410
411 /**
412 The trunc() function rounds \a __x to the nearest integer not larger
413 in absolute value.
414 */
415 extern double trunc (double __x) __ATTR_CONST__;
416 #define truncf trunc /**< The alias for trunc(). */
417
418 /**
419 The round() function rounds \a __x to the nearest integer, but rounds
420 halfway cases away from zero (instead of to the nearest even integer).
421 Overflow is impossible.
422
423 \return The rounded value. If \a __x is an integral or infinite, \a
424 __x itself is returned. If \a __x is \c NaN, then \c NaN is returned.
425 */
426 extern double round (double __x) __ATTR_CONST__;
427 #define roundf round /**< The alias for round(). */
428
429 /**
430 The lround() function rounds \a __x to the nearest integer, but rounds
431 halfway cases away from zero (instead of to the nearest even integer).
432 This function is similar to round() function, but it differs in type of
433 return value and in that an overflow is possible.
434
435 \return The rounded long integer value. If \a __x is not a finite number
436 or an overflow was, this realization returns the \c LONG_MIN value
437 (0x80000000).
438 */
439 extern long lround (double __x) __ATTR_CONST__;
440 #define lroundf lround /**< The alias for lround(). */
441
442 /**
443 The lrint() function rounds \a __x to the nearest integer, rounding the
444 halfway cases to the even integer direction. (That is both 1.5 and 2.5
445 values are rounded to 2). This function is similar to rint() function,
446 but it differs in type of return value and in that an overflow is
447 possible.
448
449 \return The rounded long integer value. If \a __x is not a finite
450 number or an overflow was, this realization returns the \c LONG_MIN
451 value (0x80000000).
452 */
453 extern long lrint (double __x) __ATTR_CONST__;
454 #define lrintf lrint /**< The alias for lrint(). */
455
456 #ifdef __cplusplus
457 }
458 #endif
459
460 /*@}*/
461 #endif /* !__MATH_H */

Automatically generated by Doxygen 1.8.1.1 on Fri Aug 17 2012.