C Tutorial/Data Type/float number

Материал из C\C++ эксперт
Версия от 10:32, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

C provides two main floating-point representations: float (single precision) and double (double precision).

A floating-point number has a fractional part and a biased exponent.

Float occupies 4 bytes and double occupies 8 bytes.

2.17.float number 2.17.1. C provides two main floating-point representations: float (single precision) and double (double precision). 2.17.2. <A href="/Tutorial/C/0040__Data-Type/Formatsforrepresentingfloatingpoints.htm">Formats for representing floating points</a> 2.17.3. <A href="/Tutorial/C/0040__Data-Type/Usetheformatfforprintingfloatingnumbers.htm">Use the format %f for printing floating numbers</a> 2.17.4. <A href="/Tutorial/C/0040__Data-Type/Forprintingdoubleyoucanuself.htm">For printing double you can use %lf.</a>

Formats for representing floating points

#include <stdio.h>
main() {
    float f1 = 0.23456;
    float f2 = 2.3456E-1;
    float f3 = .23456;
    float f4 = .23456e-2;
    float f5 = 2.3456E-4;
    float f6 = -.232456E-4;
    float f7 = 2345.6;
    float f8 = 23.456E2;
    float f9 = 23456e3;
    printf("%e \n", f1);
    printf("%e \n", f2);
    printf("%e \n", f3);
    printf("%e \n", f4);
    printf("%e \n", f5);
    printf("%e \n", f6);
    printf("%e \n", f7);
    printf("%e \n", f8);
    printf("%e \n", f9);
}
2.345600e-01
      2.345600e-01
      2.345600e-01
      2.345600e-03
      2.345600e-04
      -2.324560e-05
      2.345600e+03
      2.345600e+03
      2.345600e+07

For printing double you can use %lf.

#include <stdio.h>
main()
{
    double f1 = 123456789123456789.23456;
    printf("%lf \n", f1);
}
123456789123456784.000000

Use the format %f for printing floating numbers

#include <stdio.h>
main()
{
    float f1 = 0.23456;
    printf("%f \n", f1);
}