Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sc / inc / math.hxx
blobc2a3b992dd00422929876c33c59f94fb6e173c5c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SC_INC_MATH_HXX
21 #define INCLUDED_SC_INC_MATH_HXX
23 #include <formula/errorcodes.hxx>
25 namespace sc {
27 /** Return fNumerator/fDenominator if fDenominator!=0 else #DIV/0! error coded
28 into double.
30 inline double div( const double& fNumerator, const double& fDenominator )
32 return (fDenominator != 0.0) ? (fNumerator / fDenominator) :
33 CreateDoubleError( FormulaError::DivisionByZero);
36 /** Return fNumerator/fDenominator if fDenominator!=0 else +-Infinity if
37 fNumerator!=0 or NaN if fNumerator==0.
39 This allows to build/run with -fsanitize=float-divide-by-zero and have a
40 defined behavior for the otherwise undefined division by zero case ("If the
41 second operand of / or % is zero the behavior is undefined."
42 ([expr.mul]/4)).
44 The Calc interpreter gracefully handles Infinity or NaN double values
45 encountered as interim or final results, using this function we can ensure
46 defined behavior where desired.
48 Use where the double coded error creating div() is not wanted.
50 inline double divide( const double& fNumerator, const double& fDenominator )
52 if (fDenominator == 0.0)
54 double fVal;
55 if (rtl::math::isFinite( fNumerator) && fNumerator != 0.0)
57 rtl::math::setInf( &fVal, rtl::math::isSignBitSet( fNumerator));
59 else
61 rtl::math::setNan( &fVal);
63 return fVal;
65 return fNumerator / fDenominator;
70 #endif
72 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */