[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[979] | 2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[122] | 4 | |
---|
[109] | 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> //w vc2008 dzia³a³o tu <cmath>, ale w vc2010 juz nie bo "co" (jaki inny .h stl'a?) includuje wczeniej <cmath> bez _USE_MATH_DEFINES, a <cmath> includuje <math.h> (ale tylko raz bo ma "include guards" jak kazdy .h) |
---|
| 12 | #include <float.h> |
---|
[251] | 13 | //#define isnan(x) _isnan(x) //since 2014 we use std::isnan() |
---|
[109] | 14 | #define finite(x) _finite(x) |
---|
| 15 | #else //m.in. __BORLANDC__ |
---|
| 16 | #include <math.h> |
---|
| 17 | #endif |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | //random number generator: |
---|
| 22 | #include "random.h" |
---|
[896] | 23 | RandomGenerator &rndGetInstance(); |
---|
[109] | 24 | |
---|
[896] | 25 | inline double rndDouble(double limit_exclusive) { return rndGetInstance().getDouble() * limit_exclusive; } |
---|
| 26 | inline unsigned int rndUint(unsigned int limit_exclusive) { return (unsigned int)(rndGetInstance().getDouble() * limit_exclusive); } //returns random from 0..limit_exclusive-1 |
---|
| 27 | inline void rndSetSeed(unsigned int seed) { rndGetInstance().setSeed(seed); } |
---|
| 28 | inline unsigned int rndRandomizeSeed() { return rndGetInstance().randomize(); } |
---|
[109] | 29 | |
---|
[970] | 30 | |
---|
| 31 | |
---|
| 32 | // precision==-1 for full precision, or positive values for the number of digits to print past the decimal point. |
---|
| 33 | // Allocated buffer with bufferlen=30 is OK. |
---|
| 34 | // Returns the number of chars actually used (not including the ending 0). |
---|
| 35 | int doubleToString(double x, int precision, char *buffer, int bufferlen); |
---|
| 36 | |
---|
[979] | 37 | #include <string> |
---|
| 38 | std::string doubleToString(double x, int precision); |
---|
[970] | 39 | double round(const double x, const int precision); |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | |
---|
[109] | 45 | //floating point specific numbers |
---|
| 46 | #include "stdlib.h" |
---|
| 47 | |
---|
| 48 | #ifdef __BORLANDC__ |
---|
| 49 | #include <float.h> |
---|
| 50 | #define isnan(x) _isnan(x) //http://stackoverflow.com/questions/570669/checking-if-a-double-or-float-is-nan-in-c |
---|
| 51 | #define finite(x) _finite(x) |
---|
| 52 | #endif |
---|
| 53 | |
---|
[122] | 54 | #ifdef LINUX |
---|
| 55 | #define _isnan(a) isnan(a) |
---|
| 56 | #endif |
---|
| 57 | |
---|
[109] | 58 | #ifdef IPHONE |
---|
| 59 | #define finite(x) (!isinf(x)) |
---|
[122] | 60 | #define _isnan(a) isnan(a) |
---|
[109] | 61 | #endif |
---|
| 62 | |
---|
| 63 | |
---|
[122] | 64 | #if defined SHP |
---|
[109] | 65 | //#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" |
---|
| 66 | #define isnan(x) false //isnan() sie nie linkuje |
---|
| 67 | #define finite(x) true //j.w. |
---|
| 68 | //#include <cstdlib> //RAND_MAX defined incorrectly |
---|
| 69 | //#ifdef BADA_SIMULATOR //...but only in simulator libs |
---|
| 70 | // #undef RAND_MAX |
---|
| 71 | // #define RAND_MAX 32768 //...this is the actual value used by rand() |
---|
| 72 | //#endif |
---|
| 73 | #endif |
---|
| 74 | |
---|
| 75 | //handling floating point exceptions |
---|
| 76 | void fpExceptInit(); //call once, before ...Enable/Disable |
---|
| 77 | void fpExceptEnable(); |
---|
| 78 | void fpExceptDisable(); |
---|
| 79 | |
---|
[1053] | 80 | // 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 |
---|
[896] | 81 | template <typename Value, typename Linear> Value universal_lerp(Value a,Value b,Linear t) {return a*(1-t)+b*t;} |
---|
| 82 | |
---|
[913] | 83 | template <typename T> T linearTransform(T value, T min_in, T max_in, T min_out, T max_out) |
---|
| 84 | { |
---|
| 85 | return min_out + (value-min_in)*(max_out-min_out)/(max_in-min_in); |
---|
| 86 | } |
---|
| 87 | |
---|
[1053] | 88 | //in C++20, related: https://en.cppreference.com/w/cpp/numeric/countl_zero |
---|
| 89 | #ifdef _WIN32 //visual studio is missing ffs() |
---|
| 90 | static inline unsigned ffs(int x) |
---|
| 91 | { |
---|
| 92 | if (x == 0) return 0u; |
---|
| 93 | unsigned r = 1; |
---|
| 94 | while ((x & 1) == 0) |
---|
| 95 | x >>= 1, ++r; |
---|
| 96 | return r; |
---|
| 97 | } |
---|
[109] | 98 | #endif |
---|
[1053] | 99 | |
---|
| 100 | |
---|
| 101 | #endif |
---|