Tuesday, 29 April 2014

How to find the remainder for the floating point numbers?

No comments:
Have you ever tried to find the remainder of the two floating point numbers.

Are u going to use the modulus operator?

No we cannot achieve this using the modulus operator?

Let us try with modulus operator? Let see what happens

    float f=1.23%1.5;

The following error will occur.

     error: invalid operands to binary % (have ‘double’ and ‘double’)

So whats the idea?

math.h library provides the solution for it.

    fmod(double dividend,double divisor);

This will returns the remainder of two floating point numbers.




    int main()
            printf("%d", fmod(1.25/1.5));
            return 0;



If you pass the divisor as 0, the overflow will occur and fmod returns the zero.

Compilation hint:

gcc test.c -lm

Here the lm is the library for the math file.

No comments:

Post a Comment