Commit dfb43f7f

dfb43f7f2955Merged

Ryan Seys <[email protected]> · 5 months ago · 6 files · +2 added · ~4 modified

Description

analyze,codegen,runtime: add Float#clamp

Float#clamp with float bounds previously rejected with unsupported call (clamp) even though Integer#clamp and the Comparable#clamp string path already worked. Add the missing float path:

  • sp_float_clamp in the runtime, mirroring sp_int_clamp.
  • a codegen branch in the float-receiver dispatch that emits it.
  • float-receiver clamp inference returning a float.

All three are gated on both bounds being float-typed. With float bounds the returned value is always one of the bounds (a float), so the type is exact. A mixed or integer bound can return the integer bound, which stays poly and is left to the existing path.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
dfb43f7f2955
Parent
4058359a8053

lib/sp_core.cmodifiedView file

@@ -254,6 +254,7 @@
254254 mrb_int sp_powmod(mrb_int base,mrb_int exp,mrb_int mod){if(mod==0)sp_raise_cls("ZeroDivisionError","divided by 0");mrb_int r=1;mrb_int m=mod<0?-mod:mod;if(m==1){r=0;}else{base=base%m;if(base<0)base+=m;while(exp>0){if(exp%2==1)r=r*base%m;exp=exp/2;base=base*base%m;}}if(mod<0&&r>0)r-=m;return r;}
255255 mrb_int sp_ceildiv(mrb_int a,mrb_int b){if(b==0)sp_raise_cls("ZeroDivisionError","divided by 0");if(b==-1)return -a;mrb_int q=a/b;if(a%b!=0&&((a^b)>=0))q++;return q;}
256256 mrb_int sp_int_clamp(mrb_int v,mrb_int lo,mrb_int hi){return v<lo?lo:v>hi?hi:v;}
257+mrb_float sp_float_clamp(mrb_float v,mrb_float lo,mrb_float hi){return v<lo?lo:v>hi?hi:v;}
257258 /* Integer square root via Newton's method -- exact for the full
258259 mrb_int range. CRuby raises Math::DomainError on negative input
259260 (flattened runtime name "Math_DomainError"). The seed is n/2, not