mod

Hi everyone,

I need to take mod of two double numbers by using % command,

When I am trying to take mod of 18%5, the result is 3, OK.

But when I am trying to take mod of 17.9 and 5.5 like 17.9%5.5; the program gives fault.

Please help me

BRK




closed account (1vRz3TCk)
The modulus operator ('%') computes the remainder that results from performing integer division. So you can not use it with floating point types.

edit:
You could use fmod()...

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <math.h>

int main()
{
    float a = 17.9f;
    float b = 5.5f;
    float c = fmod(a,b);

    std::cout << c << std::endl;
    
    return 0;
}


http://www.cplusplus.com/reference/clibrary/cmath/fmod/
Last edited on
Thanks a lot for your help

Brk
Topic archived. No new replies allowed.