Skip to content

Commit

Permalink
Adds Floor, Ceiling and Round.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix "xq" Queißner committed Oct 21, 2021
1 parent af46af7 commit d8dde20
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
12 changes: 12 additions & 0 deletions documentation/standard-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ Global constant containing the number _pi_.

Compares `a` and `b` with a certain `delta`. Returns `true` when `abs(a-b) < delta`.

### ``Floor(x): number`

Rounds `x` towards negative infinity.

### ``Ceiling(x): number`

Rounds `x` towards positive infinity.

### ``Round(x): number`

Rounds `x` to the closest integer.

### `Sin(a): number`, `Cos(a): number`, `Tan(a): number`

Trigonometric functions, all use radians.
Expand Down
24 changes: 24 additions & 0 deletions src/library/libraries/stdlib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,30 @@ const sync_functions = struct {
return lola.runtime.Value.initBoolean(std.math.fabs(a - b) < delta);
}

fn Floor(env: *const lola.runtime.Environment, context: lola.runtime.Context, args: []const lola.runtime.Value) !lola.runtime.Value {
_ = env;
_ = context;
if (args.len != 1)
return error.InvalidArgs;
return lola.runtime.Value.initNumber(std.math.floor(try args[0].toNumber()));
}

fn Ceiling(env: *const lola.runtime.Environment, context: lola.runtime.Context, args: []const lola.runtime.Value) !lola.runtime.Value {
_ = env;
_ = context;
if (args.len != 1)
return error.InvalidArgs;
return lola.runtime.Value.initNumber(std.math.ceil(try args[0].toNumber()));
}

fn Round(env: *const lola.runtime.Environment, context: lola.runtime.Context, args: []const lola.runtime.Value) !lola.runtime.Value {
_ = env;
_ = context;
if (args.len != 1)
return error.InvalidArgs;
return lola.runtime.Value.initNumber(std.math.round(try args[0].toNumber()));
}

fn Sin(env: *const lola.runtime.Environment, context: lola.runtime.Context, args: []const lola.runtime.Value) !lola.runtime.Value {
_ = env;
_ = context;
Expand Down
31 changes: 31 additions & 0 deletions src/test/stdlib.lola
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,37 @@ var delta = 0.00001;

Expect(DeltaEqual(0.000001, 0.000002, delta));

ExpectEqual(Floor(2.0), 2.0);
ExpectEqual(Floor(3.0), 3.0);
ExpectEqual(Floor(2.5), 2.0);
ExpectEqual(Floor(2.999), 2.0);
ExpectEqual(Floor(3.1), 3.0);
ExpectEqual(Floor(0.0), 0.0);
ExpectEqual(Floor(-0.5), -1.0);
ExpectEqual(Floor(-1.0), -1.0);

ExpectEqual(Ceiling(2.0), 2.0);
ExpectEqual(Ceiling(3.0), 3.0);
ExpectEqual(Ceiling(2.5), 3.0);
ExpectEqual(Ceiling(2.999), 3.0);
ExpectEqual(Ceiling(3.1), 4.0);
ExpectEqual(Ceiling(0.0), 0.0);
ExpectEqual(Ceiling(-0.5), 0.0);
ExpectEqual(Ceiling(-1.0), -1.0);


ExpectEqual(Round(2.0), 2.0);
ExpectEqual(Round(3.0), 3.0);
ExpectEqual(Round(2.5), 3.0);
ExpectEqual(Round(3.5), 4.0);
ExpectEqual(Round(2.999), 3.0);
ExpectEqual(Round(3.1), 3.0);
ExpectEqual(Round(0.0), 0.0);
ExpectEqual(Round(-0.4), 0.0);
ExpectEqual(Round(-0.5), -1.0);
ExpectEqual(Round(-1.0), -1.0);
ExpectEqual(Round(-1.5), -2.0);

Expect(DeltaEqual(Sin(Pi), 0.0, delta));
Expect(DeltaEqual(Cos(Pi), -1.0, delta));
Expect(DeltaEqual(Tan(Pi), 0.0, delta));
Expand Down

0 comments on commit d8dde20

Please sign in to comment.