Category talk:Wren-check: Difference between revisions

Content added Content deleted
m (→‎Source code: Added quotes to 'lang' attribute.)
(→‎Source code: Added SafeInt and SafeInts classes.)
Line 104: Line 104:
static posInt(name, value) { int(name, value, 1) }
static posInt(name, value) { int(name, value, 1) }
static nonNegInt(name, value) { int(name, value, 0) }
static nonNegInt(name, value) { int(name, value, 0) }
static safeInt(name, value) { int(name, value, Num.minSafeInteger, Num.maxSafeInteger) }
static safeInt(name, value) { int(name, value, "a 'safe' integer") }
static posSafeInt(name, value) { int(name, value, 1, Num.maxSafeInteger) }
static posSafeInt(name, value) { int(name, value, "a positive 'safe' integer" }
static nonNegSafeInt(name, value) { int(name, value, 0, Num.maxSafeInteger) }
static nonNegSafeInt(name, value) { int(name, value, "a non-negative 'safe' integer" }


static nonZeroSafeInt(name, value) {
static nonZeroSafeInt(name, value) {
Line 297: Line 297:


static ok(condition) { ok(condition, "Condition is not met.") }
static ok(condition) { ok(condition, "Condition is not met.") }
}

/*
SafeInt is a class containing self-explanatory static methods to check
integer arithmetic is 'safe' i.e. within the ± 2^53 limit.
*/
class SafeInt {
static add(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i + j
Check.safeInt("Result", res)
return res
}

static sub(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i - j
Check.safeInt("Result", res)
return res
}

static mul(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i * j
Check.safeInt("Result", res)
return res
}

static pow(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i.pow(j)
Check.safeInt("Result", res)
return res
}
}

/* SafeInts checks the safety of arithmetic operations for a list of integers. */
class SafeInts {
static sum(a) { a.reduce(0) { |acc, x| SafeInt.add(acc, x) } }
static prod(a) { a.reduce(1) { |acc, x| SafeInt.mul(acc, x) } }
}
}