Support pi() function

This commit is contained in:
Lauri Virtanen
2024-12-12 22:54:28 +02:00
parent 5e426a7624
commit 89d0289444
4 changed files with 25 additions and 1 deletions

View File

@@ -184,7 +184,7 @@ Feature support of [sqlite expr syntax](https://www.sqlite.org/lang_expr.html).
| log10(X) | Yes | |
| log2(X) | Yes | |
| mod(X,Y) | Yes | |
| pi() | No | |
| pi() | Yes | |
| pow(X,Y) | Yes | |
| power(X,Y) | Yes | |
| radians(X) | Yes | |

View File

@@ -1604,6 +1604,20 @@ pub fn translate_expr(
}
}
Func::Math(math_func) => match math_func.arity() {
MathFuncArity::Nullary => {
if args.is_some() {
crate::bail_parse_error!("{} function with arguments", math_func);
}
program.emit_insn(Insn::Function {
constant_mask: 0,
start_reg: 0,
dest: target_register,
func: func_ctx,
});
Ok(target_register)
}
MathFuncArity::Unary => {
let args = if let Some(args) = args {
if args.len() != 1 {

View File

@@ -2492,6 +2492,10 @@ impl Program {
}
},
crate::function::Func::Math(math_func) => match math_func.arity() {
MathFuncArity::Nullary => {
state.registers[*dest] = OwnedValue::Float(std::f64::consts::PI);
}
MathFuncArity::Unary => {
let reg_value = &state.registers[*start_reg];
let result = exec_math_unary(reg_value, math_func);
@@ -2504,6 +2508,7 @@ impl Program {
let result = exec_math_binary(lhs, rhs, math_func);
state.registers[*dest] = result;
}
_ => unimplemented!(),
},
crate::function::Func::Agg(_) => {

View File

@@ -364,6 +364,11 @@ do_execsql_test bitwise-not-zero {
set tolerance 1e-13
do_execsql_test_tolerance pi {
SELECT pi()
} {3.14159265358979} $tolerance
do_execsql_test_tolerance acos-int {
SELECT acos(1)
} {0.0} $tolerance