Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define reint(x) (*(unsigned *)&x)
- #define expo(x) ((reint(x)&(255<<23))>>23)
- #define mantisa(x) (reint(x)& 8388607) /* 2^23 -1*/
- void printb(unsigned x){
- int i;
- for(i = 31; i >=0; i--)
- printf("%d%s", !!(x & 1<<i), (i != 31 && i != 23) ? "" : " ");
- puts("");
- }
- int myisnan(float f){
- return expo(f) == 255 && mantisa(f);
- }
- int myisnan2(float f){
- return !(f == f);
- }
- typedef struct pepe argi;
- struct pepe{
- unsigned sign : 1;
- unsigned exp : 16;
- unsigned mantissa : 18;
- };
- int iszero(argi a){
- return (a.mantissa == 0);
- }
- argi suma(argi a, argi b){
- /// normales (no inf, ni NAN)
- if( a.exp < b.exp ){
- argi aux = a; a = b; b = aux;
- }
- /// a >= b
- unsigned shift = (a.exp - b.exp);
- unsigned manti = a.mantissa + (b.mantissa>>shift);
- if(manti & (1<<18)) a.exp++,(manti>>=1);
- a.mantissa = manti;
- return a;
- }
- argi mult(argi a, argi b){
- if(iszero(a)) return a;
- if(iszero(b)) return b;
- argi c;
- c.sign = a.sign ^ b.sign;
- c.exp = a.exp + b.exp - 30000;
- unsigned long long manti = (unsigned long long) a.mantissa * b.mantissa;
- manti >>= 18;
- unsigned long long aux = manti;
- int pos;
- for(pos=0; aux >>= 1; pos++); //pos será la i-ésima posición del MSB de manti
- printf("--%d--\n", pos);
- for(; pos>17; pos--){ manti >>= 1; c.exp++; } //normalizar
- for(; pos<17; pos++){ manti <<= 1; c.exp--; }
- c.mantissa = manti;
- return c;
- }
- void printargi(argi a){
- long double x = a.mantissa / (double)(1<<18);
- if(a.exp >= 30000)
- x *= 1 << (a.exp - 30000);
- else
- x /= 1 << (30000 - a.exp);
- printf("%.20Lf\n", x);
- }
- void ej3(){
- float a = 6.02252e23;
- printb(reint(a));
- }
- void printbargi(argi a){
- int i;
- printf("%d ",!!(a.sign & 1));
- for( i = 15; i >= 0; --i)
- printf("%d", !!(a.exp & (1 << i)));
- printf(" ");
- for( i = 17; i >= 0; --i)
- printf("%d", !!(a.mantissa & (1 << i)));
- puts(" ");
- }
- void printfargi(argi a){
- printf("sign: %d - exp: %d - manti: %f\n", a.sign, a.exp, a.mantissa / (double)(1<<18));
- }
- int main() {
- float g = 0.0;
- float f = 0.0/g;
- float h = 12.3;
- //printf("f: %f\n",f);
- //WARNING: NAN is a GNU Extension
- //if (myisnan2(f)) printf("Es Nan\n");
- //if (myisnan(f)) printf("isNaN dice que si\n");
- argi a = {0, 30000, 1<<15 | 1<<14 | 1<<12};
- argi b = {0, 30000, 1<<17 | 1<<15 | 1<<13};
- //printbargi(a);
- //printbargi(b);
- /*
- printargi(a);
- printargi(b);
- printargi(suma(b,a));
- printargi(suma(a,b));
- */
- printfargi(a);
- printfargi(b);
- argi k = mult(a,b);
- printfargi(k);
- printbargi(k);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement