C 浮點(diǎn)類型

2018-05-20 14:55 更新

學(xué)習(xí)C - C浮點(diǎn)類型

浮點(diǎn)數(shù)保存用小數(shù)點(diǎn)寫入的值。

以下是浮點(diǎn)值的示例:

1.6    0.00018    1234.123   100.0 

最后一個(gè)常數(shù)是整數(shù),但它將被存儲為浮點(diǎn)值。

浮點(diǎn)數(shù)通常表示為十進(jìn)制值乘以10的冪,其中10的冪稱為指數(shù)。

下表顯示了如何顯示浮點(diǎn)數(shù)。

用C寫成
1.70.17E1
0.000090.9E-4
7655.1230.7655123E4
100.01.0E2

/* your weight in platinum */
#include <stdio.h>
int main(void)
{
    float weight;    /* user weight             */
    float value;     /* platinum equivalent     */
    
    printf("Please enter your weight in pounds: ");
    
    /* get input from the user                     */
    scanf("%f", &weight);
    value = 3.0 * weight;
    printf("Your weight is worth $%.2f.\n", value);
    return 0;
}

上面的代碼生成以下結(jié)果。


浮點(diǎn)變量

您可以選擇三種類型的浮點(diǎn)變量,如下表所示。

關(guān)鍵詞字節(jié)數(shù)值范圍
float4+/- 3.4E(+/- 38)
double8+/-1.7E(+/-308)
long double12+/- 1.19E(+/- 4932)

以下代碼定義了兩個(gè)浮點(diǎn)數(shù)。

float radius; 
double biggest; 

要寫一個(gè)float類型的常量,你需要附加一個(gè)f到數(shù)字,以便將它與double類型區(qū)分開來。

當(dāng)你這樣聲明它們時(shí),你可以初始化前兩個(gè)變量:

float radius = 2.5f; 
double biggest = 123E30; 

可變半徑的初始值為2.5,變量最大值被初始化為對應(yīng)于123的數(shù)字,接著是30個(gè)零。

要指定long double常數(shù),請附加大寫或小寫字母L.

long double huge = 1234567.98765L;

以下代碼顯示了如何浮動類型變量聲明和初始化。


#include <stdio.h> 
main() 
{ 
   //variable declarations 
   float y; 

   //variable initializations 
   y = 554.21; 
   //printing variable contents to standard output 
   printf("\nThe value of float variable y is %f", y); 
} 

上面的代碼生成以下結(jié)果。



使用浮點(diǎn)值進(jìn)行分割

這是一個(gè)簡單的例子,它將一個(gè)浮點(diǎn)值除以另一個(gè),并輸出結(jié)果:


    #include <stdio.h> 

    int main(void) 
    { 
      float length = 10.0f;               
      float part_count = 4.0f;                 // Number of equal pieces 
      float part_length = 0.0f;                
      part_length = length/part_count; 
      printf("%f can be divided into %f pieces and each part is f .\n", length, part_count, part_length); 
      return 0; 
    } 

上面的代碼生成以下結(jié)果。

小數(shù)點(diǎn)數(shù)

您可以在格式說明符中指定要在小數(shù)點(diǎn)后查看的地點(diǎn)數(shù)。

要將輸出獲得兩位小數(shù),您可以將格式說明符寫為%.2f 。

要獲取三位小數(shù),您可以寫入%.3f

以下代碼更改printf()語句,以便它將產(chǎn)生更合適的輸出:


    #include <stdio.h> 

    int main(void) 
    { 
      float length = 10.0f;               
      float part_count = 4.0f;                 // Number of equal pieces 
      float part_length = 0.0f;                
      part_length = length/part_count; 
  
      printf("A plank %.2f feet long can be cut into %.0f pieces %.2f feet long.\n", 
                                       length, part_count, part_length); 
      return 0; 
    } 

上面的代碼生成以下結(jié)果。

輸出字段寬度

您可以指定字段寬度和小數(shù)位數(shù)。

浮點(diǎn)值格式說明符的一般形式可以這樣寫:

%[width][.precision][modifier]f

方括號表示可選。

您可以省略寬度或精度或修飾符或這些的任何組合。

width值是一個(gè)整數(shù),指定輸出中包含空格的字符總數(shù)。它是輸出字段的寬度。

precision值是指定小數(shù)點(diǎn)后的小數(shù)位數(shù)的整數(shù)。

當(dāng)您輸出的值為long double時(shí),修飾符部分為L,否則省略。


    #include <stdio.h> 

    int main(void) 
    { 
      float length = 10.0f;               
      float part_count = 4.0f;                 // Number of equal pieces 
      float part_length = 0.0f;                
      part_length = length/part_count; 
  
      printf("A %8.2f plank foot can be cut into %5.0f pieces %6.2f feet long.\n", 
                                               length, part_count, part_length); 

      const double RENT = 3852.99;  // const-style constant 
   
      printf("*%f*\n", RENT); 
      printf("*%e*\n", RENT); 
      printf("*%4.2f*\n", RENT); 
      printf("*%3.1f*\n", RENT); 
      printf("*%10.3f*\n", RENT); 
      printf("*%10.3E*\n", RENT); 
      printf("*%+4.2f*\n", RENT); 
      printf("*%010.2f*\n", RENT); 
      return 0; 
    } 

上面的代碼生成以下結(jié)果。

精度

要使用浮點(diǎn)數(shù)創(chuàng)建精度,請使用%符號和f字符轉(zhuǎn)換說明符之間的編號方案調(diào)整轉(zhuǎn)換說明符。


    #include <stdio.h> 

    int main(void) 
    { 

        printf("%.1f", 3.123456); 
        printf("\n%.2f", 3.123456); 
        printf("\n%.3f", 3.123456); 
        printf("\n%.4f", 3.123456); 
        printf("\n%.5f", 3.123456); 
        printf("\n%.6f", 3.123456); 
      return 0; 
    } 

上面的代碼生成以下結(jié)果。

左對齊

指定字段寬度時(shí),默認(rèn)情況下,輸出值將對齊。

如果您希望該值在字段中保持對齊,則只需在%之后放置一個(gè)減號。


    #include <stdio.h> 

    int main(void) 
    { 
      float length = 10.0f;               
      float part_count = 4.0f;                 // Number of equal pieces 
      float part_length = 0.0f;                
      part_length = length/part_count; 
  
      printf("A %-18.2f plank foot can be cut into %5.0f pieces %6.2f feet long.\n", 
                                               length, part_count, part_length); 
      return 0; 
    } 

您可以在字段中指定字段寬度和對齊方式,并輸出整數(shù)值。

例如,%-15d指定一個(gè)整數(shù)值將被顯示為左對齊的字段寬度為15個(gè)字符。

上面的代碼生成以下結(jié)果。

例子

以下代碼顯示如何計(jì)算圓形表的圓周和面積。


#include <stdio.h> 

int main(void) 
{ 
  float radius = 0.0f;                  // The radius of the table 
  float diameter = 12.12f;              // The diameter of the table 
  float circumference = 0.0f;           // The circumference of the table 
  float area = 0.0f;                    // The area of the table 
  float Pi = 3.14159265f; 
  
  radius = diameter/2.0f;               // Calculate the radius 
  circumference = 2.0f*Pi*radius;       // Calculate the circumference 
  area = Pi*radius*radius;              // Calculate the area 
  
  printf("\nThe circumference is %.2f", circumference); 
  printf("\nThe area is %.2f\n", area); 
  return 0; 
} 

上面的代碼生成以下結(jié)果。

例2

以下代碼使用來自limit.h和float的定義的常量。


#include <stdio.h>
#include <limits.h>    // integer limits
#include <float.h>     // floating-point limits
int main(void)
{
    printf("Some number limits for this system:\n");
    printf("Biggest int: %d\n", INT_MAX);
    printf("Smallest long long: %lld\n", LLONG_MIN);
    printf("One byte = %d bits on this system.\n", CHAR_BIT);
    printf("Largest double: %e\n", DBL_MAX);
    printf("Smallest normal float: %e\n", FLT_MIN);
    printf("float precision = %d digits\n", FLT_DIG);
    printf("float epsilon = %e\n", FLT_EPSILON);
    
    return 0;
}

上面的代碼生成以下結(jié)果。

例3

以下代碼以兩種方式顯示浮點(diǎn)值。


#include <stdio.h> 
int main(void) { 
      float aboat = 32000.0; 
      double abet = 2.14e9; 
      long double dip = 5.32e-5; 

      printf("%f can be written %e\n", aboat, aboat); 
      // next line requires C99 or later compliance 
      printf("And it"s %a in hexadecimal, powers of 2 notation\n", aboat); 
      printf("%f can be written %e\n", abet, abet); 
      printf("%Lf can be written %Le\n", dip, dip); 
   
      return 0; 
}    

上面的代碼生成以下結(jié)果。

例4

讀取命令行的浮點(diǎn)型號。


             #include <stdio.h> 
             #include <string.h>      // for strlen() prototype 
             #define DENSITY 62.4     // human density in lbs per cu ft 
             int main() 
             {
                 float weight, volume; 
                 int size, letters; 
                 char name[40];        // name is an array of 40 chars 
              
                 printf("Hi! What"s your first name?\n"); 
                 scanf("%s", name); 
                 printf("%s, what"s your weight in pounds?\n", name); 
                 scanf("%f", &weight); 
                 size = sizeof name; 
                 letters = strlen(name); 
                 volume = weight / DENSITY; 
                 printf("%s, your volume is %2.2f cubic feet.\n", name, volume); 
                 printf("first name has %d letters,\n", letters); 
                 printf("we have %d bytes to store it.\n", size); 
              
                 return 0; 
             }    

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號