docs » hs.math

Various helpful mathematical functions

This module includes, and is a superset of the built-in Lua math library so it is safe to do something like the following in your own code and still have access to both libraries:

local math = require("hs.math")
local n = math.sin(math.minFloat) -- works even though they're both from different libraries

The documentation for the math library can be found at http://www.lua.org/manual/5.3/ or from the Hammerspoon console via the help command: help.lua.math. This includes the following functions and variables:

Additional functions and values that are specific to Hammerspoon which provide expanded math support are documented here.

API Overview

API Documentation

Constants

minFloat
Signature hs.math.minFloat
Type Constant
Description

Smallest positive floating point number representable in Hammerspoon

Notes
  • Because specifying a delay of 0 to hs.timer.doAfter results in the event not triggering, use this value to indicate that the action should occur as soon as possible after the current code block has completed execution.
Source extensions/math/math.lua line 96

Functions

isFinite
Signature hs.math.isFinite(value) -> boolean
Type Function
Description

Returns whether or not the value is a finite number

Parameters
  • value - the value to be tested
Returns
  • true if the value is a finite number, or false otherwise
Notes
Source extensions/math/math.lua line 78
isInfinite
Signature hs.math.isInfinite(value) -> 1, -1, false
Type Function
Description

Returns whether or not the value is the mathematical equivalent of either positive or negative "Infinity"

Parameters
  • value - the value to be tested
Returns
  • 1 if the value is equivalent to positive infinity, -1 if the value is equivalent to negative infinity, or false otherwise.
Notes
  • This function specifically checks if the value is equivalent to positive or negative infinity --- it does not do type checking. If value is not a numeric value (e.g. a string), it cannot be equivalent to positive or negative infinity and will return false.
  • Because lua treats any value other than nil and false as true, the return value of this function can be safely used in conditionals when you don't care about the sign of the infinite value.
Source extensions/math/math.lua line 61
isNaN
Signature hs.math.isNaN(value) -> boolean
Type Function
Description

Returns whether or not the value is the mathematical equivalent of "Not-A-Number"

Parameters
  • value - the value to be tested
Returns
  • true if value is equal to the mathematical "value" of NaN, or false otherwise
Notes
  • Mathematical NaN represents an impossible value, usually the result of a calculation, yet is still considered within the domain of mathematics. The most common case is the result of n / 0 as division by 0 is considered undefined or "impossible".
  • This function specifically checks if the value is NaN --- it does not do type checking. If value is not a numeric value (e.g. a string), it cannot be equivalent to NaN and this function will return false.
Source extensions/math/math.lua line 44
randomFloat
Signature hs.math.randomFloat() -> number
Type Function
Description

Returns a random floating point number between 0 and 1

Parameters
  • None
Returns
  • A random number between 0 and 1
Source extensions/math/libmath.m line 6
randomFromRange
Signature hs.math.randomFromRange(start, end) -> integer
Type Function
Description

Returns a random integer between the start and end parameters

Parameters
  • start - A number to start the range, must be greater than or equal to zero
  • end - A number to end the range, must be greater than zero and greater than start
Returns
  • A randomly chosen integer between start and end
Source extensions/math/libmath.m line 26