• API
• FAQ
• Tools
• Trends
• Archive
SHARE
TWEET

# DoubleToBinary32_Binary32ToDouble

a guest Apr 29th, 2012 687 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. int DoubleToBinary32(double value)
2. {
3.   int minus = 0, integer, exponent = 127, fraction = 0, i, result;
4.
5.   if(value < 0) { minus = 0x80000000; value = -value; }
6.   integer = floor(value);
7.   value -= integer;
8.   for(i = 22; i >= 0; i--)
9.     {
10.     value += value;
11.     fraction += floor(value) * pow(2, i);
12.     value -= floor(value);
13.     }
14.   while((integer != 1) && (exponent > 0) && (exponent < 255))
15.     {
16.     if(integer > 1)
17.       {
18.       fraction = (integer&1)<<22 + fraction>>1;
19.       integer = integer>>1;
20.       exponent++;
21.       }
22.     else
23.       {
24.       integer = (fraction&0x400000)>>22;
25.       fraction = (fraction&0x3FFFFF)<<1;
26.       value += value;
27.       fraction += floor(value);
28.       value -= floor(value);
29.       exponent--;
30.       }
31.     }
32.   result = minus + exponent<<23 + fraction;
33.   return(result);
34. }
35.
36. double Binary32ToDouble(int value)
37. {
38.   int minus = -1, exponent;
39.   double fraction, result;
40.
41.   if(value&0x80000000 == 0) minus = 1;
42.   exponent = ((value&0x7F800000)>>23) - 127;
43.   fraction = value&0x7FFFFF + 0x800000;
44.   fraction = fraction / 0x800000;
45.   result = minus * fraction * pow(2, exponent);
46.   return(result);
47. }
RAW Paste Data
Top