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

@@ -199,15 +199,19 @@
199199 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;}
200200 static inline char *sp_str_alloc_raw(size_t total_with_null); /* fwd decl */
201201 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;}
202-/* Integer#round(ndigits): round to nearest 10^(-ndigits).
203- Integer#ceil(ndigits): round up to 10^(-ndigits).
204- Integer#floor(ndigits): round down to 10^(-ndigits).
205- Integer#truncate(ndigits): truncate toward zero at 10^(-ndigits).
206- Positive ndigits are a no-op on integers. */
207-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;}
208-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;}
209-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;}
210-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;}
202+/* Integer#round(ndigits): round to nearest 10^(-ndigits) (half away
203+ from zero). Integer#ceil/floor/truncate at 10^(-ndigits). Positive
204+ ndigits are a no-op on integers. Pure integer arithmetic: converting
205+ to double would lose precision above 2^53, and casting pow(10,-nd)
206+ to mrb_int is undefined once 10^(-nd) exceeds MRB_INT_MAX (-nd>=19).
207+ 10^p fits mrb_int only for p<=18, so p>=19 collapses to 0 (the exact
208+ result is unrepresentable in 64-bit); the round-up multiply is
209+ overflow-guarded and falls back to the truncated value. */
210+static mrb_int sp_ipow10(mrb_int p){mrb_int f=1;mrb_int i=0;while(i<p){f*=10;i++;}return f;}
211+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;}
212+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;}
213+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;}
214+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;}
211215
212216 /* Forward decls for helpers used across this header (and by the
213217 string->number parsers that now live in libspinel_rt.a). */