More math opcode Fun. (0.37.43)
This commit is contained in:
parent
194c06e4aa
commit
c062b685e1
18 changed files with 501 additions and 2 deletions
31
src/vm/math/cos.c
Normal file
31
src/vm/math/cos.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-01-03
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file cos.c
|
||||
* @brief Implements the OP_COS opcode using C99 math.h cos().
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
case OP_COS: {
|
||||
Value v = pop_value(vm);
|
||||
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
|
||||
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
|
||||
double r = cos(x);
|
||||
push_value(vm, make_float(r));
|
||||
free_value(v);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: COS expects number, got %s\n", value_type_name(v.type));
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
31
src/vm/math/exp.c
Normal file
31
src/vm/math/exp.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-01-03
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file exp.c
|
||||
* @brief Implements the OP_EXP opcode using C99 math.h exp().
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
case OP_EXP: {
|
||||
Value v = pop_value(vm);
|
||||
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
|
||||
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
|
||||
double r = exp(x);
|
||||
push_value(vm, make_float(r));
|
||||
free_value(v);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: EXP expects number, got %s\n", value_type_name(v.type));
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
36
src/vm/math/log.c
Normal file
36
src/vm/math/log.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-01-03
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file log.c
|
||||
* @brief Implements the OP_LOG opcode using C99 math.h log() (natural logarithm).
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
case OP_LOG: {
|
||||
Value v = pop_value(vm);
|
||||
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
|
||||
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
|
||||
if (x <= 0.0) {
|
||||
/* Domain error: return NaN to signal invalid input */
|
||||
push_value(vm, make_float(NAN));
|
||||
} else {
|
||||
double r = log(x);
|
||||
push_value(vm, make_float(r));
|
||||
}
|
||||
free_value(v);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: LOG expects number, got %s\n", value_type_name(v.type));
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
35
src/vm/math/log10.c
Normal file
35
src/vm/math/log10.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-01-03
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file log10.c
|
||||
* @brief Implements the OP_LOG10 opcode using C99 math.h log10().
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
case OP_LOG10: {
|
||||
Value v = pop_value(vm);
|
||||
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
|
||||
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
|
||||
if (x <= 0.0) {
|
||||
push_value(vm, make_float(NAN));
|
||||
} else {
|
||||
double r = log10(x);
|
||||
push_value(vm, make_float(r));
|
||||
}
|
||||
free_value(v);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: LOG10 expects number, got %s\n", value_type_name(v.type));
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
31
src/vm/math/sin.c
Normal file
31
src/vm/math/sin.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-01-03
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file sin.c
|
||||
* @brief Implements the OP_SIN opcode using C99 math.h sin().
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
case OP_SIN: {
|
||||
Value v = pop_value(vm);
|
||||
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
|
||||
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
|
||||
double r = sin(x);
|
||||
push_value(vm, make_float(r));
|
||||
free_value(v);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: SIN expects number, got %s\n", value_type_name(v.type));
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
45
src/vm/math/sqrt.c
Normal file
45
src/vm/math/sqrt.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-01-03
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file sqrt.c
|
||||
* @brief Implements the OP_SQRT opcode using C99 math.h sqrt().
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
case OP_SQRT: {
|
||||
Value v = pop_value(vm);
|
||||
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
|
||||
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
|
||||
if (x < 0.0) {
|
||||
push_value(vm, make_float(NAN));
|
||||
} else {
|
||||
double r = sqrt(x);
|
||||
/* preserve int if exactly integral */
|
||||
if (r >= (double)INT64_MIN && r <= (double)INT64_MAX) {
|
||||
int64_t ii = (int64_t)r;
|
||||
if ((double)ii == r) {
|
||||
push_value(vm, make_int(ii));
|
||||
} else {
|
||||
push_value(vm, make_float(r));
|
||||
}
|
||||
} else {
|
||||
push_value(vm, make_float(r));
|
||||
}
|
||||
}
|
||||
free_value(v);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: SQRT expects number, got %s\n", value_type_name(v.type));
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
31
src/vm/math/tan.c
Normal file
31
src/vm/math/tan.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-01-03
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file tan.c
|
||||
* @brief Implements the OP_TAN opcode using C99 math.h tan().
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
case OP_TAN: {
|
||||
Value v = pop_value(vm);
|
||||
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
|
||||
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
|
||||
double r = tan(x);
|
||||
push_value(vm, make_float(r));
|
||||
free_value(v);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: TAN expects number, got %s\n", value_type_name(v.type));
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue