diff --git a/COMPAT.md b/COMPAT.md index 9580799fd..25dae3614 100644 --- a/COMPAT.md +++ b/COMPAT.md @@ -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 | | diff --git a/core/translate/expr.rs b/core/translate/expr.rs index 7bc08f2c2..c8e66821d 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -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 { diff --git a/core/vdbe/mod.rs b/core/vdbe/mod.rs index 09f52d892..796eeff94 100644 --- a/core/vdbe/mod.rs +++ b/core/vdbe/mod.rs @@ -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(_) => { diff --git a/testing/math.test b/testing/math.test index 9b43b95f9..c75c387e6 100644 --- a/testing/math.test +++ b/testing/math.test @@ -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