fixed builtins ceil, floor, round (#37)
This commit is contained in:
parent
93a15a3cf8
commit
ad95299a7e
@ -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;
|
||||
}
|
||||
}
|
||||
|
11
regression-tests/builtin/ceil.mq
Normal file
11
regression-tests/builtin/ceil.mq
Normal 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),
|
11
regression-tests/builtin/floor.mq
Normal file
11
regression-tests/builtin/floor.mq
Normal 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),
|
11
regression-tests/builtin/round.mq
Normal file
11
regression-tests/builtin/round.mq
Normal 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),
|
@ -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": []
|
||||
}
|
||||
]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user