Advertisement
juaniisuar

Untitled

Sep 16th, 2017
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define reint(x) (*(unsigned *)&x)
  6. #define expo(x) ((reint(x)&(255<<23))>>23)
  7. #define mantisa(x) (reint(x)& 8388607) /* 2^23 -1*/
  8.  
  9. void printb(unsigned x){
  10. int i;
  11. for(i = 31; i >=0; i--)
  12. printf("%d%s", !!(x & 1<<i), (i != 31 && i != 23) ? "" : " ");
  13. puts("");
  14. }
  15.  
  16. int myisnan(float f){
  17. return expo(f) == 255 && mantisa(f);
  18. }
  19. int myisnan2(float f){
  20. return !(f == f);
  21.  
  22. }
  23. typedef struct pepe argi;
  24. struct pepe{
  25. unsigned sign : 1;
  26. unsigned exp : 16;
  27. unsigned mantissa : 18;
  28. };
  29.  
  30. int iszero(argi a){
  31. return (a.mantissa == 0);
  32. }
  33.  
  34. argi suma(argi a, argi b){
  35. /// normales (no inf, ni NAN)
  36. if( a.exp < b.exp ){
  37. argi aux = a; a = b; b = aux;
  38. }
  39. /// a >= b
  40. unsigned shift = (a.exp - b.exp);
  41. unsigned manti = a.mantissa + (b.mantissa>>shift);
  42. if(manti & (1<<18)) a.exp++,(manti>>=1);
  43. a.mantissa = manti;
  44. return a;
  45. }
  46.  
  47. argi mult(argi a, argi b){
  48. if(iszero(a)) return a;
  49. if(iszero(b)) return b;
  50.  
  51. argi c;
  52. c.sign = a.sign ^ b.sign;
  53. c.exp = a.exp + b.exp - 30000;
  54. unsigned long long manti = (unsigned long long) a.mantissa * b.mantissa;
  55. manti >>= 18;
  56. unsigned long long aux = manti;
  57.  
  58. int pos;
  59. for(pos=0; aux >>= 1; pos++); //pos será la i-ésima posición del MSB de manti
  60.  
  61. printf("--%d--\n", pos);
  62. for(; pos>17; pos--){ manti >>= 1; c.exp++; } //normalizar
  63. for(; pos<17; pos++){ manti <<= 1; c.exp--; }
  64.  
  65. c.mantissa = manti;
  66. return c;
  67. }
  68.  
  69. void printargi(argi a){
  70. long double x = a.mantissa / (double)(1<<18);
  71. if(a.exp >= 30000)
  72. x *= 1 << (a.exp - 30000);
  73. else
  74. x /= 1 << (30000 - a.exp);
  75. printf("%.20Lf\n", x);
  76. }
  77.  
  78. void ej3(){
  79. float a = 6.02252e23;
  80. printb(reint(a));
  81. }
  82.  
  83. void printbargi(argi a){
  84. int i;
  85. printf("%d ",!!(a.sign & 1));
  86. for( i = 15; i >= 0; --i)
  87. printf("%d", !!(a.exp & (1 << i)));
  88. printf(" ");
  89. for( i = 17; i >= 0; --i)
  90. printf("%d", !!(a.mantissa & (1 << i)));
  91. puts(" ");
  92. }
  93.  
  94. void printfargi(argi a){
  95. printf("sign: %d - exp: %d - manti: %f\n", a.sign, a.exp, a.mantissa / (double)(1<<18));
  96. }
  97.  
  98. int main() {
  99. float g = 0.0;
  100. float f = 0.0/g;
  101. float h = 12.3;
  102. //printf("f: %f\n",f);
  103. //WARNING: NAN is a GNU Extension
  104. //if (myisnan2(f)) printf("Es Nan\n");
  105. //if (myisnan(f)) printf("isNaN dice que si\n");
  106. argi a = {0, 30000, 1<<15 | 1<<14 | 1<<12};
  107. argi b = {0, 30000, 1<<17 | 1<<15 | 1<<13};
  108. //printbargi(a);
  109. //printbargi(b);
  110. /*
  111. printargi(a);
  112. printargi(b);
  113.  
  114. printargi(suma(b,a));
  115. printargi(suma(a,b));
  116. */
  117. printfargi(a);
  118. printfargi(b);
  119.  
  120. argi k = mult(a,b);
  121. printfargi(k);
  122. printbargi(k);
  123.  
  124.  
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement