fixed builtins ceil, floor, round (#37)

This commit is contained in:
Robert Bendun 2023-01-08 09:41:30 +01:00
parent 93a15a3cf8
commit ad95299a7e
5 changed files with 110 additions and 15 deletions

View File

@ -254,22 +254,30 @@ namespace impl
static Number round(Number result, Rounding_Mode rm)
{
if (result.den <= -1 || result.den >= 1) {
if (result.den == -1 || result.den == 1) {
return result.simplify();
}
auto const negative = (result.num < 0) xor (result.den < 0);
if (result.num < 0) result.num *= -1;
if (result.den < 0) result.den *= -1;
if (auto const r = result.num % result.den; r != 0) {
auto const sign = (result.num < 0) xor (result.den < 0) ? -1 : 1;
bool ceil = rm == Rounding_Mode::Ceil;
ceil |= rm == Rounding_Mode::Round && (negative
? r*2 <= result.den
: r*2 >= result.den);
if (rm == Rounding_Mode::Round)
rm = r * 2 >= result.den ? Rounding_Mode::Ceil : Rounding_Mode::Floor;
if (rm == Rounding_Mode::Floor) result.num -= sign * r;
if (rm == Rounding_Mode::Ceil) result.num += sign * r;
if (ceil ^ negative) {
result.num += result.den;
}
// C++ integer division handles floor case
result.num /= result.den;
result.den = 1;
} else {
result.simplify_inplace();
}
}
result.num *= negative ? -1 : 1;
return result;
}
}

View File

@ -0,0 +1,11 @@
say (ceil (0 - 4)),
say (ceil (0 - 4.2)),
say (ceil (0 - 4.5)),
say (ceil (0 - 4.8)),
say (ceil (0 - 5)),
say (ceil 4),
say (ceil 4.2),
say (ceil 4.5),
say (ceil 4.8),
say (ceil 5),

View File

@ -0,0 +1,11 @@
say (floor (0 - 4)),
say (floor (0 - 4.2)),
say (floor (0 - 4.5)),
say (floor (0 - 4.8)),
say (floor (0 - 5)),
say (floor 4),
say (floor 4.2),
say (floor 4.5),
say (floor 4.8),
say (floor 5),

View File

@ -0,0 +1,11 @@
say (round (0 - 4)),
say (round (0 - 4.2)),
say (round (0 - 4.5)),
say (round (0 - 4.8)),
say (round (0 - 5)),
say (round 4),
say (round 4.2),
say (round 4.5),
say (round 4.8),
say (round 5),

View File

@ -216,6 +216,60 @@
"(1, 2, 3, 4, 5, 6, 7, 8)"
],
"stderr_lines": []
},
{
"name": "ceil.mq",
"exit_code": 0,
"stdin_lines": [],
"stdout_lines": [
"-4",
"-4",
"-4",
"-4",
"-5",
"4",
"5",
"5",
"5",
"5"
],
"stderr_lines": []
},
{
"name": "floor.mq",
"exit_code": 0,
"stdin_lines": [],
"stdout_lines": [
"-4",
"-5",
"-5",
"-5",
"-5",
"4",
"4",
"4",
"4",
"5"
],
"stderr_lines": []
},
{
"name": "round.mq",
"exit_code": 0,
"stdin_lines": [],
"stdout_lines": [
"-4",
"-4",
"-4",
"-5",
"-5",
"4",
"4",
"5",
"5",
"5"
],
"stderr_lines": []
}
]
}