Returns the "correct" x%y (that is, the positive remainder of xy). Assumes y>0, but is made to deal with x<=0. - CSharp System

CSharp examples for System:Math Number

Description

Returns the "correct" x%y (that is, the positive remainder of xy). Assumes y>0, but is made to deal with x<=0.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*w  ww  . j a v  a2 s.co  m*/

public class Main{
        /// <summary>
        /// Returns the "correct" x%y (that is, the
        /// positive remainder of x/y).  Assumes
        /// y>0, but is made to deal with x<=0.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static float Mod(float x, float y)
        {
            float r = x % y;

            return (r < 0) ? r + y : r;
        }
        /// <summary>
        /// Returns the "correct" x%y (that is, the
        /// positive remainder of x/y).  Assumes
        /// y>0, but is made to deal with x<=0.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static int Mod(int x, int y)
        {
            int r = x % y;

            return (r < 0) ? r + y : r;
        }
}

Related Tutorials