mirror of
				https://github.com/eledio-devices/thirdparty-tinyexpr.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	add ncr and npr
This commit is contained in:
		
							
								
								
									
										19
									
								
								tinyexpr.c
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								tinyexpr.c
									
									
									
									
									
								
							| @@ -125,7 +125,7 @@ static double fac(double a) {/* simplest version of fac */ | ||||
|     if (a > UINT_MAX) | ||||
|         return INFINITY; | ||||
|     unsigned int ua = (unsigned int)(a); | ||||
|     unsigned long int result = 1, i = 1; | ||||
|     unsigned long int result = 1, i; | ||||
|     for (i = 1; i <= ua; i++) { | ||||
|         if (i > ULONG_MAX / result) | ||||
|             return INFINITY; | ||||
| @@ -133,6 +133,21 @@ static double fac(double a) {/* simplest version of fac */ | ||||
|     } | ||||
|     return (double)result; | ||||
| } | ||||
| static double ncr(double n, double r) { | ||||
|     if (n < 0.0 || r < 0.0 || n < r) return NAN; | ||||
|     if (n > UINT_MAX || r > UINT_MAX) return INFINITY; | ||||
|     unsigned long int un = (unsigned int)(n), ur = (unsigned int)(r), i; | ||||
|     unsigned long int result = 1; | ||||
|     if (ur > un / 2) ur = un - ur; | ||||
|     for (i = 1; i <= ur; i++) { | ||||
|         if (result > ULONG_MAX / (un - ur + i)) | ||||
|             return INFINITY; | ||||
|         result *= un - ur + i; | ||||
|         result /= i; | ||||
|     } | ||||
|     return result; | ||||
| } | ||||
| static double npr(double n, double r) {return ncr(n, r) * fac(r);} | ||||
|  | ||||
| static const te_variable functions[] = { | ||||
|     /* must be in alphabetical order */ | ||||
| @@ -155,6 +170,8 @@ static const te_variable functions[] = { | ||||
|     {"log", log10,    TE_FUNCTION1 | TE_FLAG_PURE, 0}, | ||||
| #endif | ||||
|     {"log10", log10,  TE_FUNCTION1 | TE_FLAG_PURE, 0}, | ||||
|     {"ncr", ncr,      TE_FUNCTION2 | TE_FLAG_PURE, 0}, | ||||
|     {"npr", npr,      TE_FUNCTION2 | TE_FLAG_PURE, 0}, | ||||
|     {"pi", pi,        TE_FUNCTION0 | TE_FLAG_PURE, 0}, | ||||
|     {"pow", pow,      TE_FUNCTION2 | TE_FLAG_PURE, 0}, | ||||
|     {"sin", sin,      TE_FUNCTION1 | TE_FLAG_PURE, 0}, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user