24h購物| | PChome| 登入
2013-07-22 19:21:28| 人氣945| 回應0 | 上一篇 | 下一篇

[UVA] 1530 - Floating Point Numbers

推薦 0 收藏 0 轉貼0 訂閱站台

Floating point numbers are represented at the hardware level of most computer systems in normalized binary scientific notation. We will consider a hypothetical computer in which floating point numbers are represented in a 16-bit word. An example is shown in Figure 1:

In the common Intel chips, the exponent of a float has 8 bits and the mantissa has 23 bits, otherwise the representation is the same as in Figure 1.

We will show that this particular example represents the number whose usual decimal representation is -10.375.

The leftmost bit (the sign bit) is 1 if the floating point number is negative, 0 otherwise.

The next seven bits (1000010 in this example) represent the Exponent component of the normalized binary scientific notation. The following two operations will reveal the exponent's actual value:

  • Evaluate the binary number 1000010: it is 1*64+0*32+0*16+0*8+0*4+1*2+0*1 = 66.
  • Subtract the bias correction 63 from 66, which yields 3 as the actual value of the exponent that we will use below (the purpose of the bias correction is to allow negative exponents. As long as the exponent has seven bits, which will always be the case in this program, the bias correction is 63 = 27-1 - 1).
  • The remaining 8 bits (01001100 in this example) constitute the mantissa, which is the fractional part of the normalized binary scientific notation. You will see in the next step how it is used.

    The sign bit, the mantissa, and the exponent are used to calculate the value of the floating point number:

  • The period preceding the mantissa is the "binary point" which has the same meaning as the decimal point in a decimal (base ten) representation.
  • In the normalized binary scientific notation there is only one binary digit preceding the binary point, and it is always 1 (except if the floating point number is the number zero). It is called the characteristic. Since it is always 1, it does not explicitly appear in the 16-bit representation of Figure 1.
  • Since the number 1.01001100 is in binary, the digits to the right of the binary point represent successive negative powers of 2. Therefore, the number shown in Figure 2 can be re-written as:

    Here the result has been expressed using scientific notation (base ten). Most programming languages would output this result as -1.0375e+001. The use of a lower case "e" or upper case "E", and the number of leading zeros in the exponent can vary from one programming language to another.

    An important exception: if all bits except the sign bit are zero, then the floating point number is zero, regardless of the sign bit.

    Input 

    The input contains an undetermined number of lines. Each line contains, starting in column 1, exactly 16 printable characters which can be any permutation of 0s and 1s. The first character represents the sign bit, the next seven characters, the exponent, and the last eight characters, the mantissa of a floating point number in normalized binary scientific notation according to the conventions discussed on the preceding page.

    Output 

    Each floating point number read from the input will be written by your program to the output file, one number per line.

    The following formatting conventions are required in your output:

  • The floating point numbers will be displayed in scientific notation (base ten).
  • Column 1 will either be blank (indicating a positive number or zero) or contain a minus sign (indicating a negative number).
  • Column 2 will contain a digit.
  • Column 3 will contain the decimal point.
  • Columns 4 - 9 will contain six additional significant digits of the floating point number.
  • The remaining columns will specify the exponent of scientific notation. The sign and magnitude of the exponent must of course be correct, but the exact format in which the exponent is displayed will be determined by the programming language you use.
  • Sample Input 

    1100001001001100
    0011111100000000
    1011111110000000
    0000000010101010
    0011011111100000
    1001111011100000
    0101011001010101
    0100011011101101
    0111111111111111
    1100001000101100
    0000000000000000
    1000000000000000
    

    Sample Output 

    Program 6 by team X
    -1.037500e+001
     1.000000e+000
    -1.500000e+000
     1.804180e-019
     7.324219e-003
    -2.182787e-010
     1.117389e+007
     2.465000e+002
     3.682143e+019
    -9.375000e+000
     0.000000e+000
     0.000000e+000
    End of program 6 by team X

    遇見最詭的一道題目,一開始想說用 %e 自動輸出,但是沒過。

    後來改成 double 的手動格式處理,也沒有過。

    最後使用 float,居然過了。<(_ _)>




    #include <stdio.h>
    #include <math.h>
    int main() {
    puts("Program 6 by team X");
    char s[105];
    while(scanf("%s", s) == 1) {
    int zero = 1, i, j;
    for(i = 1; i < 16; i++)
    zero &= s[i] == '0';
    if(zero) {
    puts(" 0.000000e+000");
    continue;
    }
    long long e = 0, f = 0;
    for(i = 1; i <= 7; i++)
    e = (e<<1) + s[i]-'0';
    e = e-63;
    for(i = 8; i < 16; i++)
    f = (f<<1)|(s[i]-'0');
    double ret = (1+f/256.0)*pow(2, e);
    e = 0;
    if(ret > 10-1e-6) {
    while(ret > 10-1e-6) {
    e++;
    ret /= 10;
    }
    } else if(ret < 1 + 1e-6) {
    while(ret < 1+1e-6) {
    e--;
    ret *= 10;
    }
    }
    if(s[0] == '1') printf("-");
    else printf(" ");
    printf("%.6fe", ret);
    if(e >= 0) printf("+%03d\n", e);
    else printf("-%03d\n", -e);
    }
    puts("End of program 6 by team X");
    return 0;
    }
     

    台長: Morris
    人氣(945) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
    全站分類: 不分類 | 個人分類: UVA |
    此分類下一篇:[UVA] 196 - Spreadsheet
    此分類上一篇:[UVA][dp] 11566 - Let's Yum Cha!

    是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
    * 請輸入識別碼:
    請輸入圖片中算式的結果(可能為0) 
    (有*為必填)
    TOP
    詳全文