Skip to content

Commit

Permalink
Avoid undefined behavior of casting large floating point number to in…
Browse files Browse the repository at this point in the history
…teger.

git-svn-id: https://svn.r-project.org/R/trunk@87532 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
kalibera committed Jan 6, 2025
1 parent 491e12c commit 2a30696
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/extra/trio/trio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3418,9 +3418,11 @@ TRIO_ARGS6((self, number, flags, width, precision, base),
/* need round-to-even rule here. Applies only if base == 10 */
if(base == 10 && i == integerDigits - 1 && fractionDigits == 0
&& integerAdjust == 0.5) {
int ii = (int) trio_floor(integerNumber/
TrioPower(base, integerDigits - i - 1));
if(ii % 2 == 0)
trio_long_double_t ldi;
ldi = trio_floor(integerNumber/
TrioPower(base, integerDigits - i - 1));
int ii = (int) trio_fmod(ldi, 2.0);
if(ii == 0)
workNumber = trio_floor(((integerNumber + integerAdjust - 1e-7)
/ TrioPower(base, integerDigits - i - 1)));
else
Expand Down Expand Up @@ -3485,8 +3487,9 @@ TRIO_ARGS6((self, number, flags, width, precision, base),
fractionNumber *= dblBase;
fractionAdjust *= dblBase;
if(base == 10 && i == fractionDigits - 1) {
int ii = (int) trio_floor(fractionNumber);
if(ii % 2 == 0)
trio_long_double_t ldi = trio_floor(fractionNumber);
int ii = (int)trio_fmod(ldi, 2.0);
if(ii == 0)
workNumber = trio_floor(fractionNumber +
fractionAdjust*(1-1e-14));
else
Expand Down

0 comments on commit 2a30696

Please sign in to comment.