Convert 8 memory bytes to a floating point number (double)

Source: Internet
Author: User

{
Function: Convert eight memory bytes (before low, after high) into a floating point double
Created by: skyjacker
URL: http: // blog.csdn.net/skyjacker
Email: hemiaoyu at gmail.com
Created on:
Function Syntax: IEEE Standard 754 (four memory bytes are converted to Single)
Dual precision: N contains 32 digits, of which s occupies 1 digit, e occupies 11 digits, and m occupies 52 digits.
Formula: N = (-1) ^ s * 2 ^ e * m
}
Function bytetofloat (const Bytes: tbyte8): Double;
Const
K = 11; // power double = 11
VaR
Tmpbyte: byte; // temporary bytes

// Formula-related
N: Double; // result
S: integer; // a signed digit that represents + 1-1
E: integer; // power value
M: Double; // a bit value

// Normalization
Bias: integer; // offset
En: integer; // The power value.

I: integer; // M 23 digits
A: integer; // 3 ~ 8 bytes
B: integer; // The tbits offset starts from 5.
Bits: tbits; // stores binary digits E = 11 m = 52

// Determine whether it is normalized
Function normalizedstatus (const bits: tbits): byte;
VaR
I: integer;
A, B: integer;
Begin
A: = 0;
B: = 0;
For I: = 11 downto 1 do // error: for I: = bits. Size (= 12) downto 1 do
Begin
If bits. Bits [I] Then
INC (B)
Else
INC ();
End;
If (bits. Size-1) = A then // bits. Size changes to 12
Result: = $00
Else if (bits. Size-1) = B then
Result: = $ FF
Else result: = $01; // normalized
End;
Begin

// Symbol correlation
If (Bytes [1] and $80) = $00 then
S: = 1
Else
S: =-1;

Bias: = POW (2, (k-1)-1;

// Sort out the 11-bit binary power e
Bits: = tbits. Create;
Bits. Size: = 11;
Tmpbyte: = bytes [1];
Tmpbyte: = tmpbyte SHL 1; // remove the symbol bit
For I: = 1 to 7 do
Begin
Bits [I]: = (tmpbyte and $80) = $80 );
Tmpbyte: = tmpbyte SHL 1;
End;

Tmpbyte: = bytes [2];
For I: = 1 to 4 do
Begin
Bits [I + 7]: = (tmpbyte and $80) = $80 );
Tmpbyte: = tmpbyte SHL 1;
End;
// Evaluate the power
En: = 0;
For I: = 11 downto 1 do
Begin
If bits. Bits [12-I] Then
En: = en + POW (2, (I-1 ));
End;

Tmpbyte: = normalizedstatus (BITs );
// Normalized judgment
If tmpbyte = $00 then
Begin
E: = 1-bias;
M: = 0; // The initial value of M is not normalized.
End
Else if tmpbyte = $ FF then
Begin
// Special value: When the binary bits of E are all 1, it is a special value.
Result: = 0; // prevents overflow or Initialization
Exit;
End
Else
Begin // normalized Calculation Method
E: = en-bias;
M: = 1; // The initial value of M is normalized.
End;

// Sort the 52-bit ending number binary m
Bits. Size: = 52; // size the previous 11 values are automatically retained. The result is not affected because the value is re-assigned.
Tmpbyte: = bytes [2]; // 5 ~ 8 bit
Tmpbyte: = tmpbyte SHL 4;
For I: = 1 to 4 do
Begin
Bits [I]: = (tmpbyte and $80) = $80 );
Tmpbyte: = tmpbyte SHL 1;
End;

// 3 to 8 bytes are stored in bits
B: = 5;
For a: = 3 to 8 do
Begin
Tmpbyte: = bytes [a];
For I: = 1 to 8 do
Begin
Bits [B]: = (tmpbyte and $80) = $80 );
Tmpbyte: = tmpbyte SHL 1;
INC (B );
End;
End;

// Evaluate m
For I: = 1 to 52 do
Begin
If bits. Bits [I] Then
M: = m + 1/power (2, I );
End;

N: = S * power (2, e) * m;

Bits. Free;

Result: = N;
End;

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.