---
warning: This file is auto generated by `yarn web:inject`, do not edit by hand
examples:
- mod(7, 3)
- mod(-7, 3)
returns:
- '1'
- '2'
dependencies:
- python/_helpers/_operator
authors:
original by:
- Kevin van Zonneveld (https://kvz.io)
notes:
- >-
Uses Python's modulo semantics, where the remainder follows the divisor
sign.
parityVerified:
- Python 3.12
isTypescript: true
type: function
layout: function
title: Python's operator.mod in TypeScript
description: >-
Here's what our current TypeScript equivalent to Python's
operator.mod looks like.
function: mod
category: operator
language: python
permalink: python/operator/mod/
alias:
- /functions/python/mod/
- /functions/operator/mod/
- /python/mod/
---
{% codeblock lang:typescript %}import { pythonMod } from '../_helpers/_operator.ts'
export function mod(a: unknown, b: unknown): number {
// discuss at: https://locutus.io/python/operator/mod/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Uses Python's modulo semantics, where the remainder follows the divisor sign.
// example 1: mod(7, 3)
// returns 1: 1
// example 2: mod(-7, 3)
// returns 2: 2
return pythonMod(a, b, 'mod')
}
{% endcodeblock %}
{% codeblock lang:javascript %}import { pythonMod } from '../_helpers/_operator.ts'
export function mod(a, b) {
// discuss at: https://locutus.io/python/operator/mod/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Uses Python's modulo semantics, where the remainder follows the divisor sign.
// example 1: mod(7, 3)
// returns 1: 1
// example 2: mod(-7, 3)
// returns 2: 2
return pythonMod(a, b, 'mod')
}{% endcodeblock %}
{% codeblock lang:typescript %}// python/_helpers/_operator (Locutus helper dependency)
function toPythonNumber(value: unknown, functionName: string): number {
if (typeof value === 'number') {
return value
}
if (typeof value === 'boolean') {
return value ? 1 : 0
}
if (typeof value === 'bigint') {
const numericValue = Number(value)
if (!Number.isSafeInteger(numericValue)) {
throw new RangeError(`${functionName}() bigint values must fit within JS safe integer precision`)
}
return numericValue
}
throw new TypeError(`${functionName}() expected a numeric value`)
}
function pythonMod(left: unknown, right: unknown, functionName: string): number {
const dividend = toPythonNumber(left, functionName)
const divisor = toPythonNumber(right, functionName)
if (divisor === 0) {
throw new Error('integer division or modulo by zero')
}
return dividend - Math.floor(dividend / divisor) * divisor
}
// python/operator/mod (target function module)
function mod(a: unknown, b: unknown): number {
// discuss at: https://locutus.io/python/operator/mod/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Uses Python's modulo semantics, where the remainder follows the divisor sign.
// example 1: mod(7, 3)
// returns 1: 1
// example 2: mod(-7, 3)
// returns 2: 2
return pythonMod(a, b, 'mod')
}{% endcodeblock %}
{% codeblock lang:javascript %}// python/_helpers/_operator (Locutus helper dependency)
function toPythonNumber(value, functionName) {
if (typeof value === 'number') {
return value
}
if (typeof value === 'boolean') {
return value ? 1 : 0
}
if (typeof value === 'bigint') {
const numericValue = Number(value)
if (!Number.isSafeInteger(numericValue)) {
throw new RangeError(`${functionName}() bigint values must fit within JS safe integer precision`)
}
return numericValue
}
throw new TypeError(`${functionName}() expected a numeric value`)
}
function pythonMod(left, right, functionName) {
const dividend = toPythonNumber(left, functionName)
const divisor = toPythonNumber(right, functionName)
if (divisor === 0) {
throw new Error('integer division or modulo by zero')
}
return dividend - Math.floor(dividend / divisor) * divisor
}
// python/operator/mod (target function module)
function mod(a, b) {
// discuss at: https://locutus.io/python/operator/mod/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Uses Python's modulo semantics, where the remainder follows the divisor sign.
// example 1: mod(7, 3)
// returns 1: 1
// example 2: mod(-7, 3)
// returns 2: 2
return pythonMod(a, b, 'mod')
}{% endcodeblock %}