Programming Language Comparison: Mathematics

Strings Mathematics Operators and Symbols General (Control Flow/Debugging) Dates Objects New Features Timelines Note: UFL: Universal Function Library, a set of around 100-300 standard functions for all programming languages. Languages do not have to implement anything exactly to the UFL specification, it is a guide. See lower down for further details. UFL: Abs AutoHotkey: Abs C++: abs [also: labs/fabs] C#: Math.Abs Excel: ABS Excel VBA: Abs Java: Math.abs JavaScript: Math.abs Kotlin: Math.abs PHP: abs Python: abs [also: math.fabs] Swift: abs [also: fabs] UFL: Sign [e.g. returns 1/0/-1, -0.0 returns -0.0, NaN returns NaN] AutoHotkey: ___ C++: ___ [can use: vNum <=> 0] C#: Math.Sign Excel: SIGN Excel VBA: Sgn [note: 'Sgn', not 'Sign'] Java: Math.signum JavaScript: Math.sign Kotlin: kotlin.math.sign [also: Math.signum] PHP: ___ [can use: vNum <=> 0] Python: ___ Swift: signum [e.g. vNum.signum()] UFL: SignPsvNgv [returns -1 if negative or -0.0, returns 1 if positive or 0.0 or 0] AutoHotkey: ___ C++: ___ [can use: std::copysign(1.0, vNum)] C#: ___ Excel: ___ Excel VBA: ___ Java: [can use: Math.copySign(1.0, vNum)] JavaScript: ___ Kotlin: ___ [can use: (1.0).withSign(vNum)][requires: import kotlin.math.withSign] PHP: ___ Python: ___ [can use: copysign(1, vNum)][e.g. copysign(1, 0) and copysign(1, -0.0) return 1.0 and -1.0 respectively] Swift: ___ [can use: vSign = ((vNum).sign == .plus) ? 1 : -1][can use: copysign(1.0, vNum)] UFL: SignBit [(or IsNgv) returns 1 if negative or -0.0, returns 0 if positive or 0.0 or 0] AutoHotkey: ___ C++: std::signbit C#: ___ Excel: ___ Excel VBA: ___ Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: ___ Swift: ___ [can use: vSign = ((vNum).sign == .plus) ? 0 : 1] UFL: Pow AutoHotkey: ___ [can use: **] C++: pow C#: Math.Pow Excel: POWER [can use: ^] Excel VBA: WorksheetFunction.Power [can use: ^] Java: Math.pow JavaScript: Math.pow [can use: **] Kotlin: Math.pow PHP: pow [can use: **] Python: pow [also: math.pow/operator.pow][WARNING: math.pow treats (converts) inputs as floats (rather than maintain any integers)][can use: **] Swift: pow UFL: Sqrt/(Cbrt) AutoHotkey: Sqrt/___ C++: sqrt/cbrt C#: Math.Sqrt/Math.Cbrt Excel: SQRT/___ Excel VBA: Sqr/___ [note: 'Sqr', not 'Sqrt'] Java: Math.sqrt/Math.cbrt JavaScript: Math.sqrt/Math.cbrt Kotlin: Math.sqrt/Math.cbrt PHP: sqrt/___ Python: math.sqrt/math.cbrt Swift: sqrt/cbrt [also: squareRoot] UFL: Round [round to the nearest integer (or round to n decimal places)] AutoHotkey: Round C++: round C#: Math.Round Excel: ROUND Excel VBA: Round Java: ___ [WARNING: Math.round/Math.rint (to nearest even integer in tiebreaks) both give unusual results, particularly when handling tiebreaks e.g. 0.5][note: solutions are possible with toBigDecimal/String.format/DecimalFormat using RoundingMode.HALF_UP and/or Locale.ENGLISH] JavaScript: Math.round [also: toFixed] Kotlin: ___ [WARNING: kotlin.math.round/roundToLong/roundToInt/Math.Round all give unusual results, particularly when handling tiebreaks e.g. 0.5][note: solutions are possible with toBigDecimal/String.format/DecimalFormat using RoundingMode.HALF_UP and/or Locale.ENGLISH] PHP: round Python: round Swift: round [also: nearbyint][also: rint (to nearest even integer in tiebreaks)] UFL: Ceil/Floor [round up towards +infinity, round down towards -infinity] AutoHotkey: Ceil/Floor C++: ceil/floor C#: Math.Ceiling/Math.Floor Excel: CEILING.PRECISE/FLOOR.PRECISE [also (for floor): INT][note: a formula for Ceil: -INT(-A1)][WARNING: Excel's INT function works like a floor function, it always rounds down (logically it should work just like Excel's TRUNC function)][WARNING: Excel's CEILING/FLOOR functions have unusual behaviour] Excel VBA: WorksheetFunction.Ceiling_Precise/WorksheetFunction.Floor_Precise ASLO Int [note: a formula for Ceil: -Int(-vNum)][WARNING: Excel VBA's Int function works like a floor function, it always rounds down (logically it should work just like Excel VBA's Fix function)] Java: Math.ceil/Math.floor JavaScript: Math.ceil/Math.floor Kotlin: Math.ceil/Math.floor PHP: ceil/floor Python: math.ceil/math.floor Swift: ceil/floor UFL: Trunc [truncate: round towards zero (remove the fractional part)] AutoHotkey: Integer C++: trunc C#: Math.Truncate Excel: TRUNC [WARNING: Excel's INT function works like a floor function, it always rounds down (logically it should work just like Excel's TRUNC function)] Excel VBA: Fix [WARNING: Excel VBA's Int function works like a floor function, it always rounds down (logically it should work just like Excel VBA's Fix function)] Java: intValue [can use: vDouble.intValue] JavaScript: Math.trunc Kotlin: kotlin.math.truncate PHP: intval Python: int [also: math.trunc] Swift: trunc [also: Int] UFL: Frac [number: get the fractional part (e.g. 2.9 to 0.9, e.g. -2.9 to -0.9), e.g. x = trunc(x) + frac(x)] AutoHotkey: ___ [can use: Mod(vNum, 1)][also: vNum-Integer(vNum)] C++: ___ [can use: fmod(vNum, 1)][note: throws if use %] C#: ___ [can use: vNum % 1] Excel: ___ [can use: MOD(A1,1)][also: A1-TRUNC(A1)] Excel VBA: ___ [can use: vNum-Fix(vNum)][WARNING: don't use Mod operator, Excel VBA's Mod rounds inputs to the nearest integer] Java: ___ [can use: vNum % 1] JavaScript: ___ [can use: vNum % 1][also: vNum-Math.trunc(vNum)] Kotlin: ___ [can use: vNum % 1] PHP: ___ [can use: fmod($vNum, 1)][note: throws if use %] Python: ___ [can use: math.fmod(vNum, 1)][WARNING: don't use %, Python's % is FloorMod, e.g. -2.9 % 1 = 0.1, not -0.9] Swift: ___ [can use: vNum.truncatingRemainder(dividingBy:1)][note: throws if use %] UFL: Number [to number (integer/float)] AutoHotkey: Number C++: ___ C#: ___ Excel: N [e.g. N(A1)] Excel VBA: ___ Java: ___ JavaScript: Number Kotlin: ___ PHP: ___ Python: ___ Swift: ___ UFL: Integer [or ToInteger][to integer] AutoHotkey: Integer C++: std::stoi [e.g. std::stoi(vText)] C#: Int32.Parse [e.g. Int32.Parse(vText)][also: Int64.Parse(vText)] Excel: ___ [can use: TRUNC(N(A1))] Excel VBA: CInt [also: CLng/CLngLng/CLngPtr] Java: Integer.parseInt [e.g. Integer.parseInt(vText)] JavaScript: parseInt [e.g. parseInt(vText)] Kotlin: toInt [e.g. vText.toInt()] PHP: intval [e.g. intval(vText)][also: vNum = (int)vText] Python: int [e.g. int(vText)] Swift: Int [e.g. vNum = Int(vText) ?? 0] UFL: Float [or ToFloat/ToDouble][to float] AutoHotkey: Float C++: std::stod [e.g. std::stod(vText)] C#: Double.Parse [e.g. Double.Parse(vNum)] Excel: N [e.g. N(A1)] Excel VBA: CDbl [also: CSng/CDec/CVar] Java: Double.parseDouble [e.g. Double.parseDouble(vText)] JavaScript: parseFloat Kotlin: toDouble [e.g. vNum.toDouble()] PHP: floatval [also: vNum = (float)vText][note: '(double)' is an alias for '(float)'] Python: float Swift: Double [e.g. vNum = Double(vText) ?? 0.0] UFL: Log/Ln/Log10/(Log2) [where Log allows you to specify the base] AutoHotkey: ___/Ln/Log/___ C++: ___/log/log10/log2 C#: Math.Log/Math.Log/Math.Log10/Math.Log2 Excel: LOG/LN/LOG10/___ [note: LOG: default base is 10] Excel VBA: WorksheetFunction.Log/Log/WorksheetFunction.Log10/___ [note: WorksheetFunction.Log: default base is 10] Java: ___/Math.log/Math.log10/___ JavaScript: ___/Math.log/Math.log10/Math.log2 Kotlin: kotlin.math.log/kotlin.math.ln/kotlin.math.log10/kotlin.math.log2 [also: Math.log/Math.log10/Math.log2][note: kotlin.math.log: a base must be specified] PHP: ___/log/log10/___ Python: math.log/math.log10/math.log/math.log2 [note: math.log: default base is e (e=2.718...)] Swift: ___/log/log10/log2 UFL: Exp AutoHotkey: Exp C++: exp C#: Math.Exp Excel: EXP Excel VBA: Exp Java: Math.exp JavaScript: Math.exp Kotlin: Math.exp PHP: exp Python: math.exp Swift: exp UFL: Clamp AutoHotkey: ___ C++: ___ C#: Math.Clamp Excel: MEDIAN [note: with 3 parameters specified (in any order)] Excel VBA: WorksheetFunction.Median [note: with 3 parameters specified (in any order)] Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: ___ Swift: ___ [note: Swift has clamped for ranges] UFL: Between/(BetweenUntil) [Between: a <= x <= b, BetweenUntil: a <= x < b] AutoHotkey: ___/___ C++: ___/___ C#: ___/___ Excel: ___/___ Excel VBA: ___/___ Java: ___/___ JavaScript: ___/___ Kotlin: ___/___ PHP: ___/___ Python: ___/___ Swift: ___/___ UFL: Range/(RangeUntil) [Range: a <= x <= b (inclusive end), RangeUntil: a <= x < b (exclusive end)] AutoHotkey: ___/___ C++: ___/___ C#: ___/Enumerable.Range [WARNING: Enumerable.Range: exclusive end][can use: .. (WARNING: .. operator: exclusive end)] Excel: ___/___ Excel VBA: ___/___ Java: IntStream.rangeClosed/IntStream.range [WARNING: IntStream.range: exclusive end][also: LongStream.rangeClosed/LongStream.range] JavaScript: ___/___ Kotlin: rangeTo/rangeUntil [can use: .. (inclusive end) and ..< (exclusive end)][can use: downTo (like .. but in reverse order)] PHP: range/___ Python: ___/range [WARNING: range: exclusive end] Swift: ___/___ [can use: ... (3 dots) (inclusive end) and ..< (exclusive end)][note: can omit 1 value to create a one-sided range] UFL: Mod/FloorMod [Mod: C-style modulo (output sign based on 1st param), floor modulo (output sign based on 2nd param) (e.g. R uses %%)] AutoHotkey: Mod/___ C++: fmod/___ [can use (C-style mod): %] C#: ___/___ [can use (C-style mod): %] Excel: ___/MOD [WARNING: Excel's MOD is a floor mod, Excel VBA's Mod operator is a C-style mod] Excel VBA: ___/___ [can use (C-style mod): Mod operator e.g. a Mod b][WARNING: unlike the Excel sheet function MOD, Mod in VBA rounds floats to integers] Java: ___/Math.floorMod [can use (C-style mod): %] JavaScript: ___/___ [can use (C-style mod): %] Kotlin: ___/___ [can use (C-style mod): %] PHP: fmod/___ [can use (C-style mod): %] Python: math.fmod/operator.mod [can use (floor mod): % (WARNING: floor not C-style)][can use: divmod (returns quotient and remainder as a tuple)] Swift: fmod/___ [can use (C-style mod): %] UFL: IntDiv/FloorDiv [IntDiv (TruncDiv): divide then round towards zero (remove the fractional part) (e.g. Dart uses ~/), FloorDiv: divide then round down (towards -infinity)] AutoHotkey: ___/___ [can use (trunc. div.) (AHK v2): // (WARNING: differs from Python)] C++: ___/___ C#: ___/___ Excel: QUOTIENT/___ Excel VBA: WorksheetFunction.Quotient/___ Java: ___/Math.floorDiv JavaScript: ___/___ Kotlin: ___/Math.floorDiv PHP: intdiv/___ [can use (trunc. div.): (int)(a / b)] Python: ___/operator.floordiv [can use (floor. div.): //][can use: divmod (returns quotient and remainder as a tuple)] Swift: ___/___ UFL: (Hypot) AutoHotkey: ___ C++: hypot C#: Math.Hypot Excel: ___ Excel VBA: ___ Java: Math.hypot JavaScript: Math.hypot Kotlin: Math.hypot PHP: hypot Python: math.hypot Swift: hypot UFL: (Fact) [factorial] AutoHotkey: ___ C++: ___ C#: ___ Excel: FACT Excel VBA: WorksheetFunction.Fact Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: math.factorial Swift: ___ UFL: (IsPrime) [is prime] AutoHotkey: ___ C++: ___ C#: ___ Excel: ___ Excel VBA: ___ Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: ___ Swift: ___ UFL: (Triang) [triangular number][see also: Range.ToArray, Array.Sum, Sum] AutoHotkey: ___ C++: ___ C#: ___ Excel: ___ Excel VBA: ___ Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: ___ Swift: ___ UFL: (Fib) [Fibonacci number] AutoHotkey: ___ C++: ___ C#: ___ Excel: ___ Excel VBA: ___ Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: ___ Swift: ___ UFL: (DegToRad)/(RadToDeg) AutoHotkey: ___/___ C++: ___/___ C#: ___/___ Excel: RADIANS/DEGREES Excel VBA: WorksheetFunction.Radians/WorksheetFunction.Degrees Java: Math.toRadians/Math.toDegrees JavaScript: ___/___ Kotlin: Math.toRadians/Math.toDegrees PHP: deg2rad/rad2deg Python: math.radians/math.degrees Swift: ___/___ UFL: Cos/Sin/Tan AutoHotkey: Cos/Sin/Tan C++: cos/sin/tan C#: Math.Cos/Math.Sin/Math.Tan Excel: COS/SIN/TAN Excel VBA: Cos/Sin/Tan Java: Math.cos/Math.sin/Math.tan JavaScript: Math.cos/Math.sin/Math.tan Kotlin: Math.cos/Math.sin/Math.tan PHP: cos/sin/tan Python: math.cos/math.sin/math.tan Swift: cos/sin/tan UFL: ACos/ASin/ATan AutoHotkey: ACos/ASin/ATan C++: acos/asin/atan C#: Math.Acos/Math.Asin/Math.Atan Excel: ACOS/ASIN/ATAN Excel VBA: WorksheetFunction.Acos/WorksheetFunction.Asin/Atn [note: 'Atn', not 'Atan'] Java: Math.acos/Math.asin/Math.atan JavaScript: Math.acos/Math.asin/Math.atan Kotlin: Math.acos/Math.asin/Math.atan PHP: acos/asin/atan Python: math.acos/math.asin/math.atan Swift: acos/asin/atan UFL: ATan2 AutoHotkey: ATan2 [note: parameters: y, x] C++: atan2 [note: parameters: y, x] C#: Math.Atan2 [note: parameters: y, x] Excel: ATAN2 [WARNING: parameters: x, y (uncommon param order)] Excel VBA: WorksheetFunction.Atan2 [WARNING: parameters: x, y (uncommon param order)] Java: Math.atan2 [note: parameters: y, x] JavaScript: Math.atan2 [note: parameters: y, x] Kotlin: Math.atan2 [note: parameters: y, x] PHP: atan2 [note: parameters: y, x] Python: math.atan2 [note: parameters: y, x] Swift: atan2 [note: parameters: y, x][WARNING: there may be some extant or deprecated functions that use the x, y order][requires: import Foundation] UFL: (CosDeg)/(SinDeg)/(TanDeg)/(ACosDeg)/(ASinDeg)/(ATanDeg)/(ATan2Deg) [degree-versions of trigonometric functions][e.g. TanDeg takes degrees, e.g. ATanDeg returns degrees] AutoHotkey: ___/___/___/___/___/___/___ C++: ___/___/___/___/___/___/___ C#: ___/___/___/___/___/___/___ Excel: ___/___/___/___/___/___/___ Excel VBA: ___/___/___/___/___/___/___ Java: ___/___/___/___/___/___/___ JavaScript: ___/___/___/___/___/___/___ Kotlin: ___/___/___/___/___/___/___ PHP: ___/___/___/___/___/___/___ Python: ___/___/___/___/___/___/___ Swift: ___/___/___/___/___/___/___ UFL: DecToBase/BaseToDec AutoHotkey: ___/___ C++: itoa/std::stoi [note: and variants e.g. _ui64tow for DecToBase, and _wcstoi64/atoi/strtol for BaseToDec] C#: Convert.ToString/Convert.ToInt32 [WARNING: Convert.ToString/Convert.ToInt32 only handle bases 2/8/10/16] Excel: BASE/DECIMAL [note: both added in Excel 2013] Excel VBA: WorksheetFunction.Base/WorksheetFunction.Decimal [also (for dec2hex): Hex][also (for hex2dec): CLngLng/CDbl/CDec with '&H' (not '0x') prefix (probably avoid CLng/CInt/Val/Abs, e.g. test with '&HF'/'&HFFFFFFFF'/'&HFFFFFFFF')] Java: Integer.toString/Integer.parseInt [also: String.format] JavaScript: toString/parseInt Kotlin: Integer.toString/Integer.parseInt [also: vInt.toString()] PHP: base_convert/base_convert Python: ___/int [can use (dec2base): bin/hex] Swift: String/strtol [note: and functions for other types: strtoul/strtoll/strtoull] UFL: (DecToBin)/(BinToDec) AutoHotkey: ___/___ C++: ___/___ [can use: itoa/atoi] C#: ___/___ [can use: Convert.ToString/Convert.ToInt32] Excel: DEC2BIN/BIN2DEC Excel VBA: WorksheetFunction.Dec2Bin/WorksheetFunction.Bin2Dec Java: Integer.toBinaryString/___ [can use (bin2dec): Integer.parseInt] JavaScript: ___/___ [can use: toString/parseInt] Kotlin: Integer.toBinaryString/___ [can use (bin2dec): Integer.parseInt] PHP: decbin/bindec Python: bin/___ [can use (bin2dec): int] Swift: ___/___ [can use: String/strtol] UFL: (DecToHex)/(HexToDec) AutoHotkey: ___/___ C++: ___/___ [can use: itoa/atoi] C#: ___/___ [can use: Convert.ToString/Convert.ToInt32] Excel: DEC2HEX/HEX2DEC Excel VBA: WorksheetFunction.Dec2Hex/WorksheetFunction.Hex2Dec [also (for dec2hex): Hex][also (for hex2dec): CLngLng/CDbl/CDec with '&H' (not '0x') prefix (probably avoid CLng/CInt/Val/Abs, e.g. test with '&HF'/'&HFFFFFFFF'/'&HFFFFFFFF')] Java: Integer.toHexString/___ [can use (hex2dec): Integer.parseInt] JavaScript: ___/___ [can use: toString/parseInt] Kotlin: Integer.toHexString/___ [can use (hex2dec): Integer.parseInt] PHP: dechex/hexdec Python: hex/___ [can use (hex2dec): int] Swift: ___/___ [can use: String/strtol] UFL: Combin/Permut [combinations without repetition, permutations without repetition] AutoHotkey: ___/___ C++: ___/___ C#: ___/___ Excel: COMBIN/PERMUT [also: COMBINA/PERMUTATIONA (combinations/permutations with repetitions)] Excel VBA: WorksheetFunction.Combin/WorksheetFunction.Permut Java: ___/___ JavaScript: ___/___ Kotlin: ___/___ PHP: ___/___ Python: math.comb/math.perm Swift: ___/___ UFL: GCD [or HCF][greatest common divisor (highest common factor)][could potentially handle n params/an array] AutoHotkey: ___ C++: ___ C#: BigInteger.GreatestCommonDivisor Excel: GCD Excel VBA: WorksheetFunction.Gcd Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: math.gcd Swift: ___ UFL: LCM [lowest common multiple][could potentially handle n params/an array] AutoHotkey: ___ C++: ___ C#: ___ Excel: LCM Excel VBA: WorksheetFunction.Lcm Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: math.lcm Swift: ___ UFL: RandomInt [random(a,b) inclusive][ideally with a separate RandomSeed function] AutoHotkey: Random [e.g. Random(vMin, vMax)] C++: ___ [can use (inclusive end): vMin + rand() % (vMax-vMin+1)][beforehand: srand((time(0)))][WARNING: biased results][WARNING: RAND_MAX can be small] C#: Next [can use (inclusive end): oRand.Next(vMin, vMax+1)][beforehand: Random oRand = new Random()][also (exclusive end): oRand.Next(vMin, vMax)] Excel: RANDBETWEEN [e.g. RANDBETWEEN(1,6)] Excel VBA: WorksheetFunction.RandBetween [e.g. WorksheetFunction.RandBetween(vMin, vMax)] Java: ___ [can use (inclusive end): vMin + oRand.nextInt(vMax-vMin+1)][beforehand: Random oRand = new Random()][requires: import java.util.Random] JavaScript: ___ [can use (inclusive end): vMin + Math.floor(Math.random()*(vMax-vMin+1))] Kotlin: random [e.g. (vMin..vMax).random()][also: nextInt and other 'nextXXX' methods] PHP: rand [e.g. rand(vMin, vMax)][also: random_int()] Python: random.randint [e.g. random.randint(vMin, vMax)][also (exclusive end): random.randrange e.g. random.randrange(vMin, vMax+1)] Swift: random [e.g. Int.random(in:vMin...vMax)] UFL: RandomIntDemo [e.g. roll a die (dice): an integer between 1 and 6 inclusive] AutoHotkey: Random [e.g. Random(1, 6)] C++: ___ [e.g. 1 + rand() % 6] C#: Next [e.g. oRand.Next(1, 6+1)] Excel: RANDBETWEEN [e.g. RANDBETWEEN(1,6)] Excel VBA: WorksheetFunction.RandBetween [e.g. WorksheetFunction.RandBetween(1, 6)] Java: ___ [e.g. 1 + oRand.nextInt(6)] JavaScript: ___ [e.g. 1 + Math.floor(Math.random()*6)] Kotlin: random [e.g. (1..6).random()] PHP: rand [e.g. rand(1, 6)] Python: random.randint [e.g. random.randint(1, 6)][also (exclusive end): e.g. random.randrange(1, 6+1)] Swift: random [e.g. Int.random(in:1...6)] UFL: RandomFloat [ideally with a separate RandomSeed function][e.g. a number between 0 (inclusive) and 1 (exclusive)] AutoHotkey: Random [e.g. Random()][WARNING: 'generally': 'For floating point numbers, the maximum value is generally excluded.'] C++: ___ [can use: (double)rand()/(double)RAND_MAX][beforehand: srand((time(0)))][WARNING: can return 1.0][WARNING: RAND_MAX can be small] C#: NextDouble [e.g. oRand.NextDouble()][beforehand: Random oRand = new Random()] Excel: RAND [e.g. RAND()] Excel VBA: Rnd [e.g. Rnd()] Java: Math.random [e.g. Math.random()] JavaScript: Math.random [e.g. Math.random()] Kotlin: Math.random [e.g. Math.random()][also: nextDouble and other 'nextXXX' methods] PHP: ___ [can use: $vNum = mt_rand(0, mt_getrandmax()-1) / mt_getrandmax()][also: lcg_value() (WARNING: 0.0 to 1.0 inclusive)] Python: random.random [e.g. random.random()] Swift: random [e.g. Double.random(in:0..<1)] UFL: (NumCompare) AutoHotkey: ___ C++: ___ [can use: <=>] C#: CompareTo Excel: ___ Excel VBA: ___ Java: Integer.compare [also: Double.compare] JavaScript: ___ Kotlin: compareValues [also: compareValuesBy] PHP: ___ [can use: <=>] Python: ___ Swift: ___ UFL: VerCompare [VerCompare ALSO IPCompare][e.g. compare '1.2.3.4' against '1.2', and/or handle SemVer][e.g. compare IP addresses (IPv4 and/or IPv6)] AutoHotkey: VerCompare C++: ___ C#: ___ Excel: ___ Excel VBA: ___ Java: ___ JavaScript: ___ Kotlin: ___ PHP: version_compare Python: ___ Swift: ___ UFL: PopCount32/PopCount64 AutoHotkey: ___/___ C++: ___/___ C#: BitOperations.PopCount/BitOperations.PopCount Excel: ___/___ Excel VBA: ___/___ Java: Integer.bitCount/Long.bitCount JavaScript: ___/___ Kotlin: Integer.bitCount/java.lang.Long.bitCount PHP: ___/___ [note: PHP has gmp_popcount] Python: int.bit_count/int.bit_count [e.g. vNum.bit_count()] Swift: ___/___ UFL: (RotateLeft)/(RotateRight) AutoHotkey: ___/___ C++: ___/___ C#: BitOperations.RotateLeft/BitOperations.RotateRight Excel: ___/___ Excel VBA: ___/___ Java: Integer.rotateLeft/Integer.rotateRight JavaScript: ___/___ Kotlin: Integer.rotateLeft/Integer.rotateRight PHP: ___/___ Python: ___/___ Swift: ___/___ UFL: (BitAnd)/(BitOr)/(BitXor)/(BitNot) [And/Or/Xor could potentially handle n params/an array] AutoHotkey: ___/___/___/___ [can use: & | ^ ~] C++: ___/___/___/___ [can use: & | ^ ~] C#: ___/___/___/___ [can use: & | ^ ~] Excel: BITAND/BITOR/BITXOR/___ [note: added in Excel 2013] Excel VBA: WorksheetFunction.Bitand/WorksheetFunction.Bitor/WorksheetFunction.Bitxor/___ [can use: And Or Xor Not] Java: ___/___/___/___ [can use: & | ^ ~] JavaScript: ___/___/___/___ [can use: & | ^ ~] Kotlin: ___/___/___/___ [can use: and or xor inv] PHP: ___/___/___/___ [can use: & | ^ ~][also: some related functions: gmp_and/gmp_or/gmp_xor variant_and/variant_not] Python: operator.and_/operator.or_/operator.xor/operator.invert [also (for 'not'): operator.inv][can use: & | ^ ~] Swift: ___/___/___/___ [can use: & | ^ ~] UFL: (BitShiftLeft) AutoHotkey: ___ [can use: <<] C++: ___ [can use: <<] C#: ___ [can use: <<] Excel: BITLSHIFT [note: added in Excel 2013][can use (operators): And Or * / ^ (note: And and Or are bitwise for integer operands)] Excel VBA: WorksheetFunction.Bitlshift [note: added in Excel 2013] Java: ___ [can use: <<] JavaScript: ___ [can use: <<] Kotlin: ___ [can use (operator): shl] PHP: ___ [can use: <<] Python: operator.lshift [can use: <<] Swift: ___ [can use: <<] UFL: (BitShiftRight)/(BitShiftRightLogical) AutoHotkey: ___/___ [can use: >> >>>] C++: ___/___ [can use: >> (>> is signed/unsigned based on the type)] C#: ___/___ [can use: >> >>>] Excel: BITRSHIFT/___ [note: added in Excel 2013][can use (operators): And Or * / ^ (note: And and Or are bitwise for integer operands)] Excel VBA: WorksheetFunction.Bitrshift/___ [note: added in Excel 2013] Java: ___/___ [can use: >> >>>] JavaScript: ___/___ [can use: >> >>>] Kotlin: ___/___ [can use (operators): shr/ushr][note: ushr throws when used with *unsigned* integers] PHP: ___/___ [can use: >>] Python: operator.rshift/___ [can use: >>] Swift: ___/___ [can use: >>] UFL: MRound [round to the nearest multiple of n] AutoHotkey: ___ C++: ___ C#: ___ Excel: MROUND Excel VBA: WorksheetFunction.MRound Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: ___ Swift: ___ UFL: (Nearest) [return the nearest number: Nearest(vNum, oNums*), e.g. round to the nearest multiple of 5 (an alternative to MRound): Nearest(vNum, vNum-vNum%5, vNum-vNum%5+5)] AutoHotkey: ___ C++: ___ C#: ___ Excel: ___ Excel VBA: ___ Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: ___ Swift: ___ UFL: IsNaN/IsFinite/IsInfinite AutoHotkey: ___/___/___ C++: isnan/isfinite/isinf [i.e. C99 isnan/isfinite/isinf macros] C#: IsNaN/IsFinite/IsInfinity [note: 'IsInfinity' ends with 'y', not 'e'] Excel: ___/___/___ Excel VBA: ___/___/___ Java: Double.isNan/Double.isFinite/Double.isInfinite JavaScript: isNaN/___/___ Kotlin: isNaN/isFinite/isInfinite PHP: is_nan/is_finite/is_infinite Python: math.isnan/math.isfinite/math.isinf Swift: isnan/isfinite/isinf UFL: TrueDiv [or Div/FloatDiv] AutoHotkey: ___ C++: ___ C#: ___ Excel: ___ Excel VBA: ___ Java: ___ JavaScript: ___ Kotlin: ___ PHP: fdiv Python: operator.truediv Swift: ___ UFL: (NumToRom)/(RomToNum) [Arabic numerals to Roman numerals, and vice versa] AutoHotkey: ___/___ C++: ___/___ C#: ___/___ Excel: ROMAN/ARABIC [note: ARABIC added in Excel 2013] Excel VBA: WorksheetFunction.Roman/WorksheetFunction.Arabic Java: ___/___ JavaScript: ___/___ Kotlin: ___/___ PHP: ___/___ Python: ___/___ Swift: ___/___ UFL: Gamma AutoHotkey: ___ C++: tgamma C#: ___ Excel: GAMMA [note: added in Excel 2013] Excel VBA: WorksheetFunction.Gamma Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: math.gamma Swift: tgamma UFL: Erf AutoHotkey: ___ C++: erf C#: ___ Excel: ERF Excel VBA: WorksheetFunction.Erf Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: math.erf Swift: erf UFL: (Pi) [constant or function] AutoHotkey: ___ [can use: Pi := 4*ATan(1)] C++: ___ [can use: std::numbers::pi][also: M_PI, 4*atan(1), acos(-1), atan2(0, -1)] C#: ___ [can use: Pi = Math.PI] Excel: PI [e.g. PI()] Excel VBA: Application.WorksheetFunction.Pi [can use: Pi = 4*Atn(1)] Java: ___ [can use: Pi = Math.PI] JavaScript: ___ [can use: Pi = Math.PI] Kotlin: ___ [can use: Pi = kotlin.math.PI] PHP: pi [e.g. Pi = pi()][also: Pi = M_PI] Python: ___ [can use: Pi = math.pi] Swift: ___ [can use: Pi = Double.pi][also: Float.pi, CGFloat.pi] UFL: Max/Min [functions listed here can handle more than 2 numbers][see also: Array.Max/Array.Min] AutoHotkey: Max/Min C++: ___/___ [note: fmax/fmin handle 2 numbers] C#: Max/Min [e.g. oArray.Max()/oArray.Min()][note: Math.Max/Math.Min handle 2 numbers] Excel: MAX/MIN Excel VBA: WorksheetFunction.Max/WorksheetFunction.Min Java: Collections.max/Collections.min [note: Math.max/Math.min handle 2 numbers][can use: oIntStream.max/oDoubleStream.max and oIntStream.min/oDoubleStream.min] JavaScript: Math.max/Math.min Kotlin: ___/___ [note: Math.max/Math.min handle 2 numbers] PHP: max/min Python: max/min Swift: max/min UFL: (Sum) [sum n numbers][see also: Array.Sum] AutoHotkey: ___ C++: ___ C#: ___ Excel: SUM Excel VBA: WorksheetFunction.Sum Java: sum [can use: oIntStream.sum and oDoubleStream.sum] JavaScript: ___ [note: Math.addExact handles 2 numbers] Kotlin: sum [can use: oArray.sum] PHP: ___ Python: sum Swift: ___ UFL: (Product) [multiply n numbers][see also: Array.Product] AutoHotkey: ___ C++: ___ C#: ___ Excel: PRODUCT Excel VBA: WorksheetFunction.Product Java: ___ JavaScript: ___ [note: Math.multiplyExact handles 2 numbers] Kotlin: ___ PHP: ___ Python: math.prod Swift: ___ UFL: (Mean) [see also: Array.Mean] AutoHotkey: ___ C++: ___ C#: ___ Excel: AVERAGE Excel VBA: WorksheetFunction.Average Java: ___ [can use: oIntStream.summaryStatistics/oDoubleStream.summaryStatistics] JavaScript: ___ Kotlin: ___ PHP: ___ Python: statistics.mean Swift: ___ UFL: (Median) [see also: Array.Median] AutoHotkey: ___ C++: ___ C#: ___ Excel: MEDIAN Excel VBA: WorksheetFunction.Median Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: statistics.median Swift: ___ UFL: (Mode) [note: return all mode values][see also: Array.Mode] AutoHotkey: ___ C++: ___ C#: ___ Excel: MODE.MULT [WARNING: MODE: returns the first mode, and fails if the first mode has frequency 1] Excel VBA: WorksheetFunction.Mode_Mult [WARNING: WorksheetFunction.Mode: requires an array, returns the first mode, and fails if the first mode has frequency 1] Java: ___ JavaScript: ___ Kotlin: ___ PHP: ___ Python: statistics.multimode Swift: ___

The Universal Function Library Project: Details

Notes: Function names, parameter names, parameter order, and functionality recommendations, are approximate suggestions; there may be good arguments for alternatives. Function names in parentheses are regarded as somewhat or considerably lower priority. Many example functions are available at JEE.js, the function library that powers this website. Author's Intro: Functions that I would like to see more widely in particular are: FloorMod. For FloorMod(a,b), where b is positive, the return value is always 0 or positive. It is extremely convenient. It is worth implementing both as a function and as an operator (e.g. %%), to enable the easier migration of code between programming languages. Sign and Clamp are highly convenient, reduce bugs, and make the intention clear. Some languages use **, some use Pow. I would recommend implementing both, to enable the easier migration of code between programming languages, and to avoid the rearranging of formulas which is a major source of bugs. I would recommended TrueDiv/IntDiv/FloorDiv functions, and operators (e.g. / ~/ and //, or an alternative for floor division if // is unavailable), to enable the easier migration of code between programming languages, and to avoid terrible/catastrophic bugs. VerCompare/IPCompare functions are highly convenient. PopCount functions are very useful for algorithms, and could be more widely available. They are often used in tight loops, so having them built-in is advantageous performance-wise. An MRound function can be quite convenient (round to the nearest multiple of n). Of the more advanced functions, Combin/Permut, Gamma and Erf are worth considering, for their general utility. And just between you and me... Between: The 'between' debate has raged for decades: Introducing chained operators can allow 'a <= x <= b', but typically creates other unreadable/unpredictable/dangerous combinations of operators. Doing '(a <= x) && (x <= b)', repeats x. And x could be long or an expression. I have found it very convenient to have a Between function e.g. 'Between(x, a, b)'. A BetweenUntil function could also be implemented. It avoids the repeating of expressions, it makes it easy to find instances of 'between' comparisons in code, it avoids questions of how to order the operands. Its power can be increased by accepting a comparator function. UFL: Abs/Sign [note: return 1/0/-1, -0.0 returns -0.0, NaN returns NaN] recommendation: Sign: maintain the type of the number recommendation: Sign: for floats, maintain positive/negative 0 UFL: Pow UFL: Sqrt/(Cbrt) recommendation: having a Cbrt function helps maintain exact values across different programming languages UFL: Round [round to the nearest integer (or round to n decimal places)] UFL: Ceil/Floor [round up towards +infinity, round down towards -infinity] UFL: Trunc [truncate: round towards zero (remove the fractional part)] UFL: Log/Ln/Log10/(Log2) [where Log allows you to specify the base] recommendation: having additional Log functions helps maintain exact values across different programming languages UFL: Exp UFL: Max/Min [functions listed here can handle more than 2 numbers] UFL: Clamp UFL: Between/(BetweenUntil) [Between: a <= x <= b, BetweenUntil: a <= x < b] format: Between(Needle, Bound1, Bound2, Comparator:=unset) recommendation: accepting a comparator function could allow for strings/version number strings/IP address strings to be compared recommendation: SAFETY: it may be better to have separate (numerical) Between and StrBetween functions (alternatively, the function could throw if no comparator is specified, and a non-number is passed) UFL: Mod/FloorMod UFL: IntDiv/FloorDiv [divide then round towards zero (remove the fractional part), divide round down towards -infinity] UFL: (Hypot) UFL: (Fact) [factorial] UFL: (IsPrime)/(Triang)/(Fib) [is prime/triangular number/Fibonacci number] UFL: (DegToRad)/(RadToDeg) UFL: (Sum)/(Product) UFL: (Mean)/(Median)/(Mode) [Mode: return all mode values] recommendation: implement a version of Mode that returns all the mode values as an array UFL: Cos/Sin/Tan/(CosDeg)/(SinDeg)/(TanDeg) recommendation: 'Deg' variants make it simpler to do calculations in degrees UFL: ACos/ASin/ATan/(ACosDeg)/(ASinDeg)/(ATanDeg) recommendation: 'Deg' variants make it simpler to do calculations in degrees UFL: ATan2/(ATan2Deg) recommendation: 'Deg' variants make it simpler to do calculations in degrees UFL: DecToBase/BaseToDec/(DecToBin)/(BinToDec)/(DecToHex)/(HexToDec) recommendation: make it easy to convert between all bases, 2 to 36 recommendation: accepting an object, could facilitate converting between greater bases UFL: Combin/Permut [combinations without repetition, permutations without repetition] recommendation: these can be very awkward to implement as custom code, if not built-in (with 'with/without repetition' options) UFL: GCD/LCM [GCD ALSO HCF] UFL: RandomInt/RandomFloat [ideally with a separate RandomSeed function] UFL: (NumCompare) UFL: VerCompare [VerCompare ALSO IPCompare][e.g. compare '1.2.3.4' against '1.2', and/or handle SemVer][e.g. compare IP addresses (IPv4 and/or IPv6)] recommendation: an Options parameter could determine whether only '1.2.3.4'-style strings are handled, or whether SemVer, IPv4 and/or IPv6 are handled recommendation: VerCompare/IPCompare could be implemented as separate functions UFL: PopCount32/PopCount64 UFL: (RotateLeft)/(RotateRight) UFL: (BitAnd)/(BitOr)/(BitXor)/(BitNot) UFL: (BitShiftLeft)/(BitShiftRight)/(BitShiftRightLogical) UFL: MRound [round to the nearest multiple of n] UFL: (Nearest) [return the nearest number: Nearest(vNum, oNums*), e.g. round to the nearest multiple of 5 (an alternative to MRound): Nearest(vNum, vNum-vNum%5, vNum-vNum%5+5)] recommendation: there are many various considerations for an options parameter or separate functions: e.g. nearest greater/lesser/different number, and nearest number (greater/lesser if tiebreak) UFL: IsNaN/IsFinite/IsInfinite UFL: TrueDiv [or Div/FloatDiv] UFL: Gamma UFL: Erf