[Webkit-unassigned] [Bug 249771] Fix Decimal.floor() + Decimal.ceiling() for many non-integral values

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Sat Apr 6 09:49:21 PDT 2024


https://bugs.webkit.org/show_bug.cgi?id=249771

--- Comment #4 from Ahmad Saleem <ahmad.saleem792 at gmail.com> ---
Just copying that following compiles when added to 'Decimal.cpp' (for TestWebKit API):

// Simulate core/html/forms/StepRange
class DecimalStepRange {

public:
    Decimal maximum;
    Decimal minimum;
    Decimal step;

    DecimalStepRange(const Decimal& minimum, const Decimal& maximum, const Decimal& step)
        : maximum(maximum)
        , minimum(minimum)
        , step(step)
    {
    }

    Decimal clampValue(Decimal value) const
    {
        const Decimal result = minimum + ((value - minimum) / step).round() * step;
        ASSERT(result.isFinite());
        return result > maximum ? result - step : result;
    }
};

class Decimcal : public testing::Test {
protected:
    using Sign = Decimal::Sign;
    static constexpr Sign positive = Decimal::Positive;
    static constexpr Sign negative = Decimal::Negative;

    Decimal encode(uint64_t coefficient, int exponent, Sign sign)
    {
        return Decimal(sign, exponent, coefficient);
    }

    Decimal fromString(const String& string)
    {
        return Decimal::fromString(string);
    }

    Decimal stepDown(const String& minimum, const String& maximum, const String& step, const String& valueString, int numberOfStepTimes)
    {
        DecimalStepRange stepRange(fromString(minimum), fromString(maximum), fromString(step));
        Decimal value = fromString(valueString);

        for (int i = 0; i < numberOfStepTimes; ++i) {
            value -= stepRange.step;
            value = stepRange.clampValue(value);
        }
        return value;
    }

    Decimal stepUp(const String& minimum, const String& maximum, const String& step, const String& valueString, int numberOfStepTimes)
    {
        DecimalStepRange stepRange(fromString(minimum), fromString(maximum), fromString(step));
        Decimal value = fromString(valueString);

        for (int i = 0; i < numberOfStepTimes; ++i) {
            value += stepRange.step;
            value = stepRange.clampValue(value);
        }
        return value;
    }
};

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-unassigned/attachments/20240406/82ec1013/attachment-0001.htm>


More information about the webkit-unassigned mailing list