#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Multiply and division without operators in C 4byte float ieee754


    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 far, but its not counting correctly.

    Code:
    #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();
        }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Is it the multiply or divide function that is not working. Also there is a part missing near if (number - divideBy <

IMN logo majestic logo threadwatch logo seochat tools logo