Skip to content

Commit

Permalink
Num; add min, max and clamp
Browse files Browse the repository at this point in the history
  • Loading branch information
ruby0x1 committed Dec 3, 2020
1 parent 38f50fe commit 8361217
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
14 changes: 14 additions & 0 deletions doc/site/modules/core/num.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ The binary (base-2) logarithm of the number. Returns `nan` if the base is negati

The exponential `e` (Euler’s number) raised to the number. This: `eⁿ`.

### **min**(other)

Returns the minimum value when comparing this number and `other`.

### **max**(other)

Returns the maximum value when comparing this number and `other`.

### **clamp**(min, max)

Clamps a number into the range of `min` and `max`. If this number is less than min,
`min` is returned. If bigger than `max`, `max` is returned. Otherwise, the number
itself is returned.

### **pow**(power)

Raises this number (the base) to `power`. Returns `nan` if the base is negative.
Expand Down
26 changes: 26 additions & 0 deletions src/vm/wren_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,29 @@ DEF_PRIMITIVE(num_atan2)
RETURN_NUM(atan2(AS_NUM(args[0]), AS_NUM(args[1])));
}

DEF_PRIMITIVE(num_min)
{
double value = AS_NUM(args[0]);
double other = AS_NUM(args[1]);
RETURN_NUM(value <= other ? value : other);
}

DEF_PRIMITIVE(num_max)
{
double value = AS_NUM(args[0]);
double other = AS_NUM(args[1]);
RETURN_NUM(value > other ? value : other);
}

DEF_PRIMITIVE(num_clamp)
{
double value = AS_NUM(args[0]);
double min = AS_NUM(args[1]);
double max = AS_NUM(args[2]);
double result = (value < min) ? min : ((value > max) ? max : value);
RETURN_NUM(result);
}

DEF_PRIMITIVE(num_pow)
{
RETURN_NUM(pow(AS_NUM(args[0]), AS_NUM(args[1])));
Expand Down Expand Up @@ -1325,6 +1348,9 @@ void wrenInitializeCore(WrenVM* vm)
PRIMITIVE(vm->numClass, "floor", num_floor);
PRIMITIVE(vm->numClass, "-", num_negate);
PRIMITIVE(vm->numClass, "round", num_round);
PRIMITIVE(vm->numClass, "min(_)", num_min);
PRIMITIVE(vm->numClass, "max(_)", num_max);
PRIMITIVE(vm->numClass, "clamp(_,_)", num_clamp);
PRIMITIVE(vm->numClass, "sin", num_sin);
PRIMITIVE(vm->numClass, "sqrt", num_sqrt);
PRIMITIVE(vm->numClass, "tan", num_tan);
Expand Down
7 changes: 7 additions & 0 deletions test/core/number/clamp.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var num = 4

System.print(num.clamp(0, 10)) // expect: 4
System.print(num.clamp(0, 1)) // expect: 1
System.print(2.clamp(0, 1)) // expect: 1
System.print((-1).clamp(0, 1)) // expect: 0
System.print((-1).clamp(-20, 0)) // expect: -1
5 changes: 5 additions & 0 deletions test/core/number/min_max.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var num = 4
var num2 = 6

System.print(num.max(num2)) // expect: 6
System.print(num.min(num2)) // expect: 4

0 comments on commit 8361217

Please sign in to comment.