1 | #include "nonstd_math.h" |
---|
2 | |
---|
3 | RandomGenerator& rndGetInstance() |
---|
4 | { |
---|
5 | static RandomGenerator rnd(0); |
---|
6 | return rnd; |
---|
7 | } |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | #ifdef _WIN32 //http://stackoverflow.com/questions/2170385/c-math-functions |
---|
13 | double round(double val) |
---|
14 | { |
---|
15 | return floor(val + 0.5); |
---|
16 | } |
---|
17 | #endif |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | #if defined LINUX || defined TIZEN |
---|
23 | |
---|
24 | #include <fenv.h> |
---|
25 | |
---|
26 | void fpExceptInit() |
---|
27 | {} |
---|
28 | |
---|
29 | void fpExceptEnable() |
---|
30 | { |
---|
31 | feclearexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
32 | feenableexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
33 | } |
---|
34 | |
---|
35 | void fpExceptDisable() |
---|
36 | { |
---|
37 | fedisableexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
38 | } |
---|
39 | |
---|
40 | #endif |
---|
41 | |
---|
42 | |
---|
43 | #ifdef IPHONE |
---|
44 | |
---|
45 | #include <fenv.h> |
---|
46 | |
---|
47 | void fpExceptInit() |
---|
48 | {} |
---|
49 | |
---|
50 | void fpExceptEnable() |
---|
51 | { |
---|
52 | feclearexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
53 | //feenableexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
54 | } |
---|
55 | |
---|
56 | void fpExceptDisable() |
---|
57 | { |
---|
58 | //fedisableexcept(FE_DIVBYZERO | FE_OVERFLOW); |
---|
59 | } |
---|
60 | #endif |
---|
61 | |
---|
62 | |
---|
63 | #ifdef __BORLANDC__ |
---|
64 | |
---|
65 | #include "framsg.h" |
---|
66 | |
---|
67 | unsigned int fp_control_word_std; |
---|
68 | unsigned int fp_control_word_muted; |
---|
69 | |
---|
70 | void fpExceptInit() |
---|
71 | { |
---|
72 | //unsigned int was=_clear87(); |
---|
73 | //FMprintf("","fpExceptInit",FMLV_INFO,"control87 status before clear was %08x", was); |
---|
74 | fp_control_word_std=_control87(0, 0); //4978 = 1001101110010 |
---|
75 | // Make the new fp env same as the old one, except for the changes we're going to make |
---|
76 | fp_control_word_muted = fp_control_word_std | EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT; //4991 = 1001101111111 |
---|
77 | } |
---|
78 | |
---|
79 | void fpExceptEnable() |
---|
80 | { |
---|
81 | unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
82 | //FMprintf("","fpExceptEnable ",FMLV_INFO,"control87 status before clear was %08x", was); |
---|
83 | _control87(fp_control_word_std, 0xffffffff); |
---|
84 | //FMprintf("","fpExceptEnable ",FMLV_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
85 | } |
---|
86 | |
---|
87 | void fpExceptDisable() |
---|
88 | { |
---|
89 | unsigned int was=_clear87(); //trzeba czyscic zeby nie bylo exception... |
---|
90 | //FMprintf("","fpExceptDisable",FMLV_INFO,"control87 status before clear was %08x", was); |
---|
91 | _control87(fp_control_word_muted, 0xffffffff); |
---|
92 | //FMprintf("","fpExceptDisable",FMLV_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo |
---|
93 | } |
---|
94 | |
---|
95 | #endif |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | #ifdef _MSC_VER |
---|
100 | //Moznaby zrobic tak jak pod linuxem czyli wlaczyc exceptiony na poczatku i wylaczac na chwile przy dzieleniu w extvalue. |
---|
101 | //To by pozwoli³o na wy³apanie pod visualem z³ych sytuacji kiedy framsy licz¹ na NaN, INF itp. |
---|
102 | //http://stackoverflow.com/questions/2769814/how-do-i-use-try-catch-to-catch-floating-point-errors |
---|
103 | void fpExceptInit() {} |
---|
104 | void fpExceptEnable() {} |
---|
105 | void fpExceptDisable() {} |
---|
106 | #endif |
---|
107 | |
---|