Floating – point type
The following table provides the details of standard floating-point types with
storage sizes and value ranges and their precision:
Type Storage size Value range Precision
float 4 bytes [1.2*10-38.. 4.3*1038] 6 decimal places
double 8 bytes [2.3*10-308.. 1.7*10308] 15 decimal places
It’s preferable to use double type because nowadays computers have math
coprocessor. All operations with floating-point type are executed in math coprocessor.
#include <stdio.h>
double x = 1.234567890123456789;
double y = 14.0 / 3;
int main()
{
// by default %lf format prints 6 decimal digits
printf("%lf\n",x);
printf("%lf\n",y);
return 0;
}
The result of 14 / 3 is 4, because we have here integer division.
The result of 14.0 / 3 is 4.666667, because double / int is converted to double /
double, so the result is double.
The next table allows us to print the data with exact number of digits:
Type Format
int %<n>d
double %<n>.<m>lf
<n> means that the number will be printed in n positions.
<m> means that m digits will be printed after the decimal point.
If <n> is greater than the number of printed digits, spaces are added at the
beginning of the number. Remember, that decimal point ‘.’ takes one position for
printing.
If <n> is less than the number of printed digits, the number is printed with the
same number of digits as it contains.
#include <stdio.h>
int x = 2, y = 123, z = 5678;
int main()
{
// each integer is printed in 5 positions
printf("%5d %5d %5d\n",x,y,z);
return 0;
}
Variable Output format Output value
int a = 456 %1d 456
int a = 456 %3d 456
int a = 456 %5d 456
double f = 34.1223 %3.2f 34.12
double f = 34.1223 %7.3f 34.122
double f = 34.1223 %8.4f 34.1223
double f = 34.1223 %9.4f 34.1223
double d = 1.123456789 %5.3lf 1.123
double d = 1.123456789 %7.5lf 1.12346
double d = 1.123456789 %12.9lf 1.123456789
double d = 1.123456789 %15.11lf 1.12345678900
#include <stdio.h>
double d = 1.123456789;
int main()
{
printf("%5.3lf\n",d);
return 0;
}
If <m> = 0, the value is rounded to the nearest integer.
#include <stdio.h>
double x = 45.5678, y = 23.49654;
int main()
{
printf("%0.0lf %0.0lf\n",x,y);
return 0;
}
If you want to print the number without spaces at the beginning, and with <m>
digits after the decimal point, you can use format “%0.<m>lf” or “%.<m>lf”.
#include <stdio.h>
double x = 45.5678, y = 23.49654;
int main()
{
printf("%.2lf %.3lf\n",x,y);
return 0;
}
E-OLYMP 8604. Sum and product of real numbers Three real numbers are
given. Find their sum and product.
► Read three real numbers.
scanf("%lf %lf %lf", &a, &b, &c);
Compute and print the sum and product of three numbers.
s = a + b + c;
p = a * b * c;
printf("%.4lf %.4lf\n", s, p);
E-OLYMP 8605. Sum of real numbers Three real numbers x, y, z are given. Print
in one line the sums x + y, x + z and y + z with 4 decimal digits.
► Use double type.
E-OLYMP 8606. Sum of some real numbers Four real numbers are given. Find
the sum of first two, of first three and of all four numbers.
► Use double type.
E-OLYMP 8825. Value of variable 1 Find the value of the variable y for a given
real value of the variable x.
5x2 3
yx 3
9x 1
7 x
► Read the value of the variable x.
scanf("%lf", &x);
Compute the value of the variable y.
y = x * x * x - 5 * x * x / 7 + 9 * x - 3 / x + 1;
Print the result up to thousandths.
printf("%.3lf\n", y);
E-OLYMP 8831. Value of expression 1 Find the value of expression for the
given values of the variables x and y.
x y
2 x 2 4 xy 3 y 2
7
► To solve the problem, compute the value of the given expression.
Math functions
To work with algebraic and trigonometric functions, include the library <math.h>
to the program:
#include <math.h>
Algebraic functions:
function function call
square root, x sqrt(x)
power, xn pow(x,n)
exponent, ex exp(x)
natural logarithm, ln x log(x)
decimal logarithm, lg x log10(x)
absolute value of int, |x| abs(x)
absolute value of double, |x| fabs(x)
logс b ln b
loga b = =
logс a ln a
#include <stdio.h>
#include <math.h>
int main()
{
printf("%lf\n",sqrt(2)); // 1.4142
printf("%lf\n", pow(2, 10)); // 1024.000
printf("%lf\n", log(32) / log(2)); // log2 (32) = 5, 2 ^ 5 = 32
printf("%d %lf\n", abs(-34), fabs(-34.67));
return 0;
}
Trigonometric functions:
Function function call
sinus, sin(x) sin(x)
cosinus, cos(x) cos(x)
tangent, tg(x) tan(x)
arcsinus, arcsin(x) asin(x)
arccosinus, arccos(x) acos(x)
arctangent, arctg(x) atan(x)
Constant π is not defined in the library <math.h>. It can be obtained by calculating
arccos (-1):
const double PI = acos(-1.0);
Rounding functions:
function function call
ceiling, x ceil(x)
floor, x floor(x)
#include <stdio.h>
#include <math.h>
double x;
int main()
{
x = 2.678;
printf("%lf %lf\n",floor(x), ceil(x)); // 2.000 3.000
return 0;
}
To compute the powers of numbers, it is sometimes convenient to use the relation
xn = e ln x = e n ln x
n
Hence, for example, it follows that
1
1
x = x = e ln x = e ln x / n
n n
n
#include <stdio.h>
#include <math.h>
double x, y;
int main()
{
x = 81;
y = pow(x, 1.0 / 4); // x ^ (1/4)
printf("%lf\n",y);
y = exp(log(x) / 4); // e ^ (ln x / 4)
printf("%lf\n", y);
return 0;
}
E-OLYMP 8876. Integer One real number n is given. Print Ok, if n is integer and
No otherwise.
► Number n is an integer if its integer part equals to itself. That is, if the equality
floor(n) = n takes place.
E-OLYMP 8877. Perfect square Positive integer n is given. If n is a perfect
square of some positive integer m, then print m. Otherwise print No.
► Let a = n . If a * a = n, then number n is a perfect square.