- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.6.x
- Description
- A modern JavaScript utility library delivering modularity, performance & extras
- Author
- tessl
- Last updated
math.md docs/
1# Mathematical Operations23Mathematical utilities including basic arithmetic, rounding, and statistical operations. These methods provide safe and consistent mathematical computations.45## Capabilities67### Basic Arithmetic89Fundamental arithmetic operations.1011```javascript { .api }12/**13* Adds two numbers.14*15* @param {number} augend The first number in an addition16* @param {number} addend The second number in an addition17* @returns {number} Returns the sum18*/19function add(augend: number, addend: number): number;2021/**22* Subtracts two numbers.23*24* @param {number} minuend The first number in a subtraction25* @param {number} subtrahend The second number in a subtraction26* @returns {number} Returns the difference27*/28function subtract(minuend: number, subtrahend: number): number;29```3031**Usage Examples:**3233```javascript34import { add, subtract } from "lodash";3536add(6, 4); // 1037add(1.2, 0.8); // 23839subtract(6, 4); // 240subtract(1.2, 0.8); // 0.441```4243### Rounding Operations4445Round numbers to specified precision.4647```javascript { .api }48/**49* Computes `number` rounded up to `precision`.50*51* @param {number} number The number to round up52* @param {number} [precision=0] The precision to round up to53* @returns {number} Returns the rounded up number54*/55function ceil(number: number, precision?: number): number;5657/**58* Computes `number` rounded down to `precision`.59*60* @param {number} number The number to round down61* @param {number} [precision=0] The precision to round down to62* @returns {number} Returns the rounded down number63*/64function floor(number: number, precision?: number): number;6566/**67* Computes `number` rounded to `precision`.68*69* @param {number} number The number to round70* @param {number} [precision=0] The precision to round to71* @returns {number} Returns the rounded number72*/73function round(number: number, precision?: number): number;74```7576**Usage Examples:**7778```javascript79import { ceil, floor, round } from "lodash";8081ceil(4.006); // 582ceil(6.004, 2); // 6.0183ceil(6040, -2); // 61008485floor(4.006); // 486floor(0.046, 2); // 0.0487floor(4060, -2); // 40008889round(4.006); // 490round(4.006, 2); // 4.0191round(4060, -2); // 410092```9394### Statistical Operations9596Find maximum and minimum values.9798```javascript { .api }99/**100* Computes the maximum value of `array`. If `array` is empty or falsey,101* `undefined` is returned.102*103* @param {Array} array The array to iterate over104* @returns {*} Returns the maximum value105*/106function max(array: Array): any;107108/**109* This method is like `max` except that it accepts `iteratee` which is110* invoked for each element in `array` to generate the criterion by which111* the value is ranked.112*113* @param {Array} array The array to iterate over114* @param {Function|Object|string} [iteratee] The iteratee invoked per element115* @returns {*} Returns the maximum value116*/117function maxBy(array: Array, iteratee?: Function|Object|string): any;118119/**120* Computes the minimum value of `array`. If `array` is empty or falsey,121* `undefined` is returned.122*123* @param {Array} array The array to iterate over124* @returns {*} Returns the minimum value125*/126function min(array: Array): any;127128/**129* This method is like `min` except that it accepts `iteratee` which is130* invoked for each element in `array` to generate the criterion by which131* the value is ranked.132*133* @param {Array} array The array to iterate over134* @param {Function|Object|string} [iteratee] The iteratee invoked per element135* @returns {*} Returns the minimum value136*/137function minBy(array: Array, iteratee?: Function|Object|string): any;138```139140**Usage Examples:**141142```javascript143import { max, maxBy, min, minBy } from "lodash";144145max([4, 2, 8, 6]); // 8146max([]); // undefined147148const objects = [{ 'n': 1 }, { 'n': 2 }];149maxBy(objects, 'n'); // { 'n': 2 }150maxBy(objects, obj => obj.n); // { 'n': 2 }151152min([4, 2, 8, 6]); // 2153min([]); // undefined154155minBy(objects, 'n'); // { 'n': 1 }156minBy(objects, obj => obj.n); // { 'n': 1 }157```158159### Summation Operations160161Calculate sums and means.162163```javascript { .api }164/**165* Computes the sum of the values in `array`.166*167* @param {Array} array The array to iterate over168* @returns {number} Returns the sum169*/170function sum(array: Array): number;171172/**173* This method is like `sum` except that it accepts `iteratee` which is174* invoked for each element in `array` to generate the value to be summed.175*176* @param {Array} array The array to iterate over177* @param {Function|Object|string} [iteratee] The iteratee invoked per element178* @returns {number} Returns the sum179*/180function sumBy(array: Array, iteratee?: Function|Object|string): number;181182/**183* Computes the mean of the values in `array`.184*185* @param {Array} array The array to iterate over186* @returns {number} Returns the mean187*/188function mean(array: Array): number;189```190191**Usage Examples:**192193```javascript194import { sum, sumBy, mean } from "lodash";195196sum([4, 2, 8, 6]); // 20197198const objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];199sumBy(objects, 'n'); // 20200sumBy(objects, obj => obj.n); // 20201202mean([4, 2, 8, 6]); // 5203mean([]); // NaN204```205206### Number Constraints207208Constrain numbers within bounds.209210```javascript { .api }211/**212* Clamps `number` within the inclusive `lower` and `upper` bounds.213*214* @param {number} number The number to clamp215* @param {number} [lower] The lower bound216* @param {number} upper The upper bound217* @returns {number} Returns the clamped number218*/219function clamp(number: number, lower?: number, upper: number): number;220```221222**Usage Examples:**223224```javascript225import { clamp } from "lodash";226227clamp(-10, -5, 5); // -5228clamp(10, -5, 5); // 5229clamp(3, -5, 5); // 3230231// With only upper bound232clamp(10, 5); // 5233clamp(-10, 5); // -10234```235236### Range Checking237238Check if numbers are within ranges.239240```javascript { .api }241/**242* Checks if `n` is between `start` and up to, but not including, `end`. If243* `end` is not specified, it's set to `start` with `start` then set to `0`.244* If `start` is greater than `end` the params are swapped to support245* negative ranges.246*247* @param {number} number The number to check248* @param {number} [start=0] The start of the range249* @param {number} end The end of the range250* @returns {boolean} Returns `true` if `number` is in the range, else `false`251*/252function inRange(number: number, start?: number, end: number): boolean;253```254255**Usage Examples:**256257```javascript258import { inRange } from "lodash";259260inRange(3, 2, 4); // true261inRange(4, 8); // true (0 to 8)262inRange(4, 2); // false (0 to 2)263inRange(2, 2); // false (0 to 2, not including 2)264inRange(1.2, 2); // true (0 to 2)265inRange(5.2, 4); // false (0 to 4)266inRange(-3, -2, -6); // true (range is -6 to -2)267```268269### Random Number Generation270271Generate random numbers within specified ranges.272273```javascript { .api }274/**275* Produces a random number between the inclusive `lower` and `upper` bounds.276* If only one argument is provided a number between `0` and the given number277* is returned. If `floating` is `true`, or either `lower` or `upper` are278* floats, a floating-point number is returned instead of an integer.279*280* @param {number} [lower=0] The lower bound281* @param {number} [upper=1] The upper bound282* @param {boolean} [floating] Specify returning a floating-point number283* @returns {number} Returns the random number284*/285function random(lower?: number, upper?: number, floating?: boolean): number;286```287288**Usage Examples:**289290```javascript291import { random } from "lodash";292293random(0, 5); // an integer between 0 and 5294random(5); // an integer between 0 and 5295random(5, true); // a floating-point number between 0 and 5296random(1.2, 5.2); // a floating-point number between 1.2 and 5.2297298// Examples of possible outputs:299random(0, 5); // 3300random(1.2, 5.2); // 3.7429301random(5, true); // 2.718302```