Math
Examples
Filter
console.log(Math.tanh(-1));
// expected output: -0.7615941559557649
console.log(Math.tanh(0));
// expected output: 0
console.log(Math.tanh(Infinity));
// expected output: 1
console.log(Math.tanh(1));
// expected output: 0.7615941559557649
Math.tanh(0); // 0
Math.tanh(Infinity); // 1
Math.tanh(1); // 0.7615941559557649
package main
import (
"fmt"
"gonum.org/v1/gonum/mat"
)
func main() {
// Initialize two matrices, a and b.
a := mat.NewDense(2, 2, []float64{
4, 0,
0, 4,
})
b := mat.NewDense(2, 3, []float64{
4, 0, 0,
0, 0, 4,
})
// Take the matrix product of a and b and place the result in c.
var c mat.Dense
c.Mul(a, b)
// Print the result using the formatter.
fc := mat.Formatted(&c, mat.Prefix(" "), mat.Squeeze())
fmt.Printf("c = %v", fc)
}
Last Run
:
c = ⎡16 0 0⎤
⎣ 0 0 16⎦
// 00000000000000000000000000000001
console.log(Math.clz32(1));
// expected output: 31
// 00000000000000000000000000000100
console.log(Math.clz32(4));
// expected output: 29
// 00000000000000000000001111101000
console.log(Math.clz32(1000));
// expected output: 22
Math.clz32(1); // 31
Math.clz32(1000); // 22
Math.clz32(); // 32
var stuff = [NaN, Infinity, -Infinity, 0, -0, null, undefined, 'foo', {}, []];
stuff.every(n => Math.clz32(n) == 32); // true
Math.clz32(true); // 31
Math.clz32(3.5); // 30
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
console.log(getRandomInt(3));
// expected output: 0, 1 or 2
console.log(getRandomInt(1));
// expected output: 0
console.log(Math.random());
// expected output: a number between 0 and 1
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function difference(a, b) {
return Math.abs(a - b);
}
console.log(difference(3, 5));
// expected output: 2
console.log(difference(5, 3));
// expected output: 2
console.log(difference(1.23456, 7.89012));
// expected output: 6.6555599999999995
Math.abs('-1'); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs(''); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1,2]); // NaN
Math.abs({}); // NaN
Math.abs('string'); // NaN
Math.abs(); // NaN
Computes the least integer value greater than or equal to x.
console.log(Math.ceil(.95));
// expected output: 1
console.log(Math.ceil(4));
// expected output: 4
console.log(Math.ceil(7.004));
// expected output: 8
console.log(Math.ceil(-7.004));
// expected output: -7
Math.ceil(.95); // 1
Math.ceil(4); // 4
Math.ceil(7.004); // 8
Math.ceil(-0.95); // -0
Math.ceil(-4); // -4
Math.ceil(-7.004); // -7
Compute the greatest integer value less than or equal to x.
console.log(Math.floor(5.95));
// expected output: 5
console.log(Math.floor(5.05));
// expected output: 5
console.log(Math.floor(5));
// expected output: 5
console.log(Math.floor(-5.05));
// expected output: -6
Math.floor( 45.95); // 45
Math.floor( 45.05); // 45
Math.floor( 4 ); // 4
Math.floor(-45.05); // -46
Math.floor(-45.95); // -46
Round returns the nearest integer, rounding half away from zero.
console.log(Math.round(0.9));
// expected output: 1
console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05));
// expected output: 6 6 5
console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95));
// expected output: -5 -5 -6
Math.round( 20.49); // 20
Math.round( 20.5 ); // 21
Math.round( 42 ); // 42
Math.round(-20.5 ); // -20
Math.round(-20.51); // -21
console.log(Math.max(1, 3, 2));
// expected output: 3
console.log(Math.max(-1, -3, -2));
// expected output: -1
var array1 = [1, 3, 2];
console.log(Math.max(...array1));
// expected output: 3
Math.max(10, 20); // 20
Math.max(-10, -20); // -10
Math.max(-10, 20); // 20
console.log(Math.min(2, 3, 1));
// expected output: 1
console.log(Math.min(-2, -3, -1));
// expected output: -3
var array1 = [2, 3, 1];
console.log(Math.min(...array1));
// expected output: 1
Computes the remainder after division of one number by another.
package main
import (
"fmt"
"math"
)
func main() {
c := math.Mod(7, 4)
fmt.Printf("%.1f", c)
}
function calcHypotenuse(a, b) {
return(Math.sqrt((a * a) + (b * b)));
}
console.log(calcHypotenuse(3, 4));
// expected output: 5
console.log(calcHypotenuse(5, 12));
// expected output: 13
console.log(calcHypotenuse(0, 0));
// expected output: 0
Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095
Math.sqrt(1); // 1
Math.sqrt(0); // 0
Math.sqrt(-1); // NaN
Math.sqrt(-0); // -0
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Cbrt(27))
}
console.log(Math.exp(0));
// expected output: 1
console.log(Math.exp(1));
// expected output: 2.718281828459 (approximately)
console.log(Math.exp(-1));
// expected output: 0.36787944117144233
console.log(Math.exp(2));
// expected output: 7.38905609893065
Math.exp(-1); // 0.36787944117144233
Math.exp(0); // 1
Math.exp(1); // 2.718281828459045
Computes an implementation-dependent approximation to subtracting 1 from the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms). The result is computed in a way that is accurate even when the value of x is close 0.
console.log(Math.expm1(0));
// expected output: 0
console.log(Math.expm1(1));
// expected output: 1.718281828459045
console.log(Math.expm1(-1));
// expected output: -0.6321205588285577
console.log(Math.expm1(2));
// expected output: 6.38905609893065
Math.expm1(-1); // -0.6321205588285577
Math.expm1(0); // 0
Math.expm1(1); // 1.718281828459045
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("%.2f", math.Exp2(10))
}
console.log(Math.pow(7, 3));
// expected output: 343
console.log(Math.pow(4, 0.5));
// expected output: 2
console.log(Math.pow(7, -2));
// expected output: 0.02040816326530612
// (1/49)
console.log(Math.pow(-7, 0.5));
// expected output: NaN
// simple
Math.pow(7, 2); // 49
Math.pow(7, 3); // 343
Math.pow(2, 10); // 1024
// fractional exponents
Math.pow(4, 0.5); // 2 (square root of 4)
Math.pow(8, 1/3); // 2 (cube root of 8)
Math.pow(2, 0.5); // 1.4142135623730951 (square root of 2)
Math.pow(2, 1/3); // 1.2599210498948732 (cube root of 2)
// signed exponents
Math.pow(7, -2); // 0.02040816326530612 (1/49)
Math.pow(8, -1/3); // 0.5
// signed bases
Math.pow(-7, 2); // 49 (squares are positive)
Math.pow(-7, 3); // -343 (cubes can be negative)
Math.pow(-7, 0.5); // NaN (negative numbers don't have a real square root)
// due to "even" and "odd" roots laying close to each other,
// and limits in the floating number precision,
// negative bases with fractional exponents always return NaN
Math.pow(-7, 1/3); // NaN
Math.log(-1); // NaN, out of range
Math.log(0); // -Infinity
Math.log(1); // 0
Math.log(10); // 2.302585092994046
Compute an implementation-dependent approximation to the natural logarithm of 1 + x. The result is computed in a way that is accurate even when the value of x is close to zero.
console.log(Math.log1p(1));
// expected output: 0.6931471805599453
console.log(Math.log1p(0));
// expected output: 0
console.log(Math.log1p(-1));
// expected output: -Infinity
console.log(Math.log1p(-2));
// expected output: NaN
Math.log1p(1); // 0.6931471805599453
Math.log1p(0); // 0
Math.log1p(-1); // -Infinity
Math.log1p(-2); // NaN
console.log(Math.log2(3));
// expected output: 1.584962500721156
console.log(Math.log2(2));
// expected output: 1
console.log(Math.log2(1));
// expected output: 0
console.log(Math.log2(0));
// expected output: -Infinity
Math.log2(3); // 1.584962500721156
Math.log2(2); // 1
Math.log2(1); // 0
Math.log2(0); // -Infinity
Math.log2(-2); // NaN
Math.log2(1024); // 10
console.log(Math.log10(100000));
// expected output: 5
console.log(Math.log10(2));
// expected output: 0.3010299956639812
console.log(Math.log10(1));
// expected output: 0
console.log(Math.log10(0));
// expected output: -Infinity
Math.log10(2); // 0.3010299956639812
Math.log10(1); // 0
Math.log10(0); // -Infinity
Math.log10(-2); // NaN
Math.log10(100000); // 5
package main
import (
"fmt"
"math"
)
func LogAny(x, b float64) float64 {
return math.Log(x) / math.Log(b)
}
func main() {
fmt.Printf("%.1f", LogAny(27, 3))
}
Last Run
:
3.0
Computes the square root of the sum of squares of its arguments, that is:
console.log(Math.hypot(3, 4));
// expected output: 5
console.log(Math.hypot(5, 12));
// expected output: 13
console.log(Math.hypot(3, 4, 5));
// expected output: 7.0710678118654755
console.log(Math.hypot(-5));
// expected output: 5
Math.hypot(3, 4); // 5
Math.hypot(3, 4, 5); // 7.0710678118654755
Math.hypot(); // 0
Math.hypot(NaN); // NaN
Math.hypot(3, 4, 'foo'); // NaN, +'foo' => NaN
Math.hypot(3, 4, '5'); // 7.0710678118654755, +'5' => 5
Math.hypot(-3); // 3, the same as Math.abs(-3)
package main
import (
"fmt"
"math"
)
func main() {
c := math.Erf(1.0)
fmt.Printf("%.6f", c)
}
package main
import (
"fmt"
"math"
)
func main() {
c := math.Erfc(1.0)
fmt.Printf("%.6f", c)
}
package main
import (
"fmt"
"math"
)
func main() {
c := math.Erfcinv(1.5)
fmt.Printf("%.6f", c)
}
package main
import (
"fmt"
"math"
)
func main() {
c := math.Erfinv(0.5)
fmt.Printf("%.6f", c)
}
package main
import (
"fmt"
"math"
)
func main() {
c := math.Gamma(10.0)
fmt.Printf("%.1f", c)
}
package main
import (
"fmt"
"math"
)
func main() {
not_a_number := math.NaN()
fmt.Println(not_a_number)
}
Computes either a positive or negative 1, indicating the sign of a number passed into the argument. If the number argument is 0, it will return 0.
console.log(Math.sign(3));
// expected output: 1
console.log(Math.sign(-3));
// expected output: -1
console.log(Math.sign(0));
// expected output: 0
console.log(Math.sign('-3'));
// expected output: -1
Math.sign(3); // 1
Math.sign(-3); // -1
Math.sign('-3'); // -1
Math.sign(0); // 0
Math.sign(-0); // -0
Math.sign(NaN); // NaN
Math.sign('foo'); // NaN
Math.sign(); // NaN
Returns whether float x is an IEEE 754 “not-a-number” value.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.IsNaN(math.Log(-1.0)))
}
package main
import (
"fmt"
"math"
)
func main() {
posInf := math.Inf(1)
fmt.Println(math.IsInf(posInf, 1))
}
Last Run
:
true
package main
import (
"fmt"
"math"
)
func main() {
posInf := math.Inf(-1)
fmt.Println(math.IsInf(posInf, -1))
}
Last Run
:
true
def fibonacci( n )
return n if ( 0..1 ).include? n
( fibonacci( n - 1 ) + fibonacci( n - 2 ) )
end
puts fibonacci( 5 )