ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
safemath.hpp
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16/*
17Safe math header file
18---------------------
19This file defines basic functions for safe mathematical operations.
20Rather than raising an error on fail cases (i.e. divide by zero),
21the following libraries are designed to check for errors and return
22corresponding codes.
23
24Author: Alex Reynolds
25*/
26
27#ifndef SAFEMATH_HPP
28#define SAFEMATH_HPP
29
30#define SAFE_MATH_ZERO_TOLERANCE 1e-15
31
32namespace clockwerk {
33
34 /// @brief Overloaded functions to perform safe division
35 /// @param a Dividend
36 /// @param b Divisor
37 /// @param result PBR value to receive result of division
38 /// @return Error code corresponding to errors in clockwerkerrors.h
39 int safeDivide(const float &a, const float &b, float &result);
40 int safeDivide(const double &a, const double &b, double &result);
41 int safeDivide(const int &a, const int &b, int &result);
42 int safeDivide(const unsigned long long &a, const unsigned long long &b, unsigned long long &result);
43
44 /// @brief Overloaded functions to perform safe square root
45 /// @param input The value to take the square root of
46 /// @param result PBR value to receive result of square
47 /// @return Error code corresponding to errors in clockwerkerrors.h
48 int safeSqrt(const float &input, float &result);
49 int safeSqrt(const double &input, double &result);
50
51 /// @brief Overloaded functions to perform safe n root
52 /// @param input The value to take the n root of
53 /// @param rt The degree of root to take
54 /// @param result PBR value to receive result of n-root
55 /// @return Error code corresponding to errors in clockwerkerrors.h
56 int safeNroot(const float &input, const int &rt, float &result);
57 int safeNroot(const double &input, const int &rt, double &result);
58
59 /// @brief Overloaded functions to perform save cosine inverse
60 /// @param input The input value to take cosine of
61 /// @param result The result of the cos
62 /// @return Error code corresponding to success/failure
63 int safeAcos(const float &input, float &result);
64 int safeAcos(const double &input, double &result);
65
66 /// @brief Overloaded functions to perform save sine inverse
67 /// @param input The input value to take sine of
68 /// @param result The result of the sin
69 /// @return Error code corresponding to success/failure
70 int safeAsin(const float &input, float &result);
71 int safeAsin(const double &input, double &result);
72
73 /// @brief Overloaded functions to perform safe tangent
74 /// @param input The input value to take tangent of
75 /// @param result The result of the tangent
76 /// @return Error code corresponding to success/failure
77 int safeTan(const float &input, float &result);
78 int safeTan(const double &input, double &result);
79}
80
81#endif