Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * File: main.c
- * Author: Peter Heusch
- *
- * Created on 3. April 2018, 11:19
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #define SIGN(x) (x>>31);
- #define MANT(x) (x&0x7FFFFF);
- #define EXPO(x) ((x>>23)&0xFF);
- #define SPLIT(x, s, m, e) do { \
- s = SIGN(x); \
- m = MANT(x); \
- e = EXPO(x); \
- if ( e != 0x00 && e != 0xFF ) { \
- m |= 0x800000; \
- } \
- } while ( 0 )
- #define BUILD(x, s, m, e) do { \
- x = (s << 31) | (e<<23) | (m&0x7FFFFF); \
- } while ( 0 ) \
- float foo(float *a, float *b) {
- uint32_t ia = *(unsigned int *) a;
- uint32_t ib = *(unsigned int *) b;
- uint32_t result = 0;
- int signa, signb, signr;
- int manta, mantb, mantr;
- int expoa, expob, expor;
- SPLIT(ia, signa, manta, expoa);
- SPLIT(ib, signb, mantb, expob);
- printf("%d %d %d \n", signa, manta, expoa);
- printf("%d %d %d \n", signb, mantb, expob);
- // Berechnen Sie hier signr mantr und expor!
- if (expoa == expob) {
- signr = (signa + signb);
- mantr = (manta + mantb) >> 1;
- expor = expoa + 1;
- } else {
- int shift = expoa - expob;
- if (shift > 0) {
- expob = expob + shift;
- mantb = mantb>>shift;
- printf("%d %d\n", expob, mantb);
- signr = (signa + signb);
- mantr = (manta + mantb);
- expor = expoa;
- }
- if (shift < 0) {
- shift *= -1;
- printf("%d \n", shift);
- expoa = expoa + shift;
- manta = manta >> shift;
- signr = (signa + signb);
- mantr = (manta + mantb);
- expor = expob;
- }
- }
- BUILD(result, signr, mantr, expor);
- return *(float *) &result;
- }
- /*
- *
- */
- int main(int argc, char** argv) {
- // Nebenbedingungen im Test:
- // Die Eingaben sind Zahlen im Bereich [-10.0...10.0]
- // Bei der Division kommt die 0 als Divisor nicht vor!
- float f = 1.2;
- float g = 8.9;
- float h = foo(&f, &g);
- printf("%lf %lf %lf", f, g, h);
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement