PS-Trainer C - Entwicklung
Mathematik: Absolutwerte, Runden und Abschneiden
Homepage von PS-Trainer - C-Entwicklung - Mathematik - an PS-Trainer
PS-Trainer PS-Trainer

Math and related Absolute Values, Rounding, Truncation:
abs Calculate the absolute value of an integer argument.
cabs Calculates the absolute value of a complex number.
fabs Calculate the absolute value of a real argument.
labs Calculate the absolute value of a long integer.
floor Calculate the floor of a value.
ceil Calculate the ceiling of a value.
modf

Split a real value into fractional and integer parts.




abs
Calculates the absolute value.
int abs( int n );

Routine Required Header Compatibility
abs <stdlib.h> or <math.h> ANSI, Win 95, Win NT

Subject: Data Conversion Routines, Floating-Point Support
Keywords: See Also _cabs, fabs, labs
Return Value
The abs function returns the absolute value of its parameter. There is no error return.

Parameter
n Integer value

Example
/* ABS.C: This program computes and displays
* the absolute values of several numbers.
*/

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void main( void )
{
int ix = -4, iy;
long lx = -41567L, ly;
double dx = -3.141593, dy;

iy = abs( ix );
printf( "The absolute value of %d is %d\n", ix, iy);

ly = labs( lx );
printf( "The absolute value of %ld is %ld\n", lx, ly);

dy = fabs( dx );
printf( "The absolute value of %f is %f\n", dx, dy );
}


Output
The absolute value of -4 is 4
The absolute value of -41567 is 41567
The absolute value of -3.141593 is 3.141593


_cabs
Calculates the absolute value of a complex number.
double _cabs( struct _complex z );

Routine Required Header Compatibility
_cabs  <math.h> Win 95, Win NT

Remarks
The _cabs function calculates the absolute value of a complex number, which must be a structure of type _complex. The structure z is composed of a real component x and an imaginary component y. A call to _cabs produces a value equivalent to that of the expression sqrt( z.x*z.x + z.y*z.y ).

Subject: Floating-Point Support Routines
Keywords: See Also abs, fabs, labs
Return Value
_cabs returns the absolute value of its argument if successful. On overflow _cabs returns HUGE_VAL and sets errno to ERANGE. You can change error handling with _matherr.

Parameter
z Complex number
Example
/* CABS.C: Using _cabs, this program calculates
* the absolute value of a complex number.
*/
#include <math.h>
#include <stdio.h>

void main( void )
{
struct _complex number = { 3.0, 4.0 };
double d;

d = _cabs( number );
printf( "The absolute value of %f + %fi is %f\n",
number.x, number.y, d );
}


Output
The absolute value of 3.000000 + 4.000000i is 5.000000



fabs
Calculates the absolute value of the floating-point argument.
double fabs( double x );

Function Required Header Compatibility
fabs  <math.h> ANSI, Win 95, Win NT

Subject: Floating-Point Support Routines
Keywords: See Also abs, _cabs, labs

Return Value
fabs returns the absolute value of its argument. There is no error return.

Parameter
x Floating-point value

Example
/* ABS.C: This program computes and displays
* the absolute values of several numbers.
*/

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void main( void )
{
int ix = -4, iy;
long lx = -41567L, ly;
double dx = -3.141593, dy;

iy = abs( ix );
printf( "The absolute value of %d is %d\n", ix, iy);

ly = labs( lx );
printf( "The absolute value of %ld is %ld\n", lx, ly);

dy = fabs( dx );
printf( "The absolute value of %f is %f\n", dx, dy );
}


Output
The absolute value of -4 is 4
The absolute value of -41567 is 41567
The absolute value of -3.141593 is 3.141593


labs
Calculates the absolute value of a long integer.
long labs( long n );

Routine Required Header Compatibility
labs  <stdlib.h> and <math.h> ANSI, Win 95, Win NT

Subject: Data Conversion Routines, Floating-Point Support
Keywords: See Also abs, _cabs, fabs
Return Value
The labs function returns the absolute value of its argument. There is no error return.

Parameter
n Long-integer value
Example
/* ABS.C: This program computes and displays
* the absolute values of several numbers.
*/

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void main( void )
{
int ix = -4, iy;
long lx = -41567L, ly;
double dx = -3.141593, dy;

iy = abs( ix );
printf( "The absolute value of %d is %d\n", ix, iy);

ly = labs( lx );
printf( "The absolute value of %ld is %ld\n", lx, ly);

dy = fabs( dx );
printf( "The absolute value of %f is %f\n", dx, dy );
}

Output
The absolute value of -4 is 4
The absolute value of -41567 is 41567
The absolute value of -3.141593 is 3.141593


ceil
Calculates the ceiling of a value.
double ceil( double x );

Routine Required Header Compatibility
ceil  <math.h> ANSI, Win 95, Win NT

Subject: Floating-Point Support Routines
Keywords: See Also floor, fmod
Return Value
The ceil function returns a double value representing the smallest integer that is greater than or equal to x. There is no error return.

Parameter
x Floating-point value
Example
/* FLOOR.C: This example displays the largest integers
* less than or equal to the floating-point values 2.8
* and -2.8. It then shows the smallest integers greater
* than or equal to 2.8 and -2.8.
*/

#include <math.h>
#include <stdio.h>

void main( void )
{
double y;

y = floor( 2.8 );
printf( "The floor of 2.8 is %f\n", y );
y = floor( -2.8 );
printf( "The floor of -2.8 is %f\n", y );

y = ceil( 2.8 );
printf( "The ceil of 2.8 is %f\n", y );
y = ceil( -2.8 );
printf( "The ceil of -2.8 is %f\n", y );
}

Output
The floor of 2.8 is 2.000000
The floor of -2.8 is -3.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000


floor
Calculates the floor of a value.
double floor( double x );

Routine Required Header Compatibility
floor  <math.h> ANSI, Win 95, Win NT

Subject: Floating-Point Support Routines
Keywords: See Also ceil, fmod
Return Value
The floor function returns a floating-point value representing the largest integer that is less than or equal to x. There is no error return.

Parameter
x Floating-point value
Example
/* FLOOR.C: This example displays the largest integers
* less than or equal to the floating-point values 2.8
* and -2.8. It then shows the smallest integers greater
* than or equal to 2.8 and -2.8.
*/

#include <math.h>
#include <stdio.h>

void main( void )
{
double y;

y = floor( 2.8 );
printf( "The floor of 2.8 is %f\n", y );
y = floor( -2.8 );
printf( "The floor of -2.8 is %f\n", y );

y = ceil( 2.8 );
printf( "The ceil of 2.8 is %f\n", y );
y = ceil( -2.8 );
printf( "The ceil of -2.8 is %f\n", y );
}

Output
The floor of 2.8 is 2.000000
The floor of -2.8 is -3.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000


modf
Splits a floating-point value into fractional and integer parts.
double modf( double x, double *intptr );

Routine Required Header Compatibility
modf  <math.h> ANSI, Win 95, Win NT

Remarks
The modf function breaks down the floating-point value x into fractional and integer parts, each of which has the same sign as x. The signed fractional portion of x is returned. The integer portion is stored as a floating-point value at intptr.

Subject: Floating-Point Support Routines, Long Double
Keywords: See Also frexp, ldexp
Return Value
This function returns the signed fractional portion of x. There is no error return.

Parameters
x Floating-point value
intptr Pointer to stored integer portion

Example
/* MODF.C */

#include <math.h>
#include <stdio.h>

void main( void )
{
double x, y, n;

x = -14.87654321; /* Divide x into its fractional */
y = modf( x, &n ); /* and integer parts */

printf( "For %f, the fraction is %f and the integer is %.f\n",
x, y, n );
}


Output
For -14.876543, the fraction is -0.876543 and the integer is -14


Homepage von PS-Trainer - C-Entwicklung - Mathematik - an PS-Trainer

Aktuelle Daten dieser Seite Letzte Änderung:
  Geocities

 

 

 

Hosted by www.Geocities.ws

1