Multiply and division without operators in C 4byte float ieee754

Viewed 632

I need some help doing programing. I have to write a program, which can divide and multiply without the opearators and contains the IEEE 754 standard. I can use only +,-, negation and logical operations. The numbers and the operators needs to be read from a file.

I got this so far, but it's not working correctly.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

float mult(float a, float b) {
    float i = 0;
    float c = 0;
    while (i < b) {
        c = c + a;
        i++;
    }
    return c;
}


//this will be needed to divide
float product=1,multiplier=2,a=1;
int steps=0;
void divCore(float number, float divideBy,float lastDivison)
{
    steps++;
    if(number - divideBy < 
        return;
    else
    {
        lastDivison = divideBy;
        divideBy *= multiplier;
        if(number >= divideBy)
        {
            product *= multiplier;
            divCore(number,divideBy,lastDivison);
        }
        else
        {
            a *= 0.5;
            multiplier = 1 + a;
            divCore(number,lastDivison,lastDivison);
        }
    }
}
//its an other function for division ,you use this in main
float Divide(float numerator, float denominator)
{
    //init data
    int neg=(numerator<0)?-1:1;
    neg*=(denominator<0)?-1:1;
    product = 1;
    multiplier = 2;
    a = 1;
    steps =0;
    divCore(abs(numerator),abs(denominator),0);
    return product*neg;
}

int main()
{
    float i,j;
    char c[2];
    FILE *in=fopen("input.txt","r");
    fscanf(in,"%f",&i);
    fscanf(in,"%s",c);
    fscanf(in,"%f",&j);
    if(strcmp(c,"*")==0){
        float a = mult(i,j);
        printf("%f\n", a);
    }
    if(strcmp(c,"/")==0){
        float a2 = Divide(i,j);
        printf("%f\n", a2);
    }

    getchar();
    getchar();
}

And the file looks like this (I think the numbers doesnt count):

0x0000C942
*
0x0000C942
1 Answers
Related