/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
template <std::integral I> I ConvertWithOverflowTo(double f)
{
f = rtl::math::round(f); if (!o3tl::convertsToAtMost(f, std::numeric_limits<I>::max()))
{
SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW); return std::numeric_limits<I>::max();
} if (!o3tl::convertsToAtLeast(f, std::numeric_limits<I>::min()))
{
SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW); return std::numeric_limits<I>::min();
} return f;
}
template <std::integral To, std::integral From> To ConvertWithOverflowTo(From n)
{ using ValidRange = o3tl::ValidRange<To>; if (ValidRange::isAbove(n))
{
SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW); return std::numeric_limits<To>::max();
} if (ValidRange::isBelow(n))
{
SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW); return std::numeric_limits<To>::min();
} return n;
}
template <std::floating_point To, std::floating_point From> inline To ConvertWithOverflowTo(From n)
{ if constexpr (std::numeric_limits<From>::max() > std::numeric_limits<To>::max())
{ if (n > std::numeric_limits<To>::max())
{
SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW); return std::numeric_limits<To>::max();
}
} if constexpr (std::numeric_limits<From>::lowest() < std::numeric_limits<To>::lowest())
{ if (n < std::numeric_limits<To>::lowest())
{
SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW); return std::numeric_limits<To>::lowest();
}
} // tests for underflow - storing value too small for precision of single if constexpr (std::numeric_limits<From>::min() < std::numeric_limits<To>::min())
{ if (n > 0 && n < std::numeric_limits<To>::min())
{
SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW); return std::numeric_limits<To>::min();
} if (n < 0 && n > -std::numeric_limits<To>::min())
{
SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW); return -std::numeric_limits<To>::min();
}
} return n;
}
template <std::floating_point To, std::integral From> inline To ConvertWithOverflowTo(From n)
{ // No integral types overflow floats returnstatic_cast<To>(n);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.