avr-libc  2.2.0git
Standard C library for AVR-GCC

AVR Libc Home Page

AVRs

AVR Libc Development Pages

Main Page

User Manual

Library Reference

FAQ

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$ */
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
121extern "C" {
122#endif
123
124/**
125 The cos() function returns the cosine of \a __x, measured in radians.
126 */
127__ATTR_CONST__ extern float cosf (float __x);
128__ATTR_CONST__ extern double cos (double __x);
129__ATTR_CONST__ extern long double cosl (long double __x);
130
131/**
132 The sin() function returns the sine of \a __x, measured in radians.
133 */
134__ATTR_CONST__ extern float sinf (float __x);
135__ATTR_CONST__ extern double sin (double __x);
136__ATTR_CONST__ extern long double sinl (long double __x);
137
138/**
139 The tan() function returns the tangent of \a __x, measured in radians.
140 */
141__ATTR_CONST__ extern float tanf (float __x);
142__ATTR_CONST__ extern double tan (double __x);
143__ATTR_CONST__ extern long double tanl (long double __x);
144
145/**
146 The fabs() function computes the absolute value of a floating-point
147 number \a __x.
148 */
149static inline float fabsf (float __x)
150{
151 return __builtin_fabsf (__x);
152}
153
154static inline double fabs (double __x)
155{
156 return __builtin_fabs (__x);
157}
158
159static inline long double fabsl (long double __x)
160{
161 return __builtin_fabsl (__x);
162}
163
164/**
165 The function fmod() returns the floating-point remainder of <em>__x /
166 __y</em>.
167 */
168__ATTR_CONST__ extern float fmodf (float __x, float __y);
169__ATTR_CONST__ extern double fmod (double __x, double __y);
170__ATTR_CONST__ extern long double fmodl (long double __x, long double __y);
171
172/**
173 The modf() function breaks the argument \a __x into integral and
174 fractional parts, each of which has the same sign as the argument.
175 It stores the integral part as a double in the object pointed to by
176 \a __iptr.
177
178 The modf() function returns the signed fractional part of \a __x.
179
180 \note This implementation skips writing by zero pointer. However,
181 the GCC 4.3 can replace this function with inline code that does not
182 permit to use NULL address for the avoiding of storing.
183 */
184extern float modff (float __x, float *__iptr);
185extern double modf (double __x, double *__iptr);
186extern long double modfl (long double __x, long double *__iptr);
187
188/**
189 The sqrt() function returns the non-negative square root of \a __x.
190 */
191__ATTR_CONST__ extern float sqrtf (float __x);
192__ATTR_CONST__ extern double sqrt (double __x);
193__ATTR_CONST__ extern long double sqrtl (long double __x);
194
195/**
196 The cbrt() function returns the cube root of \a __x.
197 */
198__ATTR_CONST__ extern float cbrtf (float __x);
199__ATTR_CONST__ extern double cbrt (double __x);
200__ATTR_CONST__ extern long double cbrtl (long double __x);
201
202/**
203 The hypot() function returns <em>sqrt(__x*__x + __y*__y)</em>. This
204 is the length of the hypotenuse of a right triangle with sides of
205 length \a __x and \a __y, or the distance of the point (\a __x, \a
206 __y) from the origin. Using this function instead of the direct
207 formula is wise, since the error is much smaller. No underflow with
208 small \a __x and \a __y. No overflow if result is in range.
209 */
210__ATTR_CONST__ extern float hypotf (float __x, float __y);
211__ATTR_CONST__ extern double hypot (double __x, double __y);
212__ATTR_CONST__ extern long double hypotl (long double __x, long double __y);
213
214/**
215 The function square() returns <em>__x * __x</em>.
216
217 \note This function does not belong to the C standard definition.
218 */
219__ATTR_CONST__ extern float squaref (float __x);
220__ATTR_CONST__ extern double square (double __x);
221__ATTR_CONST__ extern long double squarel (long double __x);
222
223/**
224 The floor() function returns the largest integral value less than or
225 equal to \a __x, expressed as a floating-point number.
226 */
227__ATTR_CONST__ extern float floorf (float __x);
228__ATTR_CONST__ extern double floor (double __x);
229__ATTR_CONST__ extern long double floorl (long double __x);
230
231/**
232 The ceil() function returns the smallest integral value greater than
233 or equal to \a __x, expressed as a floating-point number.
234 */
235__ATTR_CONST__ extern float ceilf (float __x);
236__ATTR_CONST__ extern double ceil (double __x);
237__ATTR_CONST__ extern long double ceill (long double __x);
238
239/**
240 The frexp() function breaks a floating-point number into a normalized
241 fraction and an integral power of 2. It stores the integer in the \c
242 int object pointed to by \a __pexp.
243
244 If \a __x is a normal float point number, the frexp() function
245 returns the value \c v, such that \c v has a magnitude in the
246 interval [1/2, 1) or zero, and \a __x equals \c v times 2 raised to
247 the power \a __pexp. If \a __x is zero, both parts of the result are
248 zero. If \a __x is not a finite number, the frexp() returns \a __x as
249 is and stores 0 by \a __pexp.
250
251 \note This implementation permits a zero pointer as a directive to
252 skip a storing the exponent.
253 */
254__ATTR_CONST__ extern float frexpf (float __x, int *__pexp);
255__ATTR_CONST__ extern double frexp (double __x, int *__pexp);
256__ATTR_CONST__ extern long double frexpl (long double __x, int *__pexp);
257
258/**
259 The ldexp() function multiplies a floating-point number by an integral
260 power of 2. It returns the value of \a __x times 2 raised to the power
261 \a __exp.
262 */
263__ATTR_CONST__ extern float ldexpf (float __x, int __exp);
264__ATTR_CONST__ extern double ldexp (double __x, int __exp);
265__ATTR_CONST__ extern long double ldexpl (long double __x, int __exp);
266
267/**
268 The exp() function returns the exponential value of \a __x.
269 */
270__ATTR_CONST__ extern float expf (float __x);
271__ATTR_CONST__ extern double exp (double __x);
272__ATTR_CONST__ extern long double expl (long double __x);
273
274/**
275 The cosh() function returns the hyperbolic cosine of \a __x.
276 */
277__ATTR_CONST__ extern float coshf (float __x);
278__ATTR_CONST__ extern double cosh (double __x);
279__ATTR_CONST__ extern long double coshl (long double __x);
280
281/**
282 The sinh() function returns the hyperbolic sine of \a __x.
283 */
284__ATTR_CONST__ extern float sinhf (float __x);
285__ATTR_CONST__ extern double sinh (double __x);
286__ATTR_CONST__ extern long double sinhl (long double __x);
287
288/**
289 The tanh() function returns the hyperbolic tangent of \a __x.
290 */
291__ATTR_CONST__ extern float tanhf (float __x);
292__ATTR_CONST__ extern double tanh (double __x);
293__ATTR_CONST__ extern long double tanhl (long double __x);
294
295/**
296 The acos() function computes the principal value of the arc cosine of
297 \a __x. The returned value is in the range [0, pi] radians. A domain
298 error occurs for arguments not in the range [-1, +1].
299 */
300__ATTR_CONST__ extern float acosf (float __x);
301__ATTR_CONST__ extern double acos (double __x);
302__ATTR_CONST__ extern long double acosl (long double __x);
303
304/**
305 The asin() function computes the principal value of the arc sine of
306 \a __x. The returned value is in the range [-pi/2, pi/2] radians. A
307 domain error occurs for arguments not in the range [-1, +1].
308 */
309__ATTR_CONST__ extern float asinf (float __x);
310__ATTR_CONST__ extern double asin (double __x);
311__ATTR_CONST__ extern long double asinl (long double __x);
312
313/**
314 The atan() function computes the principal value of the arc tangent
315 of \a __x. The returned value is in the range [-pi/2, pi/2] radians.
316 */
317__ATTR_CONST__ extern float atanf (float __x);
318__ATTR_CONST__ extern double atan (double __x);
319__ATTR_CONST__ extern long double atanl (long double __x);
320
321/**
322 The atan2() function computes the principal value of the arc tangent
323 of <em>__y / __x</em>, using the signs of both arguments to determine
324 the quadrant of the return value. The returned value is in the range
325 [-pi, +pi] radians.
326 */
327__ATTR_CONST__ extern float atan2f (float __y, float __x);
328__ATTR_CONST__ extern double atan2 (double __y, double __x);
329__ATTR_CONST__ extern long double atan2l (long double __y, long double __x);
330
331/**
332 The log() function returns the natural logarithm of argument \a __x.
333 */
334__ATTR_CONST__ extern float logf (float __x);
335__ATTR_CONST__ extern double log (double __x);
336__ATTR_CONST__ extern long double logl (long double __x);
337
338/**
339 The log10() function returns the logarithm of argument \a __x to base 10.
340 */
341__ATTR_CONST__ extern float log10f (float __x);
342__ATTR_CONST__ extern double log10 (double __x);
343__ATTR_CONST__ extern long double log10l (long double __x);
344
345/**
346 The function pow() returns the value of \a __x to the exponent \a __y.
347 */
348__ATTR_CONST__ extern float powf (float __x, float __y);
349__ATTR_CONST__ extern double pow (double __x, double __y);
350__ATTR_CONST__ extern long double powl (long double __x, long double __y);
351
352/**
353 The function isnan() returns 1 if the argument \a __x represents a
354 "not-a-number" (NaN) object, otherwise 0.
355 */
356__ATTR_CONST__ extern int isnanf (float __x);
357__ATTR_CONST__ extern int isnan (double __x);
358__ATTR_CONST__ extern int isnanl (long double __x);
359
360/**
361 The function isinf() returns 1 if the argument \a __x is positive
362 infinity, -1 if \a __x is negative infinity, and 0 otherwise.
363
364 \note The GCC 4.3 can replace this function with inline code that
365 returns the 1 value for both infinities (gcc bug #35509).
366 */
367__ATTR_CONST__ extern int isinff (float __x);
368__ATTR_CONST__ extern int isinf (double __x);
369__ATTR_CONST__ extern int isinfl (long double __x);
370
371/**
372 The isfinite() function returns a nonzero value if \a __x is finite:
373 not plus or minus infinity, and not NaN.
374 */
375__ATTR_CONST__ static inline int isfinitef (float __x)
376{
377 unsigned char __exp;
378 __asm__ (
379 "mov %0, %C1 \n\t"
380 "lsl %0 \n\t"
381 "mov %0, %D1 \n\t"
382 "rol %0 "
383 : "=&r" (__exp)
384 : "r" (__x) );
385 return __exp != 0xff;
386}
387
388#if __SIZEOF_DOUBLE__ == __SIZEOF_FLOAT__
389static inline int isfinite (double __x)
390{
391 return isfinitef (__x);
392}
393#else
394int isfinite (double __x);
395#endif /* double = float */
396
397#if __SIZEOF_LONG_DOUBLE__ == __SIZEOF_FLOAT__
398static inline int isfinitel (long double __x)
399{
400 return isfinitef (__x);
401}
402#else
403int isfinitel (long double __x);
404#endif /* long double = float */
405
406/**
407 The copysign() function returns \a __x but with the sign of \a __y.
408 They work even if \a __x or \a __y are NaN or zero.
409*/
410__ATTR_CONST__ static inline float copysignf (float __x, float __y)
411{
412 __asm__ (
413 "bst %D2, 7 \n\t"
414 "bld %D0, 7 "
415 : "=r" (__x)
416 : "0" (__x), "r" (__y) );
417 return __x;
418}
419
420__ATTR_CONST__ static inline double copysign (double __x, double __y)
421{
422 __asm__ (
423 "bst %r1+%2-1, 7" "\n\t"
424 "bld %r0+%2-1, 7"
425 : "+r" (__x)
426 : "r" (__y), "n" (__SIZEOF_DOUBLE__));
427 return __x;
428}
429
430__ATTR_CONST__ static inline long double copysignl (long double __x, long double __y)
431{
432 __asm__ (
433 "bst %r1+%2-1, 7" "\n\t"
434 "bld %r0+%2-1, 7"
435 : "+r" (__x)
436 : "r" (__y), "n" (__SIZEOF_LONG_DOUBLE__));
437 return __x;
438}
439
440/**
441 The signbit() function returns a nonzero value if the value of \a __x
442 has its sign bit set. This is not the same as `\a __x < 0.0',
443 because IEEE 754 floating point allows zero to be signed. The
444 comparison `-0.0 < 0.0' is false, but `signbit (-0.0)' will return a
445 nonzero value.
446 */
447__ATTR_CONST__ extern int signbitf (float __x);
448__ATTR_CONST__ extern int signbit (double __x);
449__ATTR_CONST__ extern int signbitl (long double __x);
450
451/**
452 The fdim() function returns <em>max(__x - __y, 0)</em>. If \a __x or
453 \a __y or both are NaN, NaN is returned.
454 */
455__ATTR_CONST__ extern float fdimf (float __x, float __y);
456__ATTR_CONST__ extern double fdim (double __x, double __y);
457__ATTR_CONST__ extern long double fdiml (long double __x, long double __y);
458
459/**
460 The fma() function performs floating-point multiply-add. This is the
461 operation <em>(__x * __y) + __z</em>, but the intermediate result is
462 not rounded to the destination type. This can sometimes improve the
463 precision of a calculation.
464 */
465__ATTR_CONST__ extern float fmaf (float __x, float __y, float __z);
466__ATTR_CONST__ extern double fma (double __x, double __y, double __z);
467__ATTR_CONST__ extern long double fmal (long double __x, long double __y, long double __z);
468
469/**
470 The fmax() function returns the greater of the two values \a __x and
471 \a __y. If an argument is NaN, the other argument is returned. If
472 both arguments are NaN, NaN is returned.
473 */
474__ATTR_CONST__ extern float fmaxf (float __x, float __y);
475__ATTR_CONST__ extern double fmax (double __x, double __y);
476__ATTR_CONST__ extern long double fmaxl (long double __x, long double __y);
477
478/**
479 The fmin() function returns the lesser of the two values \a __x and
480 \a __y. If an argument is NaN, the other argument is returned. If
481 both arguments are NaN, NaN is returned.
482 */
483__ATTR_CONST__ extern float fminf (float __x, float __y);
484__ATTR_CONST__ extern double fmin (double __x, double __y);
485__ATTR_CONST__ extern long double fminl (long double __x, long double __y);
486
487/**
488 The trunc() function rounds \a __x to the nearest integer not larger
489 in absolute value.
490 */
491__ATTR_CONST__ extern float truncf (float __x);
492__ATTR_CONST__ extern double trunc (double __x);
493__ATTR_CONST__ extern long double truncl (long double __x);
494
495/**
496 The round() function rounds \a __x to the nearest integer, but rounds
497 halfway cases away from zero (instead of to the nearest even integer).
498 Overflow is impossible.
499
500 \return The rounded value. If \a __x is an integral or infinite, \a
501 __x itself is returned. If \a __x is \c NaN, then \c NaN is returned.
502 */
503__ATTR_CONST__ extern float roundf (float __x);
504__ATTR_CONST__ extern double round (double __x);
505__ATTR_CONST__ extern long double roundl (long double __x);
506
507/**
508 The lround() function rounds \a __x to the nearest integer, but rounds
509 halfway cases away from zero (instead of to the nearest even integer).
510 This function is similar to round() function, but it differs in type of
511 return value and in that an overflow is possible.
512
513 \return The rounded long integer value. If \a __x is not a finite number
514 or an overflow was, this realization returns the \c LONG_MIN value
515 (0x80000000).
516 */
517__ATTR_CONST__ extern long lroundf (float __x);
518__ATTR_CONST__ extern long lround (double __x);
519__ATTR_CONST__ extern long lroundl (long double __x);
520
521/**
522 The lrint() function rounds \a __x to the nearest integer, rounding the
523 halfway cases to the even integer direction. (That is both 1.5 and 2.5
524 values are rounded to 2). This function is similar to rint() function,
525 but it differs in type of return value and in that an overflow is
526 possible.
527
528 \return The rounded long integer value. If \a __x is not a finite
529 number or an overflow was, this realization returns the \c LONG_MIN
530 value (0x80000000).
531 */
532__ATTR_CONST__ extern long lrintf (float __x);
533__ATTR_CONST__ extern long lrint (double __x);
534__ATTR_CONST__ extern long lrintl (long double __x);
535
536#ifdef __cplusplus
537}
538#endif
539
540/*@}*/
541#endif /* !__MATH_H */
float fminf(float __x, float __y)
int isinff(float __x)
float expf(float __x)
float logf(float __x)
long lrintf(float __x)
float cosf(float __x)
static int isfinitef(float __x)
Definition: math.h:375
float frexpf(float __x, int *__pexp)
float fdimf(float __x, float __y)
float roundf(float __x)
float squaref(float __x)
long lroundf(float __x)
float asinf(float __x)
float ldexpf(float __x, int __exp)
static float fabsf(float __x)
Definition: math.h:149
float cbrtf(float __x)
float powf(float __x, float __y)
float fmaf(float __x, float __y, float __z)
float floorf(float __x)
float fmodf(float __x, float __y)
float sinhf(float __x)
float hypotf(float __x, float __y)
float ceilf(float __x)
float coshf(float __x)
float atan2f(float __y, float __x)
float fmaxf(float __x, float __y)
float tanhf(float __x)
float sinf(float __x)
float sqrtf(float __x)
int isnanf(float __x)
float truncf(float __x)
static float copysignf(float __x, float __y)
Definition: math.h:410
float modff(float __x, float *__iptr)
float tanf(float __x)
int signbitf(float __x)
float log10f(float __x)
float acosf(float __x)
float atanf(float __x)