jeudi 5 mars 2015

Modulo function in c++, that behaves like mod in matlab


I do a simulation with a lot of particles (up to 100000) in periodic domain(box), and in order particles to stay inside the box, I use modulo function with float or double numbers.


In Matlab everything works great with mod function. However in C++ I found out, that function fmod is not completely equal to Matlab's mod function:


mod(-0.5,10)=9.5 - I want this result in C++


fmod(-0.5,10)=-0.5 - I don't want this.


I, of course, can solve my problem with if statements. However, i think, it will affect efficiency (if statement in critical loop). Is there a way to implement this function without if statement? May be some other function?


Thanks.




Aucun commentaire:

Enregistrer un commentaire