CFLib.org – Common Function Library Project

ProperMod(y, x)

Last updated February 24, 2002

author

Tom Nunamaker

Version: 1 | Requires: CF5 | Library: MathLib

Description:
The mod function is defined as the amount by which a number exceeds the largest integer multiple of the divisor that is not greater than that number. CF has a mod operator, however, it only operates on integers and does not handle situations like 2.3 mod 2 (which should return 0.3).

Return Values:
Returns a numeric value.

Example:

<cfset y = 340>
<cfset x = 60>
<cfoutput>#y# mod #x# is #propermod(y,x)#<br></cfoutput>

<cfset y = -340>
<cfset x = 60>
<cfoutput>#y# mod #x# is #propermod(y,x)#<br></cfoutput>

<cfset y = 2.3>
<cfset x = 2>
<cfoutput>#y# mod #x# is #propermod(y,x)#<br></cfoutput>

Parameters:

Name Description Required
y Number to be modded. Yes
x Devisor. Yes

Full UDF Source:

/**
 * Computes the mathematical function Mod(y,x).
 * 
 * @param y      Number to be modded. 
 * @param x      Devisor. 
 * @return Returns a numeric value. 
 * @author Tom Nunamaker (tom@toshop.com) 
 * @version 1, February 24, 2002 
 */
function ProperMod(y,x) {
  var modvalue = y - x * int(y/x);
  
  if (modvalue LT 0) modvalue = modvalue + x;
  
  Return ( modvalue );
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson