How to make a modulo operation in objective-c / cocoa touch?

☆樱花仙子☆ 提交于 2019-11-26 08:07:48

问题


I have two CGFloat values, and want to calculate the modulo result. Or in other words: I want to know what\'s left if valueA is placed as much as possible into valueB.

So I just tried:

CGFloat moduloResult = valueB % valueA;

the compiler complains about the % and tells me: \"invalid operands to binary %\". Any idea?


回答1:


% is for int or long, not float or double.

You can use fmod() or fmodf() from <math.h> instead.

Better is <tgmath.h> as suggested by the inventor of CGFloat.




回答2:


If I remember correctly modulo requires 2 ints as its input so you'd need something like:

CGFloat moduloResult = (float)((int)valueB % (int)valueA);

Assuming that valueB and valueA are both floats



来源:https://stackoverflow.com/questions/1271102/how-to-make-a-modulo-operation-in-objective-c-cocoa-touch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!