SHARE
TWEET

Untitled

a guest Apr 21st, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /*
  8.  * File:   main.c
  9.  * Author: Peter Heusch
  10.  *
  11.  * Created on 3. April 2018, 11:19
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17.  
  18. #define  SIGN(x) (x>>31);
  19. #define  MANT(x) (x&0x7FFFFF);
  20. #define  EXPO(x) ((x>>23)&0xFF);
  21. #define SPLIT(x, s, m, e) do {    \
  22.   s = SIGN(x);                    \
  23.   m = MANT(x);                    \
  24.   e = EXPO(x);                    \
  25.   if ( e != 0x00 && e != 0xFF ) { \
  26.     m |= 0x800000;                \
  27.   }                               \
  28. } while ( 0 )
  29.  
  30. #define BUILD(x, s, m, e) do {               \
  31.     x = (s << 31) | (e<<23) | (m&0x7FFFFF);  \
  32. } while ( 0 )                                \
  33.  
  34.  
  35. float foo(float *a, float *b) {
  36.     uint32_t ia = *(unsigned int *) a;
  37.     uint32_t ib = *(unsigned int *) b;
  38.     uint32_t result = 0;
  39.     int signa, signb, signr;
  40.     int manta, mantb, mantr;
  41.     int expoa, expob, expor;
  42.     SPLIT(ia, signa, manta, expoa);
  43.     SPLIT(ib, signb, mantb, expob);
  44.     printf("%d %d %d \n", signa, manta, expoa);
  45.     printf("%d %d %d \n", signb, mantb, expob);
  46.     // Berechnen Sie hier signr mantr und expor!
  47.     if (expoa == expob) {
  48.         signr = (signa + signb);
  49.         mantr = (manta + mantb) >> 1;
  50.         expor = expoa + 1;
  51.     } else {
  52.         int shift = expoa - expob;
  53.  
  54.         if (shift > 0) {
  55.             expob = expob + shift;
  56.             mantb = mantb>>shift;
  57.             printf("%d %d\n", expob, mantb);
  58.             signr = (signa + signb);
  59.             mantr = (manta + mantb);
  60.             expor = expoa;
  61.         }
  62.         if (shift < 0) {
  63.             shift *= -1;
  64.             printf("%d \n", shift);
  65.             expoa = expoa + shift;
  66.             manta = manta >> shift;
  67.             signr = (signa + signb);
  68.             mantr = (manta + mantb);
  69.             expor = expob;
  70.         }
  71.     }
  72.     BUILD(result, signr, mantr, expor);
  73.     return *(float *) &result;
  74. }
  75.  
  76. /*
  77.  *
  78.  */
  79.  
  80. int main(int argc, char** argv) {
  81.     // Nebenbedingungen im Test:
  82.     // Die Eingaben sind Zahlen im Bereich [-10.0...10.0]
  83.     // Bei der Division kommt die 0 als Divisor nicht vor!
  84.     float f = 1.2;
  85.     float g = 8.9;
  86.     float h = foo(&f, &g);
  87.     printf("%lf %lf %lf", f, g, h);
  88.     return (EXIT_SUCCESS);
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top