Skip to content

Commit 0af10e6

Browse files
committed
Add Round, Ceiling and Floor functions
1 parent 976d1aa commit 0af10e6

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ The rest of the variable name may also contains digits.
7575
| IF | IF(n < 5, 5, n < 25, 25, 50) | Returns the second argument if the first argument is true, returns the fourth argument if the third argument is true, and so on. Otherwise, returns the last argument. Odd number of arguments required |
7676
| MIN | MIN(0, 1, ...) | Returns smallest argument |
7777
| MAX | MAX(0, 1, ...) | Returns largest argument |
78+
| ROUND | ROUND(1.1) | Rounds a number |
79+
| CEILING | CEILING(1.1) | Round number up |
80+
| FLOOR | FLOOR(1.1) | Rounds number down |
7881

7982
## How to Install
8083
Minimal Unity Version is 2020.1.

Runtime/ExpressionParser.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ select MakeUnary(Negate, fact)
167167
protected abstract T GreaterThanOrEqual(T a, T b);
168168
protected abstract bool IsTrue(T v);
169169

170+
protected abstract T Round(T v);
171+
protected abstract T Floor(T v);
172+
protected abstract T Ceiling(T v);
173+
170174
private T Not(T v) => IsTrue(v) ? False : True;
171175
private T And(T a, T b) => IsTrue(a) ? b : a;
172176
private T Or(T a, T b) => IsTrue(a) ? a : b;
@@ -186,6 +190,15 @@ private ExprBuilder MakeFunction(string name, List<ExprBuilder> parameterBuilder
186190
case "NOT":
187191
return MakeFunction1(Not);
188192

193+
case "ROUND":
194+
return MakeFunction1(Round);
195+
196+
case "CEILING":
197+
return MakeFunction1(Ceiling);
198+
199+
case "FLOOR":
200+
return MakeFunction1(Floor);
201+
189202
case "MIN":
190203
return MakeFunctionFold(Min);
191204

Runtime/FloatExpressionParser.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Globalization;
23
using UnityEngine;
34

@@ -27,5 +28,8 @@ protected override float Parse(string input) =>
2728
protected override float GreaterThan(float a, float b) => a > b ? 1 : 0;
2829
protected override float GreaterThanOrEqual(float a, float b) => a >= b ? 1 : 0;
2930
protected override bool IsTrue(float v) => !Mathf.Approximately(v, 0);
31+
protected override float Round(float v) => (float) Math.Round(v);
32+
protected override float Ceiling(float v) => (float) Math.Ceiling(v);
33+
protected override float Floor(float v) => (float) Math.Floor(v);
3034
}
3135
}

Tests/ParserTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ public class ParserTests
4141
[TestCase("NOT(-5)", ExpectedResult = 0)]
4242
[TestCase("NOT(5)", ExpectedResult = 0)]
4343
[TestCase("NOT(NOT(0))", ExpectedResult = 0)]
44+
//ROUND
45+
[TestCase("ROUND(0)", ExpectedResult = 0)]
46+
[TestCase("ROUND(0.1)", ExpectedResult = 0)]
47+
[TestCase("ROUND(0.9)", ExpectedResult = 1)]
48+
[TestCase("ROUND(-0.1)", ExpectedResult = 0)]
49+
[TestCase("ROUND(-0.9)", ExpectedResult = -1)]
50+
//CEILING
51+
[TestCase("CEILING(0)", ExpectedResult = 0)]
52+
[TestCase("CEILING(0.1)", ExpectedResult = 1)]
53+
[TestCase("CEILING(0.9)", ExpectedResult = 1)]
54+
[TestCase("CEILING(-0.1)", ExpectedResult = 0)]
55+
[TestCase("CEILING(-0.9)", ExpectedResult = 0)]
56+
//FLOOR
57+
[TestCase("FLOOR(0)", ExpectedResult = 0)]
58+
[TestCase("FLOOR(0.1)", ExpectedResult = 0)]
59+
[TestCase("FLOOR(0.9)", ExpectedResult = 0)]
60+
[TestCase("FLOOR(-0.1)", ExpectedResult = -1)]
61+
[TestCase("FLOOR(-0.9)", ExpectedResult = -1)]
4462
// MIN
4563
[TestCase("MIN(5)", ExpectedResult = 5)]
4664
[TestCase("MIN(0, 0)", ExpectedResult = 0)]

0 commit comments

Comments
 (0)