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) {
52 double p, int_part, dec_part;
54 if (value == HUGE_VAL)
56 if (value == -HUGE_VAL)
59 return ves_icall_System_Math_Round(value);
61 dec_part = modf (value, &int_part);
62 dec_part *= 1000000000000000ULL;
63 dec_part = floor(dec_part);
64 dec_part /= (1000000000000000ULL / p);
65 dec_part = ves_icall_System_Math_Round(dec_part);
67 return int_part + dec_part;
71 ves_icall_System_Math_Sin (gdouble x)
79 ves_icall_System_Math_Cos (gdouble x)
87 ves_icall_System_Math_Tan (gdouble x)
95 ves_icall_System_Math_Sinh (gdouble x)
103 ves_icall_System_Math_Cosh (gdouble x)
111 ves_icall_System_Math_Tanh (gdouble x)
119 ves_icall_System_Math_Acos (gdouble x)
130 ves_icall_System_Math_Asin (gdouble x)
141 ves_icall_System_Math_Atan (gdouble x)
149 ves_icall_System_Math_Atan2 (gdouble y, gdouble x)
154 if ((y == HUGE_VAL && x == HUGE_VAL) ||
155 (y == HUGE_VAL && x == -HUGE_VAL) ||
156 (y == -HUGE_VAL && x == HUGE_VAL) ||
157 (y == -HUGE_VAL && x == -HUGE_VAL)) {
160 result = atan2 (y, x);
161 return (result == -0)? 0: result;
165 ves_icall_System_Math_Exp (gdouble x)
173 ves_icall_System_Math_Log (gdouble x)
186 ves_icall_System_Math_Log10 (gdouble x)
199 ves_icall_System_Math_Pow (gdouble x, gdouble y)
204 if (isnan(x) || isnan(y)) {
208 if ((x == 1 || x == -1) && (y == HUGE_VAL || y == -HUGE_VAL)) {
212 /* This code is for return the same results as MS.NET for certain
214 if (x < -9007199254740991.0) {
215 if (y > 9007199254740991.0)
217 if (y < -9007199254740991.0)
223 /* This code is for return the same results as MS.NET for certain
227 ((y > 9007199254740991.0) || (y < -9007199254740991.0))) {
231 return (result == -0)? 0: result;
235 ves_icall_System_Math_Sqrt (gdouble x)