public double RoundXtoNearestIthPowerOfR(double x, double r, double i)
{
double res=0; r = Math.Abs(r);
if (r > 0)
{
double rr = x*Math.Pow(r, -i); double flrr = Math.Truncate(rr);
res = Math.Pow(r, i)*(flrr-Math.Truncate(2*flrr - 2*rr));
}
return res;
}