1 /* math.c - these are based on bob smith's csharp routines */
5 #include <mono/metadata/sysmath.h>
6 #include <mono/metadata/exception.h>
9 # if G_BYTE_ORDER == G_BIG_ENDIAN
10 # define __nan_bytes { 0x7f, 0xc0, 0, 0 }
12 # if G_BYTE_ORDER == G_LITTLE_ENDIAN
13 # define __nan_bytes { 0, 0, 0xc0, 0x7f }
16 static union { unsigned char __c[4]; float __d; } __nan_union = { __nan_bytes };
17 # define NAN (__nan_union.__d)
21 #define __huge_val_t union { unsigned char __c[8]; double __d; }
22 # if G_BYTE_ORDER == G_BIG_ENDIAN
23 # define __HUGE_VAL_bytes { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 }
25 # if G_BYTE_ORDER == G_LITTLE_ENDIAN
26 # define __HUGE_VAL_bytes { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f }
28 static __huge_val_t __huge_val = { __HUGE_VAL_bytes };
29 # define HUGE_VAL (__huge_val.__d)
33 gdouble ves_icall_System_Math_Floor (gdouble x) {
38 gdouble ves_icall_System_Math_Round (gdouble x) {
39 double int_part, dec_part;
42 dec_part = x - int_part;
43 if (((dec_part == 0.5) &&
44 ((2.0 * ((int_part / 2.0) - floor(int_part / 2.0))) != 0.0)) ||
51 gdouble ves_icall_System_Math_Round2 (gdouble value, gint32 digits, gboolean away_from_zero) {
52 #if !defined (HAVE_ROUND) || !defined (HAVE_RINT)
53 double int_part, dec_part;
58 if (value == HUGE_VAL)
60 if (value == -HUGE_VAL)
63 return ves_icall_System_Math_Round(value);
65 #if defined (HAVE_ROUND) && defined (HAVE_RINT)
67 return round (value * p) / p;
69 return rint (value * p) / p;
71 dec_part = modf (value, &int_part);
72 dec_part *= 1000000000000000ULL;
73 if (away_from_zero && dec_part > 0)
74 dec_part = ceil (dec_part);
76 dec_part = floor (dec_part);
77 dec_part /= (1000000000000000ULL / p);
80 dec_part = floor (dec_part + 0.5);
82 dec_part = ceil (dec_part - 0.5);
84 dec_part = ves_icall_System_Math_Round (dec_part);
86 return ves_icall_System_Math_Round ((int_part + dec_part) * p) / p;
91 ves_icall_System_Math_Sin (gdouble x)
99 ves_icall_System_Math_Cos (gdouble x)
107 ves_icall_System_Math_Tan (gdouble x)
115 ves_icall_System_Math_Sinh (gdouble x)
123 ves_icall_System_Math_Cosh (gdouble x)
131 ves_icall_System_Math_Tanh (gdouble x)
139 ves_icall_System_Math_Acos (gdouble x)
150 ves_icall_System_Math_Asin (gdouble x)
161 ves_icall_System_Math_Atan (gdouble x)
169 ves_icall_System_Math_Atan2 (gdouble y, gdouble x)
174 if ((y == HUGE_VAL && x == HUGE_VAL) ||
175 (y == HUGE_VAL && x == -HUGE_VAL) ||
176 (y == -HUGE_VAL && x == HUGE_VAL) ||
177 (y == -HUGE_VAL && x == -HUGE_VAL)) {
180 result = atan2 (y, x);
181 return (result == -0)? 0: result;
185 ves_icall_System_Math_Exp (gdouble x)
193 ves_icall_System_Math_Log (gdouble x)
206 ves_icall_System_Math_Log10 (gdouble x)
219 ves_icall_System_Math_Pow (gdouble x, gdouble y)
224 if (isnan(x) || isnan(y)) {
228 if ((x == 1 || x == -1) && (y == HUGE_VAL || y == -HUGE_VAL)) {
232 /* This code is for return the same results as MS.NET for certain
234 if (x < -9007199254740991.0) {
235 if (y > 9007199254740991.0)
237 if (y < -9007199254740991.0)
243 /* This code is for return the same results as MS.NET for certain
247 ((y > 9007199254740991.0) || (y < -9007199254740991.0))) {
251 return (result == -0)? 0: result;
255 ves_icall_System_Math_Sqrt (gdouble x)