Category talk:Wren-check: Difference between revisions

→‎Source code: Added SafeInt and SafeInts classes.
m (→‎Source code: Added quotes to 'lang' attribute.)
(→‎Source code: Added SafeInt and SafeInts classes.)
Line 104:
static posInt(name, value) { int(name, value, 1) }
static nonNegInt(name, value) { int(name, value, 0) }
static safeInt(name, value) { int(name, value, Num.minSafeInteger,"a Num.maxSafeInteger'safe' integer") }
static posSafeInt(name, value) { int(name, value, 1,"a Num.maxSafeInteger)positive 'safe' integer" }
static nonNegSafeInt(name, value) { int(name, value, 0,"a Num.maxSafeInteger)non-negative 'safe' integer" }
 
static nonZeroSafeInt(name, value) {
Line 297:
 
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) } }
}
 
9,476

edits