1

one example of my problem.

#include <stdio.h>
#include <math.h>
int main() {
    long double mod = fmod(0.06, 0.02);        
    printf("%Le", mod); //prints 2.000000e-02 instead of 0.000000e+00.
    long double mod1 = fmod(0.006, 0.002);        
    printf("%Le", mod1); //prints 0.000000e+00 correct.
    return 0;
} 

how can i solve than problem, i tried to use fmodf() and fmodl() but i have the same problem on another values.

i have to work with long double so is there some way to solve that...

  • 1
    You use the same function twice. Are you sure you’re testing what you think you’re testing? – Jonathan Leffler Oct 08 '17 at 17:37
  • Remember that [`fmod`](http://en.cppreference.com/w/c/numeric/math/fmod) only uses double-precision arithmetic. Assigning the `double` result to a `long double` is kind of useless. – Some programmer dude Oct 08 '17 at 17:39
  • yes but i use fmodl but i still with the same problem – Mohammad Jaber Oct 08 '17 at 17:40
  • long double mod = fmodl((long double) 0.06, (long double) 0.02); ....... long double mod1 = fmodl((long double) 0.006, (long double) 0.002);.......... i wrote it like this and i have the same values. – Mohammad Jaber Oct 08 '17 at 17:45
  • Yup. One of the many pitfalls when working with floating point. Do study the duplicate carefully: it will be obvious once you've read that. A simple resolution in this case is to work in integer arithmetic and scale the result. – Bathsheba Oct 08 '17 at 17:58
  • but the solve that they suggest didnt help my in my case. is there another solution. – Mohammad Jaber Oct 08 '17 at 17:59
  • you mean to do that .... (0.06 * 100) % (0.02*100) ?? – Mohammad Jaber Oct 08 '17 at 18:02

0 Answers0