Commit b91e46e3
b91e46e3c64bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Integer#round/ceil/floor/truncate: exact integer arithmetic
Follow-up to #1059 (gemini-code-assist review). The merged
implementation routed through double ((mrb_int)round((double)v/f)
with f=(mrb_int)pow(10,-nd)), which:
- loses precision for |v| > 2^53 (e.g. 1000000000000000005.round(-1) gave ...000 instead of ...010), and
- is undefined behavior once 10^(-nd) exceeds INT64_MAX (-nd>=19): the pow result overflows the mrb_int cast, producing garbage (5000000000000000000.round(-19) returned INT64_MIN).
Replace with pure integer division/modulo (sp_ipow10 + half-away-from- zero for round, sign-aware adjust for ceil/floor, truncation for truncate). 10^p fits mrb_int only for p<=18, so p>=19 collapses to 0 (the exact result is unrepresentable in 64-bit), and the round-up multiply is overflow-guarded against INT64_MAX/MIN. All in-range results now match CRuby exactly.
Refs #1059
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- b91e46e3c64b
- Parent
- 48ec92a3db83
lib/sp_runtime.hmodifiedView file
static 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;}static inline char *sp_str_alloc_raw(size_t total_with_null); /* fwd decl */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;}/* Integer#round(ndigits): round to nearest 10^(-ndigits). Integer#ceil(ndigits): round up to 10^(-ndigits). Integer#floor(ndigits): round down to 10^(-ndigits). Integer#truncate(ndigits): truncate toward zero at 10^(-ndigits). Positive ndigits are a no-op on integers. */static mrb_int sp_int_round(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int f=(mrb_int)pow(10,-nd);return(mrb_int)round((double)v/f)*f;}static mrb_int sp_int_ceil(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int f=(mrb_int)pow(10,-nd);return(mrb_int)ceil((double)v/f)*f;}static mrb_int sp_int_floor(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int f=(mrb_int)pow(10,-nd);return(mrb_int)floor((double)v/f)*f;}static mrb_int sp_int_truncate(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int f=(mrb_int)pow(10,-nd);return(v>=0?(mrb_int)floor((double)v/f):(mrb_int)ceil((double)v/f))*f;}/* Integer#round(ndigits): round to nearest 10^(-ndigits) (half away from zero). Integer#ceil/floor/truncate at 10^(-ndigits). Positive ndigits are a no-op on integers. Pure integer arithmetic: converting to double would lose precision above 2^53, and casting pow(10,-nd) to mrb_int is undefined once 10^(-nd) exceeds MRB_INT_MAX (-nd>=19). 10^p fits mrb_int only for p<=18, so p>=19 collapses to 0 (the exact result is unrepresentable in 64-bit); the round-up multiply is overflow-guarded and falls back to the truncated value. */static mrb_int sp_ipow10(mrb_int p){mrb_int f=1;mrb_int i=0;while(i<p){f*=10;i++;}return f;}static mrb_int sp_int_round(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=19)return 0;mrb_int f=sp_ipow10(p);mrb_int q=v/f,r=v%f,half=f/2;if(v>=0){if(r>=half&&q<INT64_MAX/f)return(q+1)*f;return q*f;}if(-r>=half&&q>INT64_MIN/f)return(q-1)*f;return q*f;}static mrb_int sp_int_ceil(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=19)return 0;mrb_int f=sp_ipow10(p);mrb_int q=v/f,r=v%f;if(r!=0&&v>0&&q<INT64_MAX/f)return(q+1)*f;return q*f;}static mrb_int sp_int_floor(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=19)return 0;mrb_int f=sp_ipow10(p);mrb_int q=v/f,r=v%f;if(r!=0&&v<0&&q>INT64_MIN/f)return(q-1)*f;return q*f;}static mrb_int sp_int_truncate(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=19)return 0;mrb_int f=sp_ipow10(p);return(v/f)*f;}/* Forward decls for helpers used across this header (and by the string->number parsers that now live in libspinel_rt.a). */test/int_round_large_precision.rbaddedView file
Loading diff…
test/int_round_large_precision.rb.expectedaddedView file
Loading diff…