Jump to content

Always returning 0 in C

CJPowell27

Hey guys, I'm having an issue with my program not returning the value that it should. I know its probably 

something to do with my scanf use, but I cannot think of what to do. Could anyone spot where my issue is>

Spoiler

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

//---------------------------------------------
//Variables
//---------------------------------------------
int fracBits, expBits, fracBitIsolate, expBitIsolate;
unsigned int hex;

//---------------------------------------------
//Function Prototypes
//---------------------------------------------
int mathDenorm(int fracBitIsolate, int expBitIsolate);
int mathNorm(int fracBitIsolate, int expBitIsolate);
int inRange(int fracBits, int expBits);

//---------------------------------------------
//Program
//---------------------------------------------
int main()
{
    printf("Test\n");
    scanf("%d %d %x", &fracBits, &expBits, &hex);
    //Check if split numbers are all in range.
    if(!inRange(fracBits,expBits))
    {
        printf("\nThe numbers entered are invalid. Exiting program\n");
        return 0; //This will exit the program.
    }

    int signIsolate = hex >> expBits + fracBits;//Correct
    int expIsolate = hex >> fracBits; 
        expIsolate = expIsolate << (32 - expBits);
        expIsolate = expIsolate >> (32-expBits);
    int fracIsolate = hex << (32 - fracBits) >> (32 - fracBits);
    if(signIsolate == 1)
    {
        //The number is negative
        printf("-");
    }



    if(expBitIsolate == 0)//Calls a math function for a denormalized number
        printf("\nThe value of the number is: %d \n",mathDenorm(fracBitIsolate, expBitIsolate));
    
    if(expBitIsolate > 0)//Calls a math function for a normalized number, inf is handled by function itself
        printf("\nThe value of the number is: %d \n",mathNorm(fracBitIsolate, expBitIsolate));

}

//-----------------------------------------------
//Functions
//-----------------------------------------------
int inRange(int fracBits, int expBits)//All vars right and done
{
    if(((fracBits >= 2) && (fracBits <= 15)) && ((expBits >= 3) && (expBits <=5)))
    {
        return 1;
    }
    else
        return 0;
}


int mathDenorm(int fracBitIsolate, int expBitIsolate)
{
    int bias = pow(2,expBitIsolate-1)-1;//Done
    int E = 1 - bias;//Done
    int M = fracBitIsolate/8;//Done

    int ret = M * pow(2,E);
    
    return ret;
}


int mathNorm(int fracBitIsolate, int expBitIsolate)
{
    //E is the exp - bias;
    //M is the fracBits;
    if(~expBits == 0 && fracBits == 0)
    {
        printf("INF");
        return 0;
    }
    int bias = pow(2,expBitIsolate-1)-1;//Done
    int E = expBitIsolate - bias;//Done
    int M = 1 + (fracBitIsolate/8);//Done

    int ret = M * pow(2,E);
    
    return ret;
}

 

 

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
Share on other sites

Link to post
Share on other sites

If you're talking about the return value of the whole program (as measured by your shell), you currently don't specify an explicit return value, so most compilers (certainly GCC) default to returning 0. You would need to add an explicit return value in your main function to fix that, but convention is that 0 means success and non-zero means error, so returning a number from the program would be unusual.

If that's not the bit that's not working, we're going to need more details on exactly what's not working.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, colonel_mortis said:

If you're talking about the return value of the whole program (as measured by your shell), you currently don't specify an explicit return value, so most compilers (certainly GCC) default to returning 0. You would need to add an explicit return value in your main function to fix that, but convention is that 0 means success and non-zero means error, so returning a number from the program would be unusual.

If that's not the bit that's not working, we're going to need more details on exactly what's not working.

Oops I guess I should've specified which return value. the mathNorm and mathDenorm  functions should be returning the calculated value to main which would be printed from the line below

 

30 minutes ago, CJPowell27 said:

printf("\nThe value of the number is: %d \n",mathDenorm(fracBitIsolate, expBitIsolate));

 but they only return 0.

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, CJPowell27 said:

<snip>

For starters, 'fracBitIsolate' and 'expBitIsolate' are never assigned a value before you use them as parameters to call those functions.

 

Also, you're doing questionable things like dual bitshifts on a sigle line:

int fracIsolate = hex << (32 - fracBits) >> (32 - fracBits);

Is this shifting 'hex' (32 - fracbits) to the left and then shifting the result of that (32 - fracbits) to the right ?

Or is it shifting (32 - fracbits) (32 - fracbits) to the right and then shifting 'hex' to the left with the resulting amount of bits? Why not split such things up into multiple lines and give the programmers that come after you some headache relief.

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, CJPowell27 said:

Oops I guess I should've specified which return value. the mathNorm and mathDenorm  functions should be returning the calculated value to main which would be printed from the line below

 

 but they only return 0.

Firstly, you declared expBitIsolate as a global variable, and use it when reading but then use a new variable expIsolate when assigning to it. Fixing that makes your program print non-zero for my test cases.

If your code is still not working as you expect, you should create some test cases, where you decide what the program should output and what state it should be in at each step. You can then add some print statements throughout your program to see where it's deviating from that. (Ideally you want to use a debugger like gdb, but if you're fairly new to C it's probably easier to just use print statements).

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, colonel_mortis said:

Firstly, you declared expBitIsolate as a global variable, and use it when reading but then use a new variable expIsolate when assigning to it. Fixing that makes your program print non-zero for my test cases.

If your code is still not working as you expect, you should create some test cases, where you decide what the program should output and what state it should be in at each step. You can then add some print statements throughout your program to see where it's deviating from that. (Ideally you want to use a debugger like gdb, but if you're fairly new to C it's probably easier to just use print statements).

Oh my god I didn't even notice that I didn't use the same variable name, making those all match should fix it right?

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, CJPowell27 said:

Oh my god I didn't even notice that I didn't use the same variable name, making those all match should fix it right?

Fixing that makes the program print a non-zero value at the end, but that doesn't necessarily mean it will give the value you are expecting - you'll have to test it to check that, and if it isn't you'll need to add some debugging printf statements to figure out why not.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, colonel_mortis said:

Fixing that makes the program print a non-zero value at the end, but that doesn't necessarily mean it will give the value you are expecting - you'll have to test it to check that, and if it isn't you'll need to add some debugging printf statements to figure out why not.

Okay I got that working! Do you have any idea how I would make a bitmasked number into a binary decimal for example 0111 into 7/16?

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, CJPowell27 said:

Do you have any idea how I would make a bitmasked number into a binary decimal for example 0111 into 7/16?

Binary 0111 isn't 7/16.  It is just 7.  It's an integer...there's no real reason to convert it to a floating point.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×