source: cpp/common/nonstd_math.h @ 1316

Last change on this file since 1316 was 1302, checked in by Maciej Komosinski, 7 months ago

Cosmetic / minor fixes

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#ifndef _NONSTD_MATH_H_
6#define _NONSTD_MATH_H_
7
8
9#ifdef _MSC_VER
10 #define _USE_MATH_DEFINES //after this is defined, the next #include <math.h> or <cmath> will define M_PI etc.
11 #include <math.h> //in vc2008, <cmath> worked here, but no longer in vc2010 because "something" (some other .h from stl?) earlier includes <cmath> without _USE_MATH_DEFINES, and <cmath> includes <math.h> (just once because it has "include guards" like any other .h)
12 #include <float.h>
13 //#define isnan(x) _isnan(x) //since 2014 we use std::isnan()
14 #define finite(x) _finite(x)
15#else //e.g. __BORLANDC__
16 #include <math.h>
17#endif
18
19
20
21//random number generator:
22#include "random.h"
23RandomGenerator &rndGetInstance();
24
25inline double rndDouble(double limit_exclusive) { return rndGetInstance().getDouble() * limit_exclusive; }
26inline unsigned int rndUint(unsigned int limit_exclusive) { return (unsigned int)(rndGetInstance().getDouble() * limit_exclusive); } //returns random from 0..limit_exclusive-1
27#ifndef __BORLANDC__ //for embarcadero 11u3, unsigned int and size_t are duplicates
28inline unsigned int rndUint(size_t limit_exclusive) {return rndUint((unsigned int)limit_exclusive);} //just an overload with size_t argument
29#endif
30unsigned int rndUint(int limit_exclusive); //just an overload with int argument
31inline void rndSetSeed(unsigned int seed) { rndGetInstance().setSeed(seed); }
32inline unsigned int rndRandomizeSeed() { return rndGetInstance().randomize(); }
33
34
35
36// precision==-1 for full precision, or positive values for the number of digits to print past the decimal point.
37// Allocated buffer with bufferlen=30 is OK.
38// Returns the number of chars actually used (not including the ending 0).
39int doubleToString(double x, int precision, char *buffer, int bufferlen);
40
41#include <string>
42std::string doubleToString(double x, int precision);
43double round(const double x, const int precision);
44static inline void clipNegativeZeroIfNeeded(double& value, const double range_low)
45{
46        if (value == 0.0 && range_low == 0.0) // if we have range_low==0.0 and we get value==-0.0 (which is ==0.0)
47                value = 0.0; //turn -0.0 to 0.0 so that it does not look like exceeding the allowed range (even though -0.0==0.0). This code unnecessarily also "overwrites" value==0.0 with 0.0, but it is faster and simpler than additionally checking std::signbit(value) just to distinguish -0.0 from 0.0.
48        //these conditions are not intended for range_low==-0.0, as we assume nobody would define allowed ranges using -0.0.
49}
50
51
52
53
54
55//floating point specific numbers
56#include "stdlib.h"
57
58#ifdef __BORLANDC__
59        #include <float.h>
60        #define isnan(x) _isnan(x) //http://stackoverflow.com/questions/570669/checking-if-a-double-or-float-is-nan-in-c
61        #define finite(x) _finite(x)
62#endif
63
64#ifdef LINUX
65  #define _isnan(a) isnan(a)
66#endif
67
68#ifdef IPHONE
69        #define finite(x) (!isinf(x))
70  #define _isnan(a) isnan(a)
71#endif
72
73
74#if defined SHP
75 //#define __assert_func(a,b,c,d) 0 //Currently, we are sorry to inform you that assert() is not yet supported. We have considered your request for internal discussion. Na szcz�cie jest w�asna (byle by by�a, bo i tak zak�adamy ze assert ktore przeciez dziala tylko w trybie debug nie jest potrzebne na bada) implementacja w "bada-assert.cpp"
76 #define isnan(x) false //isnan() sie nie linkuje
77 #define finite(x) true //j.w.
78 //#include <cstdlib> //RAND_MAX defined incorrectly
79 //#ifdef BADA_SIMULATOR //...but only in simulator libs
80 // #undef RAND_MAX
81 // #define RAND_MAX 32768 //...this is the actual value used by rand()
82 //#endif
83#endif
84
85#if defined LINUX || defined TIZEN || defined __ANDROID__
86#include <fenv.h>
87#endif
88
89namespace fpExcept
90{
91//handling floating point exceptions
92#if defined LINUX || defined TIZEN || defined __ANDROID__
93        //fenv.h values
94        static constexpr unsigned int FPEX_DIV0 = FE_DIVBYZERO;
95        static constexpr unsigned int FPEX_INVALID = FE_INVALID;
96        static constexpr unsigned int FPEX_OVERFLOW = FE_OVERFLOW;
97#elif defined IPHONE
98        // (not implemented but these constants are still needed)
99        static constexpr unsigned int FPEX_DIV0 = 0;
100        static constexpr unsigned int FPEX_INVALID = 0;
101        static constexpr unsigned int FPEX_OVERFLOW = 0;
102#else
103        //_control87() values
104        static constexpr unsigned int FPEX_DIV0 = EM_ZERODIVIDE;
105        static constexpr unsigned int FPEX_INVALID = EM_INVALID;
106        static constexpr unsigned int FPEX_OVERFLOW = EM_OVERFLOW;
107#endif
108        extern int wanted_exceptions;
109       
110        void init(); //call once, before ...Enable/Disable
111        void enable();
112        void disable();
113};
114
115// std::lerp can be used since C++20 (and has some guaranteed properties probably better than this basic formula) but apparently it is not a template
116template <typename Value, typename Linear> Value universal_lerp(Value a,Value b,Linear t) {return a*(1-t)+b*t;}
117
118template <typename T> T linearTransform(T value, T min_in, T max_in, T min_out, T max_out)
119{
120        return min_out + (value-min_in)*(max_out-min_out)/(max_in-min_in);
121}
122
123//in C++20, related: https://en.cppreference.com/w/cpp/numeric/countl_zero
124#ifdef _WIN32 //visual studio is missing ffs()
125static inline unsigned ffs(int x)
126{
127        if (x == 0) return 0u;
128        unsigned r = 1;
129        while ((x & 1) == 0)
130                x >>= 1, ++r;
131        return r;
132}
133#endif
134
135
136#endif
Note: See TracBrowser for help on using the repository browser.