[UVA] 1530 - Floating Point Numbers@Morris' Blog|PChome Online 人新台
2013-07-22 19:21:28| 人976| 回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 antissa 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
    人(976) | 回(0)| 推 (0)| 收藏 (0)|
    全站分: 不分 | 人分: UVA |
    此分下一篇:[UVA] 196 - Spreadsheet
    此分上一篇:[UVA][dp] 11566 - Let's Yum Cha!

    是 (若未登入"人新台"看不到回覆唷!)
    * 入:
    入片中算式的果(可能0) 
    (有*必填)
    TOP
    全文
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86