Fun
0.41.5
The programming language that makes You have fun
Main Page
Data Structures
Files
File List
Globals
Loading...
Searching...
No Matches
src
vm
math
fmax.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
18
case
OP_FMAX
: {
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: FMAX 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
= fmax(
da
,
db
);
29
Value
out
;
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
35
out
=
make_float
(
r
);
36
}
else
{
37
out
=
make_float
(
r
);
38
}
39
free_value
(
a
);
40
free_value
(
b
);
41
push_value
(vm,
out
);
42
break
;
43
}
a
Value a
Definition
add.c:37
out
Value out
Definition
apop.c:38
b
uint32_t b
Definition
band.c:32
r
uint32_t r
Definition
band.c:33
OP_FMAX
@ OP_FMAX
Definition
bytecode.h:275
free_value
free_value(a)
da
double da
Definition
fmax.c:26
push_value
push_value(vm, out)
db
double db
Definition
fmax.c:27
Value
Tagged union representing a Fun value.
Definition
value.h:68
make_float
Value make_float(double v)
Construct a Value representing a double-precision float.
Definition
value.c:64
make_int
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition
value.c:51
VAL_INT
@ VAL_INT
Definition
value.h:51
VAL_FLOAT
@ VAL_FLOAT
Definition
value.h:58
fprintf
#define fprintf
Definition
vm.c:200
exit
#define exit(code)
Definition
vm.c:230
Generated on
for Fun by
1.16.1