hello-stephen commented on code in PR #71094:
URL: https://github.com/apache/doris/pull/71094#discussion_r4751373368


##########
be/src/exprs/function/round.h:
##########
@@ -329,6 +382,20 @@ class FloatRoundingComputation : public 
BaseFloatRoundingComputation<T, tie_brea
             val = Base::divide(val, scale);
         } else if (scale_mode == ScaleMode::Negative) {
             val = Base::multiply(val, scale);
+            // For example, ceil(1.7e308, -308) needs 2e308, which DOUBLE 
cannot store.
+            if (!std::isfinite(val)) {

Review Comment:
   [P2] Preserve finite inputs before negative-scale rescaling
   
   For a stored DOUBLE `x = 9.999999999999999e39`, `round(x, -20)` now returns 
`9.999999999999998e39`. Division by the new exact `1e20` factor produces the 
already-integral `99999999999999983616`, and multiplication back loses one 
input ULP. The result is finite, so this recovery never runs. Since `1e20` is 
below half the input's inward ULP (`2^79`), all five rounding modes should 
preserve `x`; the base preserved it through the exact binary factor `2^64`. 
Apply the identity decision before the lossy round trip, including finite 
results, and add exact-equality coverage for this non-extremal input across 
argument shapes.



##########
be/src/exprs/function/round.h:
##########
@@ -316,11 +337,43 @@ class FloatRoundingComputation : public 
BaseFloatRoundingComputation<T, tie_brea
     static inline void compute(const T* __restrict in, const typename 
Base::VectorType& scale,
                                T* __restrict out) {
         auto val = Base::load(in);
-
-        if (scale_mode == ScaleMode::Positive) {
-            val = Base::multiply(val, scale);
-        } else if (scale_mode == ScaleMode::Negative) {
+        if (!std::isfinite(val)) {
+            Base::store(out, val);
+            return;
+        }
+        if constexpr (scale_mode == ScaleMode::Positive) {
+            const auto scaled = Base::multiply(val, scale);
+            // Above 2^(digits + 1), a decimal unit is below half the input's 
inward
+            // spacing, including at powers of two. Keep the input: rescaling 
an
+            // already integral product can turn round(47.5, 20) into 
47.50000000000001.
+            constexpr T unchanged_threshold = T(UInt64(1) << 
(std::numeric_limits<T>::digits + 1));
+            if (!std::isfinite(scale) || std::abs(scaled) > 
unchanged_threshold) {
+                Base::store(out, val);
+                return;
+            }
+            val = scaled;
+        } else if constexpr (scale_mode == ScaleMode::Negative) {
+            if (!std::isfinite(scale)) {
+                // For example, round(1, -309) is zero. Directed rounding may 
exceed DOUBLE.
+                if ((rounding_mode == RoundingMode::Ceil && val > 0) ||
+                    (rounding_mode == RoundingMode::Floor && val < 0)) {
+                    throw Exception(ErrorCode::ARITHMETIC_OVERFLOW_ERRROR,
+                                    "Arithmetic overflow in floating rounding 
result");
+                }
+                Base::store(out, std::copysign(T(0), val));
+                return;
+            }
             val = Base::divide(val, scale);
+            // A tiny nonzero quotient can underflow. Preserve its direction 
for FLOOR and CEIL.
+            if (val == 0 && *in != 0) {
+                if constexpr (rounding_mode == RoundingMode::Floor) {
+                    Base::store(out, *in < 0 ? -scale : T(0));
+                    return;
+                } else if constexpr (rounding_mode == RoundingMode::Ceil) {
+                    Base::store(out, *in > 0 ? scale : T(0));

Review Comment:
   [P3] Preserve negative zero in the CEIL underflow shortcut
   
   For a stored DOUBLE `x = -1e-300`, `ceil(x, -308)` underflows the quotient 
to negative zero and enters this branch, which returns positive `T(0)`. 
Consequently `signbit(ceil(x, -308))` changes from `true` on the base to 
`false`. The normal `ceil` path preserves negative zero, as does the new 
infinite-factor branch via `copysign`. Preserve that sign here too, and test it 
through `signbit` because ordinary zero equality does not distinguish the 
results.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to