Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
fmin.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2026 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
15
16#include <math.h>
17
18case OP_FMIN: {
19 Value b = pop_value(vm);
20 Value a = pop_value(vm);
21 if (!((a.type == VAL_INT || a.type == VAL_FLOAT) && (b.type == VAL_INT || b.type == VAL_FLOAT))) {
22 fprintf(stderr, "Runtime type error: FMIN expects numbers, got %s and %s\n",
23 value_type_name(a.type), value_type_name(b.type));
24 exit(1);
25 }
26 double da = (a.type == VAL_FLOAT) ? a.d : (double)a.i;
27 double db = (b.type == VAL_FLOAT) ? b.d : (double)b.i;
28 double r = fmin(da, db);
30 if (!isnan(r) && !isinf(r) && r >= (double)INT64_MIN && r <= (double)INT64_MAX) {
31 int64_t ii = (int64_t)r;
32 if ((double)ii == r)
33 out = make_int(ii);
34 else
36 } else {
38 }
42 break;
43}
Value a
Definition add.c:37
Value out
Definition apop.c:38
uint32_t b
Definition band.c:32
uint32_t r
Definition band.c:33
@ OP_FMIN
Definition bytecode.h:274
double da
Definition fmax.c:26
double db
Definition fmax.c:27
free_value(a)
push_value(vm, out)
Tagged union representing a Fun value.
Definition value.h:68
Value make_float(double v)
Construct a Value representing a double-precision float.
Definition value.c:64
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_INT
Definition value.h:51
@ VAL_FLOAT
Definition value.h:58
#define fprintf
Definition vm.c:200
#define exit(code)
Definition vm.c:230