Testing floating point numbers

Hi guys

I have problem with my simple calculator, author of my book wrote

One way I tried is to test if one the inpur number is grater than zero, and then substatct

And my protptype function is

#include <stdio.h> 

int main(void) {
    
    float a, b , result;
    
    printf("\t\tThis program is only a test for %% C function\n");
    printf("Enrer two numbers: ");
    scanf("%f %f", &a, &b);
    
    // Test if input number has fractional part or not 
    
    if ( (a > 0) && (a - (int) a ) == 0 && (b > 0) && (b - (int) b ) == 0) {
        (int) (result = ((int) a) % ((int)b));
        printf("Result: %d\n", result);
        return 0;
      }
    else if ( (a < 0) && a + ( (int) - (a) ) == 0 && (b < 0) && b + ( (int)-(b) ) == 0) {
        (int) (result = ((int) a) % ((int)b));
        printf("Result: %d\n", result);
        return 0;
      }
    else if ( (a > 0) && (a - (int) a ) == 0 && (b < 0) && b - ( (int) - (b) ) == 0) {
        (int) (result = ((int) a) % ((int)b));
        printf("Result: %d\n", result);
        return 0;
      }
    else if ((a < 0) && a - ((int)-(a)) == 0  && (b > 0) && (b - (int) b ) == 0) {
        (int) (result = ((int) a) % ((int)b));
        printf("Result: %d\n", result);
        return 0;
        }
    else
        printf("Cannot perform action\n");
        return 1;
    }

But this is too complicated for basic calculator. I don't my C code look like spaghetti.

My simple algorithm (flowchart) is what I have on my mind, but litle black box (diamond) is that I don't know how to implement in

http://img62.imageshack.us/img62/4655/diagram1o.jpg

I guess the author wants you to use the floor() function:

int fraction_is_zero(double val)
{
     double b=floor(val);
     int retval=(b>0) ? 0 : 1;
     return retval;
}

floor() finds the largest whole number (integer) less than val. This function
returns 1 if the largest whole number == val. or the fractional part of the number is zero.

Jim thanks for your answer but I don't think the author ment to use the floor() function because for now I only solved begginer C exercizes.

But I will try it to implement :wink:

Are you at least allowed to use abs()?

int ival=(int)fval;
float d=abs(fval-ival);
if(d < 0.000000001)
{
        printf("Float %f has no fractional part\n");
}

== with floating almost never works because of the many digits of accuracy, but < > are reliable.

Have you come across the bitwise operators yet...as this can easily be solved using them.

bitwise operators -- on floating point numbers? You're assuming certain formats and endian-nesses of floats, and having to do pointer typecast tricks to get at the bits at all.

Why would endianness matter if program is run on the same machine it is compiled on...and all follow the IEEE format on unix/linux flavors.

OK, Shamrock, I am curious. How would you solve the OPs problem using bitwise operators?

It appears to be my bad as I misunderstood what's needed. IMO the op wanted to determine if the fractional part of the float stored in memory is zero (easy todo with bitwise operators). After carefully reading the post it seems the OP wants to determine if the number input has a fractional part which is an entirely different matter.

Thanks for the clarification, Shamrock. I was scratching my head wondering how you could safely do it using bitwise operators in a Korn or Bash shell.

BTW, you can safely manipulate IEEE Floating Point number bits in certain languages if you know what you are doing. For an example see Half-Precision Floating Point Format

I wasnt thinking k/bash but a short piece of C code that tells if fractional part of the float stored in memory is zero...

read a floating point number
make an int* to address of the float
& with 0x007fffff tells if fractional part is zero

but after rereading the post I dont think this is what the op intends :o

That's a very nice blog and I must admit until now i didnt even know the existence of half precision floats. So do you do a lot of graphics processing.

Guys thanks a lot for new and interesting informations, I gaved up because I don't have any advanced knowledge so I printed result using floats.

You may close the thread