Category talk:Wren-math: Difference between revisions

Content added Content deleted
(→‎Source code: Added segmented sieve method.)
(→‎Source code: Removed all methods which are now in core library as well as type aliases. Added Int.isSquare and Int.isCube methods.)
Line 7: Line 7:
static e { 2.71828182845904523536 } // base of natural logarithms
static e { 2.71828182845904523536 } // base of natural logarithms
static phi { 1.6180339887498948482 } // golden ratio
static phi { 1.6180339887498948482 } // golden ratio
static tau { 1.6180339887498948482 } // synonym for phi
static ln2 { 0.69314718055994530942 } // natural logarithm of 2
static ln2 { 0.69314718055994530942 } // natural logarithm of 2
static ln10 { 2.30258509299404568402 } // natural logarithm of 10
static ln10 { 2.30258509299404568402 } // natural logarithm of 10


// Special values.
// Log function.
static inf { 1/0 } // positive infinity
static ninf { (-1)/0 } // negative infinity
static nan { 0/0 } // nan

// Returns the base 'e' exponential of 'x'
static exp(x) { e.pow(x) }

// Log functions.
static log2(x) { x.log/ln2 } // Base 2 logarithm
static log10(x) { x.log/ln10 } // Base 10 logarithm
static log10(x) { x.log/ln10 } // Base 10 logarithm


Line 36: Line 26:
static radians(d) { d * Num.pi / 180}
static radians(d) { d * Num.pi / 180}
static degrees(r) { r * 180 / Num.pi }
static degrees(r) { r * 180 / Num.pi }

// Returns the cube root of 'x'.
static cbrt(x) { (x >= 0) ? x.pow(1/3) : -(-x).pow(1/3) }


// Returns the square root of 'x' squared + 'y' squared.
// Returns the square root of 'x' squared + 'y' squared.
Line 59: Line 46:
}
}
}
}

// Return the minimum and maximum of 'x' and 'y'.
static min(x, y) { (x < y) ? x : y }
static max(x, y) { (x > y) ? x : y }


// Round away from zero.
// Round away from zero.
Line 107: Line 90:
// Maximum safe integer = 2^53 - 1.
// Maximum safe integer = 2^53 - 1.
static maxSafe { 9007199254740991 }
static maxSafe { 9007199254740991 }

// Returns whether or not 'n' is a perfect square.
static isSquare(n) {
var s = n.sqrt.floor
return s * s == n
}

// Returns whether or not 'n' is a perfect cube.
static isCube(n) {
var c = n.cbrt.truncate
return c * c * c == n
}


// Returns the greatest common divisor of 'x' and 'y'.
// Returns the greatest common divisor of 'x' and 'y'.
Line 532: Line 527:
return itob_(btoi_(b1) ^ btoi_(b2))
return itob_(btoi_(b1) ^ btoi_(b2))
}
}
}</lang>
}

// Type aliases for classes in case of any name clashes with other modules.
var Math_Math = Math
var Math_Int = Int
var Math_Nums = Nums
var Math_Boolean = Boolean
</lang>