Log
bbb151e42204
feat(core): add sqrt, exp, log, even, odd built-in functions
public
feat(core): add sqrt, exp, log, even, odd built-in functions
Extend the single-word, non-temporal built-in slice (ADR-15) with the
remaining single-word numeric functions: sqrt, exp, log (natural), and
the even/odd integer predicates. Dispatch by name + arity through a
shared `unary_num` helper; domain errors (sqrt of a negative, log of a
non-positive, even/odd of a non-integer) follow the ADR-14 error-null
policy, while a value-null argument propagates as null.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
modified
crates/feel-core/src/builtins.rs
+57 −0
@@ -68,6 +68,11 @@
68 68 " ceiling " = > floor_ceiling_fn(args, false ), 69 69 " abs " = > abs_fn(args), 70 70 " modulo " = > modulo_fn(args), 71 + " sqrt " = > sqrt_fn(args), 72 + " exp " = > exp_fn(args), 73 + " log " = > log_fn(args), 74 + " even " = > even_odd_fn(args, true ), 75 + " odd " = > even_odd_fn(args, false ), 71 76 72 77 // Unknown / unimplemented (multi-word, temporal, user-defined) call. 73 78 _ = > Value::Error,
@@ -783,3 +788,55 @@
783 788 _ = > Value::Error, 784 789 } 785 790 } 791 + 792 +/// Apply a unary numeric function: a value-null argument propagates as `null`, 793 +/// a non-number or wrong arity is an error-null, and `f` maps a valid number to 794 +/// the result. `f` may itself return an error-null for a domain error (e.g. 795 +/// `sqrt` of a negative, `log` of a non-positive). 796 +fn unary_num (args: Vec < Value> , f: impl Fn (f64 ) -> Value ) -> Value { 797 + match args.as_slice() { 798 + [v] = > match num_arg(v) { 799 + NumArg::Num(n) = > f(n), 800 + NumArg::Null = > Value::Null, 801 + NumArg::Err = > Value::Error, 802 + }, 803 + _ = > Value::Error, 804 + } 805 +} 806 + 807 +fn sqrt_fn (args: Vec < Value> ) -> Value { 808 + // sqrt of a negative has no real value -> domain error. 809 + unary_num(args, | n| { 810 + if n < 0.0 { 811 + Value::Error 812 + } else { 813 + Value::Number(n.sqrt()) 814 + } 815 + }) 816 +} 817 + 818 +fn exp_fn (args: Vec < Value> ) -> Value { 819 + unary_num(args, | n| Value::Number(n.exp())) 820 +} 821 + 822 +fn log_fn (args: Vec < Value> ) -> Value { 823 + // log is the natural logarithm; its domain is the positive reals. 824 + unary_num(args, | n| { 825 + if n < = 0.0 { 826 + Value::Error 827 + } else { 828 + Value::Number(n.ln()) 829 + } 830 + }) 831 +} 832 + 833 +fn even_odd_fn (args: Vec < Value> , want_even: bool ) -> Value { 834 + // even/odd are defined on integers; a non-integral number is a domain error. 835 + unary_num(args, | n| { 836 + if n.fract() ! = 0.0 { 837 + return Value::Error; 838 + } 839 + let is_even = (n % 2.0 ) = = 0.0 ; 840 + Value::Boolean(is_even = = want_even) 841 + }) 842 +}
modified
crates/feel-core/src/lib.rs
+11 −1
@@ -388,6 +388,16 @@
388 388 assert_eq! (ev(" decimal(2.5, 0) " ), num(2.0 )); // round half to even 389 389 assert_eq! (ev(" modulo(12, 0) " ), Value::Null); // divide by zero -> null 390 390 assert_eq! (ev(" abs(true) " ), Value::Null); // wrong type -> null 391 + assert_eq! (ev(" sqrt(16) " ), num(4.0 )); 392 + assert_eq! (ev(" sqrt(-1) " ), Value::Null); // negative -> domain error -> null 393 + assert_eq! (ev(" exp(0) " ), num(1.0 )); 394 + assert_eq! (ev(" log(1) " ), num(0.0 )); 395 + assert_eq! (ev(" log(0) " ), Value::Null); // non-positive -> domain error -> null 396 + assert_eq! (ev(" even(2) " ), b(true )); 397 + assert_eq! (ev(" even(3) " ), b(false )); 398 + assert_eq! (ev(" even(2.5) " ), Value::Null); // non-integer -> domain error -> null 399 + assert_eq! (ev(" odd(3) " ), b(true )); 400 + assert_eq! (ev(" odd(2) " ), b(false )); 391 401 392 402 // --- list --- (spec §10.3.4.4) 393 403 assert_eq! (ev(" count([1, 2, 3]) " ), num(3.0 ));
@@ -440,6 +450,6 @@
440 450 list(vec! [s(" a " ), s(" b " ), s(" c " ), s(" " ), s(" " )])); 441 451 442 452 // --- unknown / out-of-scope function -> null (error-null, lenient) --- 443 - assert_eq! (ev(" sqrt(4) " ), Value::Null); 453 + assert_eq! (ev(" now() " ), Value::Null); // temporal, not yet implemented 444 454 } 445 455 }