Czech version
logolink

< Back to the list of lessons

Mathematical Formulas and Functions

AlgortimyContent of the lesson:

  • Mathematical Functions - Powers and Roots
  • Decimal Numbers and Real Data Type
  • Writing Decimal Numbers to Console
  • Pi Number

Mathematical Functions - Powers and Roots

To let our programs solve more difficult problems you have to use more functions like powers and roots. We are going to show these function inside the programming language C.

Note: To be able to use these functions you have to add the header file (also called library) math.h. A header file is a file which contains declaration of additional functions (for example mathematical) which can be used after inserting the library, otherwise our program would not be able to use these functions and it would report a problem.

ADDING THE HEADER FILE MATH.H
#include "stdafx.h";
#include <math.h>
int _tmain(int argc, _TCHAR* argv[])
{
	...
	return 0;
}

Power

To compute powers you can use the pow function (shortcut from power). You can see the following text in the reference manual of C language:

Information about power in reference manual
double pow (double base, double exponent );
long double pow ( long double base, long double exponent );
float pow (float base, float exponent );
double pow (double base, int exponent );
long double pow (long double base, int exponent ); 

Raise to power.
Returns base raised to the power exponent.

The definition tells that the function can be used by the pow command which has 2 parameters:

  • base - the base number
  • exponent - the exponent number

The reference manual offers more variants of notation - these variants show which data type can be used for the input values and the data type of the result.

In case you want your program to compute and write the result of the expression a5 you have to write printf("%f", pow(a,5));

Square Root

To compute the square root function you can use the sqrt function (shortcut for square root).  Using the reference manual you can see this:

Information about Sqrt function from the reference manual
double sqrt (double x );
float sqrt (float x );
long double sqrt (long double x );

Compute square root.
Returns the square root of x.

Decimal Numbers and Data Types float, double and long double

The power and sqrt function use the real numbers so you have to use another types of variables when working with these functions. These data types are float, double and long double. The list of these variables was mentioned in the previous lesson but you can see it once more for revision:

Data type

Allowed shortcut

Description

Range

Integer data types with sign

signed int

int

Integer number with sign

from - 2 147 483 648
to + 2 147 483 647

signed short int

short int or short

Short integer number with sign

from - 32 768
to + 32 767

signed long int

long int or long

Long integer number with sign

from - 2 147 483 648
to + 2 147 483 647

signed char

char

Char with sign

from - 128
to + 127

Integer data types without sign

signed int

 

Integer number without sign

from 0
to + 4 294 967 295

signed short int

signed short

Short integer number without sign

from 0
to + 65 536

signed long int

signed long

Long integer number without sign

from 0
to + 4 294 967 295

signed char

 

Char without sign

from 0
to + 256

Real data types

float

 

Real number with single accuracy

from 3.4×10-38
to 3.4×1038

double

 

Real number with double accuracy

from 1.7×10-308
to 1.7×10308

long double

 

Real number with extended accuracy

from 3.4×10-38
to 3.4×1038

 

Data type float and Pow function in C language
#include "stdafx.h"
#include <math.h>

int _tmain(int argc, _TCHAR* argv[])
{
	float a, b, e;
	printf("Zadejte prosim zaklad mocniny: ");
	scanf("%f",&a);
	printf("Zadejte prosim exponent mocniny: ");
	scanf("%f",&e);
	b = pow(a,e);
	printf("Vyraz %f^%f = %f\n", a, e, b);
	getchar();
	return 0;
}

If you launch the program you can see that all results are displayed with 6 decimal places which can be unpractical so we have to set the number of decimal places. Adjust the program in the following way:

Data type real and function Power in C language
#include "stdafx.h"
#include <math.h>

int _tmain(int argc, _TCHAR* argv[])
{
	float a, b, e;
	printf("Zadejte prosim zaklad mocniny: ");
	scanf("%f",&a);
	printf("Zadejte prosim exponent mocniny: ");
	scanf("%f",&e);
	b = pow(a,e);
	printf("Vyraz %5.2f^%5.2f = %5.2f\n", a, e, b);
    fflush(stdin);
	getchar();
	return 0;
}

If you do not want to specify the minimal number of characters but only the number of decimal places, ou can use the notation %5.2f with syntax %.2f.

Pi Number

In case you need the value of PI in C language, you can define it as a constant. To define a constant you have to use the directive #define which has to be written at the beginning of the program similarly as the directives #include (which were used in the previous part to insert a header file).

To define a constant use the following syntax:

Definition of a constant
#define JMENO HODNOTA

You can see two things in the previous syntax: as for the directive #include the line is not ended by a semicolon and the data type is not written (the syntax is written in the form "#define space name of constant space value).

The definition of the pi constant is shown in the following example (note: the value can be changed according to required accuracy):

Definition of the PI constant
#define PI 3.14159

Individual Task I

Write a program to compute the capacity of any cylinder and any sphere (user should be able to set values). Find required formulas in the Internet.

Individual Task II

Write a program to compute this formula: ax+by. User should be able to set all values.

Additional Texts

Links

Questions

  1. What is the name of function to compute powers in C?
  2. What is the name of function to compute the square root in C?
  3. What is the name of function to compute roots in C?
  4. How can you use the data type float?
  5. How can you clearly write a real number to console?
webdesign, xhtml, css, php - Mgr. Michal Mikláš