AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page User Manual Reference FAQ Example Projects

math.h

Go to the documentation of this file.
01	1 Copyright (c) 2002,2007-2009 Michael Stumpf
022 033Portions of documentation Copyright (c) 1990 - 1994 044The Regents of the University of California. 055 066All rights reserved. 077 088Redistribution and use in source and binary forms, with or without 099modification, are permitted provided that the following conditions are met: 1010 1111* Redistributions of source code must retain the above copyright 1212notice, this list of conditions and the following disclaimer. 1313 1414* Redistributions in binary form must reproduce the above copyright 1515notice, this list of conditions and the following disclaimer in 1616the documentation and/or other materials provided with the 1717distribution. 1818 1919* Neither the name of the copyright holders nor the names of 2020contributors may be used to endorse or promote products derived 2121from this software without specific prior written permission. 2222 2323THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2424AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2525IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2626ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 2727LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2828CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2929SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3030INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3131CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3232ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3333POSSIBILITY OF SUCH DAMAGE. */ 3434 3535/* $Id: math.h 2503 2016-02-07 22:59:47Z joerg_wunsch $ */ 3636 3737/* 3838math.h - mathematical functions 3939 4040Author : Michael Stumpf 4141Michael.Stumpf@t-online.de 4242 4343__ATTR_CONST__ added by marekm@linux.org.pl for functions 4444that "do not examine any values except their arguments, and have 4545no effects except the return value", for better optimization by gcc. 4646*/ 4747 4848#ifndef __MATH_H 4949#define __MATH_H 5050 5151/** \file */ 5252/** \defgroup avr_math <math.h>: Mathematics 5353\code #include <math.h> \endcode 5454 5555This header file declares basic mathematics constants and 5656functions. 5757 5858\par Notes: 5959- In order to access the functions declared herein, it is usually 6060also required to additionally link against the library \c libm.a. 6161See also the related \ref faq_libm "FAQ entry". 6262- Math functions do not raise exceptions and do not change the 6363\c errno variable. Therefore the majority of them are declared 6464with const attribute, for better optimization by GCC. */ 6565 6666 6767/** \ingroup avr_math */ 6868/*@{*/ 6969 7070/** The constant \a e. */ 71 71#define M_E 2.7182818284590452354 7272 7373/** The logarithm of the \a e to base 2. */ 74 74#define M_LOG2E 1.4426950408889634074 /* log_2 e */ 7575 7676/** The logarithm of the \a e to base 10. */ 77 77#define M_LOG10E 0.43429448190325182765 /* log_10 e */ 7878 7979/** The natural logarithm of the 2. */ 80 80#define M_LN2 0.69314718055994530942 /* log_e 2 */ 8181 8282/** The natural logarithm of the 10. */ 83 83#define M_LN10 2.30258509299404568402 /* log_e 10 */ 8484 8585/** The constant \a pi. */ 86 86#define M_PI 3.14159265358979323846 /* pi */ 8787 8888/** The constant \a pi/2. */ 89 89#define M_PI_2 1.57079632679489661923 /* pi/2 */ 9090 9191/** The constant \a pi/4. */ 92 92#define M_PI_4 0.78539816339744830962 /* pi/4 */ 9393 9494/** The constant \a 1/pi. */ 95 95#define M_1_PI 0.31830988618379067154 /* 1/pi */ 9696 9797/** The constant \a 2/pi. */ 98 98#define M_2_PI 0.63661977236758134308 /* 2/pi */ 9999 100100/** The constant \a 2/sqrt(pi). */ 101 101#define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */ 102102 103103/** The square root of 2. */ 104 104#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */ 105105 106106/** The constant \a 1/sqrt(2). */ 107 107#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */ 108108 109109/** NAN constant. */ 110 110#define NAN __builtin_nan("") 111111 112112/** INFINITY constant. */ 113 113#define INFINITY __builtin_inf() 114114 115115 116116#ifndef __ATTR_CONST__ 117117# define __ATTR_CONST__ __attribute__((__const__)) 118118#endif 119119 120120#ifdef __cplusplus 121121extern "C" { 122122#endif 123123 124124/** 125125The cos() function returns the cosine of \a __x, measured in radians. 126126*/ 127127extern double cos(double __x) __ATTR_CONST__; 128 128#define cosf cos /**< The alias for cos(). */ 129129 130130/** 131131The sin() function returns the sine of \a __x, measured in radians. 132132*/ 133133extern double sin(double __x) __ATTR_CONST__; 134 134#define sinf sin /**< The alias for sin(). */ 135135 136136/** 137137The tan() function returns the tangent of \a __x, measured in radians. 138138*/ 139139extern double tan(double __x) __ATTR_CONST__; 140 140#define tanf tan /**< The alias for tan(). */ 141141 142142/** 143143The fabs() function computes the absolute value of a floating-point 144144number \a __x. 145145*/ 146146extern double fabs(double __x) __ATTR_CONST__; 147 147#define fabsf fabs /**< The alias for fabs(). */ 148148 149149/** 150150The function fmod() returns the floating-point remainder of <em>__x / 151151__y</em>. 152152*/ 153153extern double fmod(double __x, double __y) __ATTR_CONST__; 154 154#define fmodf fmod /**< The alias for fmod(). */ 155155 156156/** 157157The modf() function breaks the argument \a __x into integral and 158158fractional parts, each of which has the same sign as the argument. 159159It stores the integral part as a double in the object pointed to by 160160\a __iptr. 161161 162162The modf() function returns the signed fractional part of \a __x. 163163 164164\note This implementation skips writing by zero pointer. However, 165165the GCC 4.3 can replace this function with inline code that does not 166166permit to use NULL address for the avoiding of storing. 167167*/ 168168extern double modf(double __x, double *__iptr); 169169 170170/** An alias for modf(). */ 171171extern float modff (float __x, float *__iptr); 172172 173173/** 174174The sqrt() function returns the non-negative square root of \a __x. 175175*/ 176176extern double sqrt(double __x) __ATTR_CONST__; 177177 178178/** An alias for sqrt(). */ 179179extern float sqrtf (float) __ATTR_CONST__; 180180 181181/** 182182The cbrt() function returns the cube root of \a __x. 183183*/ 184184 extern double cbrt(double __x) __ATTR_CONST__; 185 185#define cbrtf cbrt /**< The alias for cbrt(). */ 186186 187187/** 188188The hypot() function returns <em>sqrt(__x*__x + __y*__y)</em>. This 189189is the length of the hypotenuse of a right triangle with sides of 190190length \a __x and \a __y, or the distance of the point (\a __x, \a 191191__y) from the origin. Using this function instead of the direct 192192formula is wise, since the error is much smaller. No underflow with 193193small \a __x and \a __y. No overflow if result is in range. 194194*/ 195195extern double hypot (double __x, double __y) __ATTR_CONST__; 196 196#define hypotf hypot /**< The alias for hypot(). */ 197197 198198/** 199199The function square() returns <em>__x * __x</em>. 200200 201201\note This function does not belong to the C standard definition. 202202*/ 203203extern double square(double __x) __ATTR_CONST__; 204 204#define squaref square /**< The alias for square(). */ 205205 206206/** 207207The floor() function returns the largest integral value less than or 208208equal to \a __x, expressed as a floating-point number. 209209*/ 210210extern double floor(double __x) __ATTR_CONST__; 211 211#define floorf floor /**< The alias for floor(). */ 212212 213213/** 214214The ceil() function returns the smallest integral value greater than 215215or equal to \a __x, expressed as a floating-point number. 216216*/ 217217extern double ceil(double __x) __ATTR_CONST__; 218 218#define ceilf ceil /**< The alias for ceil(). */ 219219 220220/** 221221The frexp() function breaks a floating-point number into a normalized 222222fraction and an integral power of 2. It stores the integer in the \c 223223int object pointed to by \a __pexp. 224224 225225If \a __x is a normal float point number, the frexp() function 226226returns the value \c v, such that \c v has a magnitude in the 227227interval [1/2, 1) or zero, and \a __x equals \c v times 2 raised to 228228the power \a __pexp. If \a __x is zero, both parts of the result are 229229zero. If \a __x is not a finite number, the frexp() returns \a __x as 230230is and stores 0 by \a __pexp. 231231 232232\note This implementation permits a zero pointer as a directive to 233233skip a storing the exponent. 234234*/ 235235extern double frexp(double __x, int *__pexp); 236 236#define frexpf frexp /**< The alias for frexp(). */ 237237 238238/** 239239The ldexp() function multiplies a floating-point number by an integral 240240power of 2. It returns the value of \a __x times 2 raised to the power 241241\a __exp. 242242*/ 243243extern double ldexp(double __x, int __exp) __ATTR_CONST__; 244 244#define ldexpf ldexp /**< The alias for ldexp(). */ 245245 246246/** 247247The exp() function returns the exponential value of \a __x. 248248*/ 249249extern double exp(double __x) __ATTR_CONST__; 250 250#define expf exp /**< The alias for exp(). */ 251251 252252/** 253253The cosh() function returns the hyperbolic cosine of \a __x. 254254*/ 255255extern double cosh(double __x) __ATTR_CONST__; 256 256#define coshf cosh /**< The alias for cosh(). */ 257257 258258/** 259259The sinh() function returns the hyperbolic sine of \a __x. 260260*/ 261261extern double sinh(double __x) __ATTR_CONST__; 262 262#define sinhf sinh /**< The alias for sinh(). */ 263263 264264/** 265265The tanh() function returns the hyperbolic tangent of \a __x. 266266*/ 267267extern double tanh(double __x) __ATTR_CONST__; 268 268#define tanhf tanh /**< The alias for tanh(). */ 269269 270270/** 271271The acos() function computes the principal value of the arc cosine of 272272\a __x. The returned value is in the range [0, pi] radians. A domain 273273error occurs for arguments not in the range [-1, +1]. 274274*/ 275275extern double acos(double __x) __ATTR_CONST__; 276 276#define acosf acos /**< The alias for acos(). */ 277277 278278/** 279279The asin() function computes the principal value of the arc sine of 280280\a __x. The returned value is in the range [-pi/2, pi/2] radians. A 281281domain error occurs for arguments not in the range [-1, +1]. 282282*/ 283283extern double asin(double __x) __ATTR_CONST__; 284 284#define asinf asin /**< The alias for asin(). */ 285285 286286/** 287287The atan() function computes the principal value of the arc tangent 288288of \a __x. The returned value is in the range [-pi/2, pi/2] radians. 289289*/ 290290extern double atan(double __x) __ATTR_CONST__; 291 291#define atanf atan /**< The alias for atan(). */ 292292 293293/** 294294The atan2() function computes the principal value of the arc tangent 295295of <em>__y / __x</em>, using the signs of both arguments to determine 296296the quadrant of the return value. The returned value is in the range 297297[-pi, +pi] radians. 298298*/ 299299extern double atan2(double __y, double __x) __ATTR_CONST__; 300 300#define atan2f atan2 /**< The alias for atan2(). */ 301301 302302/** 303303The log() function returns the natural logarithm of argument \a __x. 304304*/ 305305extern double log(double __x) __ATTR_CONST__; 306 306#define logf log /**< The alias for log(). */ 307307 308308/** 309309The log10() function returns the logarithm of argument \a __x to base 10. 310310*/ 311311extern double log10(double __x) __ATTR_CONST__; 312 312#define log10f log10 /**< The alias for log10(). */ 313313 314314/** 315315The function pow() returns the value of \a __x to the exponent \a __y. 316316*/ 317317extern double pow(double __x, double __y) __ATTR_CONST__; 318 318#define powf pow /**< The alias for pow(). */ 319319 320320/** 321321The function isnan() returns 1 if the argument \a __x represents a 322322"not-a-number" (NaN) object, otherwise 0. 323323*/ 324324extern int isnan(double __x) __ATTR_CONST__; 325 325#define isnanf isnan /**< The alias for isnan(). */ 326326 327327/** 328328The function isinf() returns 1 if the argument \a __x is positive 329329infinity, -1 if \a __x is negative infinity, and 0 otherwise. 330330 331331\note The GCC 4.3 can replace this function with inline code that 332332returns the 1 value for both infinities (gcc bug #35509). 333333*/ 334334extern int isinf(double __x) __ATTR_CONST__; 335 335#define isinff isinf /**< The alias for isinf(). */ 336336 337337/** 338338The isfinite() function returns a nonzero value if \a __x is finite: 339339not plus or minus infinity, and not NaN. 340340*/ 341 341__ATTR_CONST__ static inline int isfinite (double __x) 342342{ 343343unsigned char __exp; 344344__asm__ ( 345345"mov %0, %C1 \n\t" 346346"lsl %0 \n\t" 347347"mov %0, %D1 \n\t" 348348"rol %0 " 349349: "=r" (__exp) 350350: "r" (__x) ); 351351return __exp != 0xff; 352352} 353 353#define isfinitef isfinite /**< The alias for isfinite(). */ 354354 355355/** 356356The copysign() function returns \a __x but with the sign of \a __y. 357357They work even if \a __x or \a __y are NaN or zero. 358358*/ 359 359__ATTR_CONST__ static inline double copysign (double __x, double __y) 360360{ 361361__asm__ ( 362362"bst %D2, 7 \n\t" 363363"bld %D0, 7 " 364364: "=r" (__x) 365365: "0" (__x), "r" (__y) ); 366366return __x; 367367} 368 368#define copysignf copysign /**< The alias for copysign(). */ 369369 370370/** 371371The signbit() function returns a nonzero value if the value of \a __x 372372has its sign bit set. This is not the same as `\a __x < 0.0', 373373because IEEE 754 floating point allows zero to be signed. The 374374comparison `-0.0 < 0.0' is false, but `signbit (-0.0)' will return a 375375nonzero value. 376376*/ 377377extern int signbit (double __x) __ATTR_CONST__; 378 378#define signbitf signbit /**< The alias for signbit(). */ 379379 380380/** 381381The fdim() function returns <em>max(__x - __y, 0)</em>. If \a __x or 382382\a __y or both are NaN, NaN is returned. 383383*/ 384384extern double fdim (double __x, double __y) __ATTR_CONST__; 385 385#define fdimf fdim /**< The alias for fdim(). */ 386386 387387/** 388388The fma() function performs floating-point multiply-add. This is the 389389operation <em>(__x * __y) + __z</em>, but the intermediate result is 390390not rounded to the destination type. This can sometimes improve the 391391precision of a calculation. 392392*/ 393393extern double fma (double __x, double __y, double __z) __ATTR_CONST__; 394 394#define fmaf fma /**< The alias for fma(). */ 395395 396396/** 397397The fmax() function returns the greater of the two values \a __x and 398398\a __y. If an argument is NaN, the other argument is returned. If 399399both arguments are NaN, NaN is returned. 400400*/ 401401extern double fmax (double __x, double __y) __ATTR_CONST__; 402 402#define fmaxf fmax /**< The alias for fmax(). */ 403403 404404/** 405405The fmin() function returns the lesser of the two values \a __x and 406406\a __y. If an argument is NaN, the other argument is returned. If 407407both arguments are NaN, NaN is returned. 408408*/ 409409extern double fmin (double __x, double __y) __ATTR_CONST__; 410 410#define fminf fmin /**< The alias for fmin(). */ 411411 412412/** 413413The trunc() function rounds \a __x to the nearest integer not larger 414414in absolute value. 415415*/ 416416extern double trunc (double __x) __ATTR_CONST__; 417 417#define truncf trunc /**< The alias for trunc(). */ 418418 419419/** 420420The round() function rounds \a __x to the nearest integer, but rounds 421421halfway cases away from zero (instead of to the nearest even integer). 422422Overflow is impossible. 423423 424424\return The rounded value. If \a __x is an integral or infinite, \a 425425__x itself is returned. If \a __x is \c NaN, then \c NaN is returned. 426426*/ 427427extern double round (double __x) __ATTR_CONST__; 428 428#define roundf round /**< The alias for round(). */ 429429 430430/** 431431The lround() function rounds \a __x to the nearest integer, but rounds 432432halfway cases away from zero (instead of to the nearest even integer). 433433This function is similar to round() function, but it differs in type of 434434return value and in that an overflow is possible. 435435 436436\return The rounded long integer value. If \a __x is not a finite number 437437or an overflow was, this realization returns the \c LONG_MIN value 438438(0x80000000). 439439*/ 440440extern long lround (double __x) __ATTR_CONST__; 441 441#define lroundf lround /**< The alias for lround(). */ 442442 443443/** 444444The lrint() function rounds \a __x to the nearest integer, rounding the 445445halfway cases to the even integer direction. (That is both 1.5 and 2.5 446446values are rounded to 2). This function is similar to rint() function, 447447but it differs in type of return value and in that an overflow is 448448possible. 449449 450450\return The rounded long integer value. If \a __x is not a finite 451451number or an overflow was, this realization returns the \c LONG_MIN 452452value (0x80000000). 453453*/ 454454extern long lrint (double __x) __ATTR_CONST__; 455 455#define lrintf lrint /**< The alias for lrint(). */ 456456 457457#ifdef __cplusplus 458458} 459459#endif 460460 461461/*@}*/ 462462#endif /* !__MATH_H */

isinf int isinf(double __x)

cosh double cosh(double __x)

fabs double fabs(double __x)

ldexp double ldexp(double __x, int __exp)

signbit int signbit(double __x)

modff float modff(float __x, float *__iptr)

isfinite static int isfinite(double __x)

Definition: math.h:341

log double log(double __x)

cbrt double cbrt(double __x)

lround long lround(double __x)

fmod double fmod(double __x, double __y)

sin double sin(double __x)

fmin double fmin(double __x, double __y)

cos double cos(double __x)

sqrt double sqrt(double __x)

fdim double fdim(double __x, double __y)

square double square(double __x)

sinh double sinh(double __x)

hypot double hypot(double __x, double __y)

round double round(double __x)

tan double tan(double __x)

lrint long lrint(double __x)

tanh double tanh(double __x)

fmax double fmax(double __x, double __y)

acos double acos(double __x)

log10 double log10(double __x)

ceil double ceil(double __x)

exp double exp(double __x)

pow double pow(double __x, double __y)

frexp double frexp(double __x, int *__pexp)

sqrtf float sqrtf(float)

fma double fma(double __x, double __y, double __z)

copysign static double copysign(double __x, double __y)

Definition: math.h:359

asin double asin(double __x)

trunc double trunc(double __x)

isnan int isnan(double __x)

atan double atan(double __x)

modf double modf(double __x, double *__iptr)

floor double floor(double __x)

atan2 double atan2(double __y, double __x)