Modulo "%" function - how to?

Hello,

Trying the modulo function, ex: 121 % 100 = 21, to get the remainder of a division did not give the expected result.

Does someone knows if this function needs a library to work on the spark core web IDE?

Thank you,
Robert

This might not be the answer to your question. But if you need to implement it meanwhile you can use the actual formula to calculate it:

(Dividend - ((Dividend / Divisor) * Divisor))
1 Like

Yes, could use this meanwhile. Thanks you

The doc (http://docs.spark.io/#/firmware/arithmetic-operators-modulo) says that modulo only works on integer types and not on floats. Could that be your problem?

The doc also gives a work-around expression for floats that does work.

I have used modulo on unsigned long’s in my code and it worked fine for me.

1 Like

The other points of view made me realize that it was actually a code related problem and not the modulo function itself. After checking it again, I confirm that the function works well on integers types.

Thank you for giving me reactions to my question and something to learn. Thanks!

1 Like

@ bko,
Hi!
how do I divide two variables? or even constants for that matter. X = A/B?

The “/” does not seem to act like a division. All others “+”, "=’, etc… are red when I use the IDE but “/” stays white.
I know it is something simple but I can’t figure it out

thanks!

Hi @Dup

Divide work OK for me, but it follows the C rules for integer division, which can be surprising. The operator coloring in the web IDE might not be perfect, but that does not affect your code at all.

Are you getting a compile error or an unexpected value at run time?

Can you show a simple example?

@bko,
thanks

int A=1/2;

int B = ( A * 300);

result of B should be 150 but I get zero

This thread confuses me. Dup, when working with integers you cannot expect a non integer value for 1/2. The result is 0.5 which is integer zero. Integers are WHOLE numbers and cannot represent fractional values. However if you do float A = 1/2 and float B = (A 300), you will get the correct results. Also, doing int B = (int)(A * 300) should work as well since (A300) being evaluated first is a float value then being cast to an (int).

The modulo operator does not make sense when using floating point numbers because your remainder is preserved in your decimal values. However, when using integers, the remainder is lost and thus the need for modulo. :smile:

1 Like

Thanks guys,
it makes more sense now :smile:

Glad you guys got it worked out! @peekay123 is spot on here.

I will just say the modulo can make sense with floating point numbers in the sense of you want the integer-and-remainder representation for some reason. For for floating point modulo, the standard way to do a mod b is:

a - (b * ((int)(a / b)))

This is based on the definition of division being a/b = q + r/b where q is the integer part and r is remainder.

2 Likes

Hi,
me again
anything less than 1 equates to 0 (i.e 1/2). I tried different approaches but nothing seem to work :frowning:

I am using two variables A and B in an equation (A/B) and B will always be greater than A therefore A/B will always be less than 1

must I use the Modulo function?

thanks!

YES but ONLY if A and B are integers. Are they?

Hi @Dub,

This just means that integer division (which finds the integer nearest the quotient) is not the way you want to go. If A and B are integer types (such as int or uint8_t or char or byte etc.) then you cannot get a fractional result. It is just not representable as an integer.

You can get a floating point type like double or float to represent a fraction.

Modulo gives you the remainder, not the quotient, so that is not the same at all.

Maybe you should tell us more about why you are dividing these numbers and then we can help more.

Hi!
That was my problem!!
when i tried 0.2/0.3, it worked!

A and B are currently integers but I will try making them float and it should work!!

thanks again!

x%y = x - (y*int(x/y))

Just occurred to me you can also use:

#include <cmath>
std::fmod(x,y);