I have a floating value $k$ and an integer $P$
I want to calculate $(\dfrac{k}{\sqrt5}) \mod P$
How do I calculate it?
PS: I know how to calculate MMI (Modulo Multiplicative Inverse of integer values using Fermat's little Theorem ). I am doing $k\times\sqrt5\times 5^{-1} \mod P$.
Here $5^{-1}$ is MMI of $5$ (modulo $P$). But the overall answer is coming wrong.
Eg : $k=4,\,P=7\,$ Hence, $\,4\times\sqrt5\times 5^{-1} \mod 7 = 1.62472$
Is it correct? I guess NO. So how to calculate it then?
The rule $(a \bmod p)·(b\bmod p)\bmod p=(a·b)\bmod p$ only works for integer $a,b$, since in $$ (a+k·p)·(b+m·p)=a·b+(am+bk+mkp)·p $$ the resulting factor in front of $p$ is usually not an integer if $a,b$ are not integers.
Thus your question has empty meaning, you can't even speak of wrong answers. To avoid that, explain how you would test that the answer obtained is correct.
You could, of course, specify
xas the solution of0==fmod(sqrt(5)*x-k,p). That is,x=(k+7*m)/sqrt(5)without the requirement ofxbeing integer.Or, in principle equivalent,
0==fmod(5*x-k*sqrt(5),p).Or, avoiding the roots,
0==fmod(5*x*x-k*k,p), which actually might have proper integer (remainder class) solutions.