-1

I wrote a function that receives a float number, changes it to its fractional part and returns its integer part. The fuction is structured as a if-else statement (to differentiate positive from negative numbers). However, only the first part of the statement is working. In the case below, only when I type negative or a zero number the code works; when I type a positive number, it only prints zero. The problem isn't with the algorithm for separating the integer from the fractional part as I also have structured a else-if with the first part being x positive, and also only the first part worked.

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

//creates a function that changes the value of x to its
//fractional part and returns its integer part
float integerpart(float *x)
{
    float integer = 0;
    //If x is negative
    if (*x<0)
    {
        //I use an algorithm to separate its integer from its fractional part
        while (*x+1 <= integer)
        {
            integer--;
        }
        //I define the new value of x as its fractional part
        *x = *x - (integer);
    }
    else if (*x=0)
    {
        *x = 0;
        integer = 0;
    }
    //In the case in which x is positive
    else
    {
        //I do the inverse algorithm
        while (integer <= *x-1)
        {
            integer++;
        }
        *x-=integer;
    }
    //I return the integer part
    return integer;
}


int main()
{
    //Ask for the user to type a real number
    float number;
    printf("Type a real number:\n");
    scanf("%f", &number);
    //Uses the function to take the integer part of x
    float integ;
    integ = integerpart(&number);
    //Prints the integer and fractional part
    printf("Integer part = %f   Fractional Part = %f\n", integ, number);
    system("pause");
    return 0;
}

  • 2
    Common typo: *x=0 is assignment. You want *x==0, which is comparison. – uneven_mark Oct 20 at 0:41
  • 1
    else if (*x=0) {} I see what you did there. BTW: for() loops are your friend. Also: comparing a float value to zero is difficult. To say the least... – wildplasser Oct 20 at 0:41
  • 1
    Is there a reason you aren't using functions like floor or ceiling? – Scott Hunter Oct 20 at 0:44
  • Are you trying to extract exact values? – Jason Oct 20 at 0:56
  • 2
    The library function modff does what you want. – Blastfurnace Oct 20 at 1:06
1

Consider this

while (integer <= *x-1)
{
    integer++;
}
*x-=integer;

For large values of x this will be painfully slow. The worst case (if the above worked as intended) would be with a maximum value of ~1e38. Let's say that your computer can perform a billion iterations of the loop per second. That's going to make a ~1e29 second runtime.

However, the above will not work as intended. For any value of x greater than ~1.6e7 the limits of single precision float accuracy mean that the unit in the last place (ULP) is greater than 1.0f. Concretely that means for values greater than about 16 million, the increment operator does nothing. The result is that the while loop will never exit.

As said in the comments, consider using standard functions. Otherwise I suggest that you pick apart the sign mantissa and exponents of the float value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.