Double
Double
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
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 next table allows us to print the data with exact number of digits:
Type Format
int %<n>d
double %<n>.<m>lf
double d = 1.123456789;
int main()
{
printf("%5.3lf\n",d);
return 0;
}
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>
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.
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);
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:
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:
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;
}
#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.