Skip to content

Commit

Permalink
fix(math): maxInt/minInt, maxFloat/minFloat
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Sep 9, 2023
1 parent a41b963 commit f2f13ac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
30 changes: 24 additions & 6 deletions src/lib/buzz_math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,42 @@ export fn bzlog(ctx: *api.NativeCtx) c_int {
return 1;
}

export fn max(ctx: *api.NativeCtx) c_int {
const a_f: f64 = ctx.vm.bz_peek(0).float();
const b_f: f64 = ctx.vm.bz_peek(1).float();
export fn maxFloat(ctx: *api.NativeCtx) c_int {
const a_f = ctx.vm.bz_peek(0).float();
const b_f = ctx.vm.bz_peek(1).float();

ctx.vm.bz_pushFloat(@max(a_f, b_f));

return 1;
}

export fn min(ctx: *api.NativeCtx) c_int {
const a_f: f64 = ctx.vm.bz_peek(0).float();
const b_f: f64 = ctx.vm.bz_peek(1).float();
export fn minFloat(ctx: *api.NativeCtx) c_int {
const a_f = ctx.vm.bz_peek(0).float();
const b_f = ctx.vm.bz_peek(1).float();

ctx.vm.bz_pushFloat(@min(a_f, b_f));

return 1;
}

export fn maxInt(ctx: *api.NativeCtx) c_int {
const a_f = ctx.vm.bz_peek(0).integer();
const b_f = ctx.vm.bz_peek(1).integer();

ctx.vm.bz_pushInteger(@max(a_f, b_f));

return 1;
}

export fn minInt(ctx: *api.NativeCtx) c_int {
const a_f = ctx.vm.bz_peek(0).integer();
const b_f = ctx.vm.bz_peek(1).integer();

ctx.vm.bz_pushInteger(@min(a_f, b_f));

return 1;
}

export fn random(ctx: *api.NativeCtx) c_int {
ctx.vm.bz_pushFloat(std.crypto.random.float(f64));

Expand Down
16 changes: 12 additions & 4 deletions src/lib/math.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ extern fun bzfloor(float n) > int;
extern fun bzlog(float base, float n) > float;

|| @return max of a and b
extern fun max(float a, float b) > float;
extern fun maxFloat(float a, float b) > float;

|| @return min of a and b
extern fun min(float a, float b) > float;
extern fun minFloat(float a, float b) > float;

|| @return max of a and b
extern fun maxInt(int a, int b) > int;

|| @return min of a and b
extern fun minInt(int a, int b) > int;

|| Convert degree to radian
fun rad(float n) > float {
Expand Down Expand Up @@ -72,8 +78,10 @@ export deg;
export bzexp as exp;
export bzfloor as floor;
export bzlog as log;
export max;
export min;
export minFloat;
export maxFloat;
export minInt;
export maxInt;
export pi;
export rad;
export random;
Expand Down

0 comments on commit f2f13ac

Please sign in to comment.