Commit 85665a83
85665a834671Merged
IG <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
feat(codegen,runtime): add Integer#lcm
Implement least common multiple using the existing sp_gcd runtime function. Formula: lcm(a, b) = |a| / gcd(a, b) * |b|, dividing first to avoid overflow.
Changes:
- lib/sp_runtime.h: add sp_lcm() next to sp_gcd()
- spinel_codegen.rb: infer_type + codegen for "lcm"
- test/integer_lcm.rb: 17 cases covering zero, negative, coprime, divisibility, primes, and large values
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 85665a834671
- Parent
- 51d369bb0e1c
lib/sp_runtime.hmodifiedView file
@@ -76,6 +76,7 @@
7676
}7777
7878
static mrb_int sp_gcd(mrb_int a,mrb_int b){if(a<0)a=-a;if(b<0)b=-b;while(b){mrb_int t=b;b=a%b;a=t;}return a;}79+
static mrb_int sp_lcm(mrb_int a,mrb_int b){if(a==0||b==0)return 0;mrb_int g=sp_gcd(a,b);if(a<0)a=-a;if(b<0)b=-b;return (a/g)*b;}7980
static mrb_int sp_int_clamp(mrb_int v,mrb_int lo,mrb_int hi){return v<lo?lo:v>hi?hi:v;}8081
static inline char *sp_str_alloc_raw(size_t total_with_null); /* fwd decl */8182
static const char*sp_int_chr(mrb_int n){char*s=sp_str_alloc_raw(2);s[0]=(char)n;s[1]=0;return s;}spinel_codegen.rbmodifiedView file
Loading diff…
test/integer_lcm.rbaddedView file
Loading diff…