"C-language simulation implementation" floating-point number-to-point number

Source: Internet
Author: User

If you want to be super God, you must be fine!

Knowledge Preparation: 1. 16 binary form of the output floating point number? (with pointer output)

Converts floating-point pointer-to-integer pointer to output the pointer content in 16-binary format.

Sample program:

#include <stdio.h>int  main () {        float *var;        scanf ("%f",var);        printf ("%x", * ((int*)var));}

Test :

Input (float) Output (hex) Binary
8.25 41040000 0100 0001 0000 0100 0000 0000 0000 0000
-8.25 c1040000 1100 0001 0000 0100 0000 0000 0000 0000

Floating point number in the computer's storage format?

Sign bit: 0 is positive, 1 is negative;

Digit: Shift code representation;

Trailing bit: Implicitly stores 1 in front of the decimal point, that is, only the bits after the decimal point are stored

Example:

Decimal: 8.25

Binary: 1000.01 = 1.00001 x = 1.00001 x 2011

The sign bit is: 0

Decimal point: 3 + 127 = 0010b

Number of digits: 00001

Finally, 8.25 is stored in the computer in the form of 0100 0001 0000 0100 0000 0000 0000 0000B

Compare our own calculated results with the results of the computer output, consistent: The program is correct.

It can be formally programmed.

Programming:

We type int as a container of fixed-point number, assuming the fixed-point number 32 bits, symbol Part 1 bit, integer Part 15 bit, fractional part 16 bit

The symbol of floating-point number, integral part and fractional part are obtained respectively, corresponding to the parts of fixed-point numbers.

Program:

#include <stdio.h>#defineSign_bit 0x80000000#defineExp_bit 0x7f800000#defineTail_bit 0X007FFFFFintMain () {float*afloat;//floating point number        intAfix =0;//Point number Container        intTMP =0;//floating point Container        intExp =0;//Exponential size        intTail =0;//Trailing bits containerscanf"%f", afloat); TMP= *((int*) afloat);//symbol bit for fixed point numberAfix = tmp &Sign_bit; //integer portion of the fixed-point numberExp = ((tmp & Exp_bit) >> at) -127;//Exponential valueTail= ((tmp & Tail_bit) |0x00800000);//Mantissa, Gentlemen .Afix= Afix | ((Tail >> ( at-EXP)) << -); //the decimal part of the fixed point numberAfix = Afix | (Tail & ~ (0xFFFFFFFF<< ( at-EXP))) >> (7-exp)); printf ("%x\n", Afix);}

Test:

Input Output (hex) Output (binary)
8.25 84000 0000 0000 0000 1000 0100 0000 0000 0000 0000
-8.25 80084000 1000 0000 0000 1000 0100 0000 0000 0000 0000

Follow the rules we specified earlier: sign bits of fixed-point number 1 bits, integer digits 15 bits, decimal place 16 bits

The binary is converted and the answer is correct.

C language Simulation Implementation floating point number-goto-Fixed number

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.