Jump to content
Wikibooks The Free Textbook Project

C++ Programming/Code/Standard C Library/Math

From Wikibooks, open books for an open world

Standard C Math

[edit | edit source ]

This section will cover the Math elements of the C Standard Library.

Syntax
#include<cstdlib>
intabs(intnum);

The abs() function returns the absolute value of num. For example:

intmagic_number=10;
cout<<"Enter a guess: ";
cin>>x;
cout<<"Your guess was "<<abs(magic_number-x)<<" away from the magic number."<<endl;
Related topics
fabs - labs

acos

[edit | edit source ]
Syntax
#include<cmath>
doubleacos(doublearg);

The acos() function returns the arc cosine of arg, which will be in the range [0, pi]. arg should be between -1 and 1. If arg is outside this range, acos() returns NAN and raises a floating-point exception.

Related topics
asin - atan - atan2 - cos - cosh - sin - sinh - tan - tanh

asin

[edit | edit source ]
Syntax
#include<cmath>
doubleasin(doublearg);

The asin() function returns the arc sine of arg, which will be in the range [-pi/2, +pi/2]. arg should be between -1 and 1. If arg is outside this range, asin() returns NAN and raises a floating-point exception.

Related topics
acos - atan - atan2 - cos - cosh - sin - sinh - tan - tanh

atan

[edit | edit source ]
Syntax
#include<cmath>
doubleatan(doublearg);

The function atan() returns the arc tangent of arg, which will be in the range [-pi/2, +pi/2].

Related topics
acos - asin - atan2 - cos - cosh - sin - sinh - tan - tanh

atan2

[edit | edit source ]
Syntax
#include<cmath>
doubleatan2(doubley,doublex);

The atan2() function computes the arc tangent of y/x, using the signs of the arguments to compute the quadrant of the return value.

Related topics
acos - asin - atan - cos - cosh - sin - sinh - tan - tanh

ceil

[edit | edit source ]
Syntax
#include<cmath>
doubleceil(doublenum);

The ceil() function returns the smallest integer no less than num. For example:

y=6.04;
x=ceil(y);

would set x to 7.0.

Related topics
floor - fmod
Syntax
#include<cmath>
floatcos(floatarg);
doublecos(doublearg);
longdoublecos(longdoublearg);

The cos() function returns the cosine of arg, where arg is expressed in radians. The return value of cos() is in the range [-1,1]. If arg is infinite, cos() will return NAN and raise a floating-point exception.

Related topics
acos - asin - atan - atan2 - cosh - sin - sinh - tan - tanh

cosh

[edit | edit source ]
Syntax
#include<cmath>
floatcosh(floatarg);
doublecosh(doublearg);
longdoublecosh(longdoublearg);

The function cosh() returns the hyperbolic cosine of arg.

Related topics
acos - asin - atan - atan2 - cos - sin - sinh - tan - tanh
Syntax
#include<cstdlib>
div_tdiv(intnumerator,intdenominator);

The function div() returns the quotient and remainder of the operation numerator / denominator. The div_t structure is defined in cstdlib, and has at least:

intquot;// The quotient
intrem;// The remainder

For example, the following code displays the quotient and remainder of x/y:

div_ttemp;
temp=div(x,y);
printf("%d divided by %d yields %d with a remainder of %d\n",
x,y,temp.quot,temp.rem);
Related topics
ldiv
Syntax
#attrid <cmath>
doubleexp(doublearg);

The exp() function returns e (2.7182818) raised to the argth power.

Related topics
log - pow - sqrt

fabs

[edit | edit source ]
Syntax
#include<cmath>
doublefabs(doublearg);

The function fabs() returns the absolute value of arg.

Related topics
abs - fmod - labs

floor

[edit | edit source ]
Syntax
#include<cmath>
doublefloor(doublearg);

The function floor() returns the largest integer value not greater than arg.

// Example for positive numbers
y=6.04;
x=floor(y);

would result in x being set to 6 (double 6.0).

// Example for negative numbers
y=-6.04;
x=floor(y);

would result in x being set to -7 (double -7.0).

Related topics
ceil - fmod

fmod

[edit | edit source ]
Syntax
#include<cmath>
doublefmod(doublex,doubley);

The fmod() function returns the remainder of x/y.

Related topics
ceil - fabs - floor

frexp

[edit | edit source ]
Syntax
#include<cmath>
doublefrexp(doublenum,int*exp);

The function frexp() is used to decompose num into two parts: a mantissa between 0.5 and 1 (returned by the function) and an exponent returned as exp. Scientific notation works like this:

num=mantissa*(2^exp)
Related topics
ldexp - modf

labs

[edit | edit source ]
Syntax
#include<cstdlib>
longlabs(longnum);

The function labs() returns the absolute value of num.

Related topics
abs - fabs

ldexp

[edit | edit source ]
Syntax
#include<cmath>
doubleldexp(doublenum,intexp);

The ldexp() function returns num * (2 ^ exp). And get this: if an overflow occurs, HUGE_VAL is returned.

Related topics
frexp - modf

ldiv

[edit | edit source ]
Syntax
#include<cstdlib>
ldiv_tldiv(longnumerator,longdenominator);

Testing: adiv_t, div_t, ldiv_t.

The ldiv() function returns the quotient and remainder of the operation numerator / denominator. The ldiv_t structure is defined in cstdlib and has at least:

longquot;// the quotient
longrem;// the remainder
Related topics
div
Syntax
#include<cmath>
doublelog(doublenum);

The function log() returns the natural (base e) logarithm of num. There's a domain error if num is negative, a range error if num is zero.

In order to calculate the logarithm of x to an arbitrary base b, you can use:

doubleanswer=log(x)/log(b);
Related topics
exp - log10 - pow - sqrt

log10

[edit | edit source ]
Syntax
#include<cmath>
doublelog10(doublenum);

The log10() function returns the base 10 (or common) logarithm for num. There will be a domain error if num is negative and a range error if num is zero.

Related topics
log

modf

[edit | edit source ]
Syntax
#include<cmath>
doublemodf(doublenum,double*i);

The function modf() splits num into its integer and fraction parts. It returns the fractional part and loads the integer part into i.

Related topics
frexp - ldexp
Syntax
#include<cmath>
doublepow(doublebase,doubleexp);

The pow() function returns base raised to the expth power. There's a domain error if base is zero and exp is less than or equal to zero. There's also a domain error if base is negative and exp is not an integer. There's a range error if an overflow occurs.

Related topics
exp - log - sqrt
Syntax
#include<cmath>
doublesin(doublearg);

If you don't want cmath you can write sin function it is;

  1. include <iostream>

using namespace std;

double sin(double x) //sin function {return x-((x*x*x)/6.)+((x*x*x*x*x)/120.);}

int main () {

 double a;
 cin>>a;
 
 cout<<"sin("<<a<<")="<<sin(a*(3.14159/180.))<<endl;

return 0;}


The function sin() returns the sine of arg, where arg is given in radians. The return value of sin() will be in the range [-1,1]. If arg is infinite, sin() will return NAN and raise a floating-point exception.

Related topics
acos - asin - atan - atan2 - cos - cosh - sinh - tan - tanh

sinh

[edit | edit source ]
Syntax
#include<cmath>
doublesinh(doublearg);

The function sinh() returns the hyperbolic sine of arg.

Related topics
acos - asin - atan - atan2 - cos - cosh - sin - tan - tanh

sqrt

[edit | edit source ]
Syntax
#include<cmath>
doublesqrt(doublenum);

The sqrt() function returns the square root of num. If num is negative, a domain error occurs.

Related topics
exp - log - pow
Syntax
#include<cmath>
doubletan(doublearg);

The tan() function returns the tangent of arg, where arg is given in radians. If arg is infinite, tan() will return NAN and raise a floating-point exception.

Related topics
acos - asin - atan - atan2 - cos - cosh - sin - sinh - tanh

tanh

[edit | edit source ]
Syntax
#include<cmath>
doubletanh(doublearg);


/*example*/
#include<stdio.h>
#include<math.h>
intmain(){
doublec,p;
c=log(2.0);
p=tanh(c);
printf("The hyperbolic tangent of %lf is %lf.\n",c,p);
return0;
}

The function tanh() returns the hyperbolic tangent of arg.

Related topics
acos - asin - atan - atan2 - cos - cosh - sin - sinh - tan

AltStyle によって変換されたページ (->オリジナル) /