# Math

Juttle Math module provides functions and constants for mathematical operations.

## Math constants

Juttle supports these Math constants as well as Number constants. As all exports from Math module, they can be referenced without an import directive in a Juttle program.

Constant Description
Math.E Euler's constant, the base of natural logarithms, approximately 2.718
Math.LN10 The natural logarithm of 10, approximately 2.302
Math.LN2 The natural logarithm of 2, approximately 0.693
Math.LOG2E The base 2 logarithm of Euler's constant, approximately 1.442
Math.Log10E The base 10 logarithm of Euler's constant, approximately 0.434
Math.PI Pi, the ratio of the circumference of a circle to its diameter, approximately 3.14159
Math.SQRT1_2 The square root of 1/2, approximately 0.707
Math.SQRT2 The square root of 2, approximately 1.414

Example

``````emit
| put area = Math.round(Math.PI * Math.pow(radius, 2))
``````

## Math.abs

Return the absolute value of a number.

``````Math.abs(number)
``````

Example

``````emit -limit 1
| put step1 = 3.1415, step2 = 0.5772
| put delta = Math.abs(step2 - step1)
``````

## Math.acos

Return the arccosine of a specified number, in radians.

``````Math.acos(x)
``````
Parameter Description Required?
`x` A number between -1 and 1 Yes

## Math.asin

Return the arcsine of a specified number, in radians.

``````Math.asin(x)
``````
Parameter Description Required?
`x` A number between -1 and 1 Yes

## Math.atan

Return the arctangent of a specified number, in radians.

``````Math.atan(x)
``````
Parameter Description Required?
`x` A number Yes

## Math.atan2

Return the arctangent of the quotient of two numbers, in radians.

``````Math.atan2(y,x)
``````
Parameter Description Required?
`y` A number representing the Y coordinate of a point Yes
`x` A number representing the X coordinate of a point Yes `Note:` The Y coordinate must be specified before the X coordinate.

## Math.ceil

Return the smallest integer greater than or equal to a specified number.

``````Math.ceil(number, digits)
``````
Parameter Description Required?
`digits` The number of significant digits to be used. If 0, the value is rounded to the nearest integer; if greater than 0, the value is rounded to the specified number of decimal places; if less than 0, the value is rounded to the left of the decimal point. No

Example

``````function get_integer(x) {
return (x >= 0) ? Math.floor(x) : Math.ceil(x);
}
emit -limit 1
| put a = get_integer(3.1415), b = get_integer(-0.5772)
| view table
``````

## Math.cos

Return the cosine of a specified number.

``````Math.cos(x)
``````
Parameter Description Required?
`x` A number, in radians Yes

## Math.exp

Return ex, where x is a specified number and e is Euler's constant, the base of the natural logarithms.

``````Math.exp(x)
``````
Parameter Description Required?
`x` A number Yes

## Math.floor

Return the largest integer less than or equal to a number.

``````Math.floor(number, digits)
``````
Parameter Description Required?
`digits` The number of significant digits to be used. If 0, the value is rounded to the nearest integer; if greater than 0, the value is rounded to the specified number of decimal places; if less than 0, the value is rounded to the left of the decimal point. No

Example

``````function get_integer(x) {
return (x >= 0) ? Math.floor(x) : Math.ceil(x);
}
emit -limit 1
| put a = get_integer(3.1415), b = get_integer(-0.5772)
| view table
``````

## Math.log

Return the natural logarithm of the specified number.

``````Math.log(x)
``````
Parameter Description Required?
`x` A number Yes

## Math.max

Return the largest of one or more numbers.

``````Math.max(x[, y[, .. ]])
``````

Example

``````const max = Math.max(Math.random(), Math.random(), Math.random());
emit -limit 1
| put max = max
| view table

``````

## Math.min

Return the smallest of one or more numbers.

``````Math.min(x[, y[, .. ]])
``````

Example

``````emit -limit 1
| put a = 1, b = 2, c = 3, d = Math.min(a, b, c)
| view table
``````

## Math.random

Return a floating-point, pseudo-random number in the range [0, 1); that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.

``````Math.random()
`````` `Note:` The implementation selects the initial seed to the random number generation algorithm. Use Math.seed to choose your own.

Example: Return a random integer between 1 and max

``````function randomInt(max) {
return Math.ceil(Math.random() * max);
}

emit -limit 10
| put r = randomInt(10)
| view table

``````

Example: Throw rock, paper, or scissors at random

``````const paper = 'PAPER';
const rock = 'ROCK';
const scissors = 'SCISSORS';

function compute_result(you_threw, i_threw){
if(you_threw == i_threw){
return "A DRAW!!";
}
if(you_threw == paper){
if(i_threw == rock){
return "You have defeated me.";
}
else{
return "The victory is mine!";
}
}
if(you_threw == rock){
if(i_threw == paper){
return "I win again!";
}
else{
return "You have bested me once more.";
}
}
if(you_threw == scissors){
if(i_threw == paper){
}
else{
return "My superior talent has given me the victory.";
}
}
// If we get here without returning, then we didn't have a pair of arguments
// that we recognize.
return "I'm not sure what happened there.";
}

function what_did_i_throw(){
var rando = Math.random();
if(rando < .33){
return rock;
}
if(rando >= .33 && rando < .67){
return paper;
}
if(rando >= .67){
return scissors;
}
}

input selection :dropdown
-items [rock, paper, scissors]
-default rock;

emit -limit 1
| put you_threw = selection,
i_threw = what_did_i_throw(),
result = compute_result(you_threw, i_threw)
| view table
-title "The duel"
-columnOrder "time", "you_threw", "i_threw", "result"
``````

## Math.pow

Return base to the exponent power, that is, baseexponent.

``````Math.pow(base, exponent)
``````

Example

``````emit -limit 5
| put c = count(), power = Math.pow(2, c)
| keep power
| view table -title "Powers of two"
``````

## Math.round

Return the value of a number rounded to the nearest integer.

``````Math.round(number, digits)
``````
Parameter Description Required?
`digits` The number of significant digits to be used. If 0, the value is rounded to the nearest integer; if greater than 0, the value is rounded to the specified number of decimal places; if less than 0, the value is rounded to the left of the decimal point. No

Example

``````emit -limit 1
| put a = Math.round(3.1415), b = Math.round(-0.5772), c = Math.round(1.5), d = Math.round(42)
| view table
``````

## Math.seed

Initialize the random number generator to a known value. Subsequent calls to Math.random will always generate the same pseudo-random sequence for a given seed value

``````Math.seed(seed)
``````
Parameter Description Required?
`seed` Random number generator seed value. A number. Yes

Example: Do something predictably random

``````const ignored = Math.seed(42);
emit -limit 1
| put pick_a_number = Math.random()
| put lucky_guess = 0.0016341939679719736
| put amazed = (pick_a_number == lucky_guess)
``````

## Math.sin

Return the sine of the specified number.

``````Math.sin(x)
``````
Parameter Description Required?
`x` A number, in radians Yes

## Math.sqrt

Return the positive square root of a number.

``````Math.sqrt(number)
``````

Example: find numbers with whole square roots

``````emit -from :0: -limit 100
| put a = count()
| put b = Math.floor(Math.sqrt(a))
| uniq b
``````

## Math.tan

Return the tangent of the specified number.

``````Math.tan(x)
``````
Parameter Description Required?
`x` A number, in radians Yes