This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

division remainder seems to be wrong

Hi all,

I need to recover the remainder of a division but no matter what I try the result comes out as 1.

so far I have tried:
u8 a = 10
u8 b = 3
u8 r;

r = a % b;
r = fmod(a,b)
res = div(a,b)
r = res.rem;

Casting applied in real code removed for ease of reading.
10/3 = 3.33 clearly
what I need is a way to recover the 33 etc, I expect it to be truncated but I didn't expect to get 1 or all remainders.

Compiler is realview Arm, on a STM32 cortex-m3.

Thanks,
Dave

Parents
  • Floating point: 10 / 3 = 3.3333333
    Integer: 10 / 3 = 3

    10 - 3*3 = 1

    So, since the reminder _is_ 1, you should stop trying to get a different reminder... ;)

    In this case, you are _not_ interested in getting a reminder, you are interested in retrieving the fractional value.

    Several ways go get your value:

    (10 % 3) / 3.0;
    
    fmod(10.0/3.0,1.0);
    
    10.0/3.0 - (10/3);
    
    10.0/3.0 - floor(10.0/3.0);
    

Reply
  • Floating point: 10 / 3 = 3.3333333
    Integer: 10 / 3 = 3

    10 - 3*3 = 1

    So, since the reminder _is_ 1, you should stop trying to get a different reminder... ;)

    In this case, you are _not_ interested in getting a reminder, you are interested in retrieving the fractional value.

    Several ways go get your value:

    (10 % 3) / 3.0;
    
    fmod(10.0/3.0,1.0);
    
    10.0/3.0 - (10/3);
    
    10.0/3.0 - floor(10.0/3.0);
    

Children