Commit 0b176e83

0b176e83033dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 6 files · +2 added · ~4 modified

Description

Merge pull request #1411 from ryanseys/rs-c-math-domain-error

fix(math): raise Math::DomainError on out-of-domain Math.* input

Details

Commit
0b176e83033d
Parent
effd67618b56
Merge parent
7f2fadbd9c76

lib/sp_core.cmodifiedView file

@@ -255,8 +255,11 @@
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;}
257257 /* Integer square root via Newton's method -- exact for the full
258- mrb_int range. Negative input returns 0 (no exception path). */
259-mrb_int sp_int_sqrt(mrb_int n){if(n<0)return 0;if(n<2)return n;mrb_int x=n,y=(x+1)/2;while(y<x){x=y;y=(x+n/x)/2;}return x;}
258+ mrb_int range. CRuby raises Math::DomainError on negative input
259+ (flattened runtime name "Math_DomainError"). The seed is n/2, not
260+ (n+1)/2: at n == MRB_INT_MAX the latter overflows (signed UB), and
261+ n/2 is a valid Newton seed for all n >= 2. */
262+mrb_int sp_int_sqrt(mrb_int n){if(n<0)sp_raise_cls("Math_DomainError","Numerical argument is out of domain - \"isqrt\"");if(n<2)return n;mrb_int x=n,y=n/2;while(y<x){x=y;y=(x+n/x)/2;}return x;}
260263 /* Integer#round/ceil/floor/truncate at 10^(-ndigits). Pure integer
261264 arithmetic (no double precision loss above 2^53). 10^p fits mrb_int
262265 only for p<=18; p>=19 collapses to 0. Round-up multiply is overflow-