Zen API
 All Classes Files Functions Variables Typedefs Friends Macros Modules Pages
kMath.h
Go to the documentation of this file.
1 
10 #ifndef K_API_MATH_H
11 #define K_API_MATH_H
12 
13 #include <kApi/kApiDef.h>
14 #include <float.h>
15 
22 typedef kObject kMath;
23 
26 #define kMATH_PI (3.1415926535897932384626433832795)
27 #define kMATH_E (2.7182818284590452353602874713527)
28 #define kMATH_SQRT2 (1.4142135623730950488016887242097)
29 #define kMATH_SQRT3 (1.7320508075688772935274463415059)
30 
31 #define kMath_RadToDeg_(RAD) ((RAD)*180.0/(kMATH_PI))
32 #define kMath_DegToRad_(DEG) ((DEG)*(kMATH_PI)/180.0)
33 #define kMath_Abs_(A) (((A) >= 0) ? (A) : -(A))
34 #define kMath_Min_(A, B) (((A) < (B)) ? (A) : (B))
35 #define kMath_Max_(A, B) (((A) > (B)) ? (A) : (B))
36 #define kMath_Sign_(A) (((A) > 0) ? 1 : ((A) == 0) ? 0 : -1)
37 #define kMath_Common_Residue_(A,B) (((A) >= 0) ? ((A) % (B)) : (((B) - (-(A) % (B))) % (B)))
38 #define kMath_Clamp_(V, VMIN, VMAX) (kMath_Min_(kMath_Max_((V), (VMIN)), (VMAX)))
39 #define kMath_Round8s_(A) ((k8s) ((A) > 0 ? ((A)+0.5) : ((A)-0.5)))
40 #define kMath_Round8u_(A) ((k8u) ((A)+0.5))
41 #define kMath_Round16s_(A) ((k16s)((A) > 0 ? ((A)+0.5) : ((A)-0.5)))
42 #define kMath_Round16u_(A) ((k16u)((A)+0.5))
43 #define kMath_Round32s_(A) ((k32s)((A) > 0 ? ((A)+0.5) : ((A)-0.5)))
44 #define kMath_Round32u_(A) ((k32u)((A)+0.5))
45 #define kMath_Round64s_(A) ((k64s)((A) > 0 ? ((A)+0.5) : ((A)-0.5)))
46 #define kMath_Round64u_(A) ((k64u)((A)+0.5))
47 
48 
49 #include <kApi/Data/kMath.x.h>
50 
64 kFx(kStatus) kMath_FindFirst32s(const k32s* v, kSize count, kComparison comparison, k32s value, kSize* index);
65 
77 kFx(kStatus) kMath_FindFirst64f(const k64f* v, kSize count, kComparison comparison, k64f value, kSize* index);
78 
90 kFx(kStatus) kMath_FindLast32s(const k32s* v, kSize count, kComparison comparison, k32s value, kSize* index);
91 
103 kFx(kStatus) kMath_FindLast64f(const k64f* v, kSize count, kComparison comparison, k64f value, kSize* index);
104 
114 kFx(kStatus) kMath_FindMin32s(const k32s* v, kSize count, kSize* index);
115 
125 kFx(kStatus) kMath_FindMin32u(const k32u* v, kSize count, kSize* index);
126 
136 kFx(kStatus) kMath_FindMin64f(const k64f* v, kSize count, kSize* index);
137 
147 kFx(kStatus) kMath_FindMax32s(const k32s* v, kSize count, kSize* index);
148 
158 kFx(kStatus) kMath_FindMax32u(const k32u* v, kSize count, kSize* index);
159 
169 kFx(kStatus) kMath_FindMax64f(const k64f* v, kSize count, kSize* index);
170 
180 kFx(kStatus) kMath_Sum32s(const k32s* v, kSize count, k64s* sum);
181 
191 kFx(kStatus) kMath_Sum64f(const k64f* v, kSize count, k64f* sum);
192 
202 kFx(kStatus) kMath_Average32s(const k32s* v, kSize count, k64f* average);
203 
213 kFx(kStatus) kMath_Average64f(const k64f* v, kSize count, k64f* average);
214 
224 kFx(kStatus) kMath_Stdev32s(const k32s* v, kSize count, k64f* stdev);
225 
235 kFx(kStatus) kMath_Stdev64f(const k64f* v, kSize count, k64f* stdev);
236 
246 kFx(kStatus) kMath_Min32s(const k32s* v, kSize count, k32s* minValue);
247 
257 kFx(kStatus) kMath_Min32u(const k32u* v, kSize count, k32u* minValue);
258 
268 kFx(kStatus) kMath_Min64f(const k64f* v, kSize count, k64f* minValue);
269 
279 kFx(kStatus) kMath_Max32s(const k32s* v, kSize count, k32s* maxValue);
280 
290 kFx(kStatus) kMath_Max32u(const k32u* v, kSize count, k32u* maxValue);
291 
301 kFx(kStatus) kMath_Max64f(const k64f* v, kSize count, k64f* maxValue);
302 
312 kFx(kStatus) kMath_Centroid32s(const k32s* v, kSize count, k64f* centroid);
313 
323 kFx(kStatus) kMath_Centroid64f(const k64f* v, kSize count, k64f* centroid);
324 
334 kFx(kStatus) kMath_Set32s(k32s* v, kSize count, k32s value);
335 
345 kFx(kStatus) kMath_Set64f(k64f* v, kSize count, k64f value);
346 
357 kFx(kStatus) kMath_Step32s(k32s* v, kSize count, k32s startValue, k32s increment);
358 
369 kFx(kStatus) kMath_Step64f(k64f* v, kSize count, k64f startValue, k64f increment);
370 
381 kFx(kStatus) kMath_Span32s(k32s* v, kSize count, k32s startValue, k32s endValue);
382 
393 kFx(kStatus) kMath_Span64f(k64f* v, kSize count, k64f startValue, k64f endValue);
394 
404 kFx(kStatus) kMath_Abs32s(const k32s* vIn, k32s* vOut, kSize count);
405 
415 kFx(kStatus) kMath_Abs64f(const k64f* vIn, k64f* vOut, kSize count);
416 
427 kFx(kStatus) kMath_AddC32s(const k32s* vIn, k32s* vOut, kSize count, k32s value);
428 
439 kFx(kStatus) kMath_AddC64f(const k64f* vIn, k64f* vOut, kSize count, k64f value);
440 
451 kFx(kStatus) kMath_SubC32s(const k32s* vIn, k32s* vOut, kSize count, k32s value);
452 
463 kFx(kStatus) kMath_SubC64f(const k64f* vIn, k64f* vOut, kSize count, k64f value);
464 
475 kFx(kStatus) kMath_MulC32s(const k32s* vIn, k32s* vOut, kSize count, k32s value);
476 
487 kFx(kStatus) kMath_MulC64f(const k64f* vIn, k64f* vOut, kSize count, k64f value);
488 
499 kFx(kStatus) kMath_DivC32s(const k32s* vIn, k32s* vOut, kSize count, k32s value);
500 
511 kFx(kStatus) kMath_DivC64f(const k64f* vIn, k64f* vOut, kSize count, k64f value);
512 
524 kFx(kStatus) kMath_ClampC32s(const k32s* vIn, k32s* vOut, kSize count, k32s minValue, k32s maxValue);
525 
537 kFx(kStatus) kMath_ClampC64f(const k64f* vIn, k64f* vOut, kSize count, k64f minValue, k64f maxValue);
538 
551 kFx(kStatus) kMath_ReplaceC32s(const k32s* vIn, k32s* vOut, kSize count, kComparison comparison, k32s value, k32s replacement);
552 
565 kFx(kStatus) kMath_ReplaceC64f(const k64f* vIn, k64f* vOut, kSize count, kComparison comparison, k64f value, k64f replacement);
566 
578 kFx(kStatus) kMath_CompareC32s(const k32s* vIn, kBool* vOut, kSize count, kComparison comparison, k32s value);
579 
591 kFx(kStatus) kMath_CompareC64f(const k64f* vIn, kBool* vOut, kSize count, kComparison comparison, k64f value);
592 
603 kFx(kStatus) kMath_Add32s(const k32s* vIn1, const k32s* vIn2, k32s* vOut, kSize count);
604 
615 kFx(kStatus) kMath_Add64f(const k64f* vIn1, const k64f* vIn2, k64f* vOut, kSize count);
616 
627 kFx(kStatus) kMath_Sub32s(const k32s* vIn1, const k32s* vIn2, k32s* vOut, kSize count);
628 
639 kFx(kStatus) kMath_Sub64f(const k64f* vIn1, const k64f* vIn2, k64f* vOut, kSize count);
640 
651 kFx(kStatus) kMath_Mul32s(const k32s* vIn1, const k32s* vIn2, k32s* vOut, kSize count);
652 
663 kFx(kStatus) kMath_Mul64f(const k64f* vIn1, const k64f* vIn2, k64f* vOut, kSize count);
664 
675 kFx(kStatus) kMath_Div32s(const k32s* vIn1, const k32s* vIn2, k32s* vOut, kSize count);
676 
687 kFx(kStatus) kMath_Div64f(const k64f* vIn1, const k64f* vIn2, k64f* vOut, kSize count);
688 
699 kFx(kStatus) kMath_MovingAvg32s(const k32s* vIn, k32s* vOut, kSize count, kSize window);
700 
711 kFx(kStatus) kMath_MovingAvg64f(const k64f* vIn, k64f* vOut, kSize count, kSize window);
712 
722 kFx(kStatus) kMath_Gcd32s(k32s a, k32s b, k32s* result);
723 
733 kFx(kStatus) kMath_Lcm32s(k32s a, k32s b, k32s* result);
734 
743 {
744  return (a > 0) ? 1 : ((a == 0) ? 0 : -1);
745 }
746 
754 kFx(k32u) kMath_Log2Ceil32u(k32u a);
755 
763 kFx(k64f) kMath_Round64f(k64f a);
764 
778 {
779  /* Adapted from simple relative diff code: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ */
780 
781  k32f absDiff = kAbs_(a - b);
782  k32f absA = kAbs_(a);
783  k32f absB = kAbs_(b);
784  k32f larger = kMax_(absA, absB);
785 
786  return (absDiff <= larger * FLT_EPSILON);
787 }
788 
802 {
803  /* Adapted from simple relative diff code: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ */
804 
805  k64f absDiff = kAbs_(a - b);
806  k64f absA = kAbs_(a);
807  k64f absB = kAbs_(b);
808  k64f larger = kMax_(absA, absB);
809 
810  return (absDiff <= larger * DBL_EPSILON);
811 }
812 
813 #endif
kStatus kMath_FindMax32s(const k32s *v, kSize count, kSize *index)
Finds the index of the maximum value within a numerical array.
Represents a 32-bit unsigned integer.
kStatus kMath_Average32s(const k32s *v, kSize count, k64f *average)
Calculates the average value for a numerical array.
kStatus kMath_MulC32s(const k32s *vIn, k32s *vOut, kSize count, k32s value)
Multiplies each element in an input array by a constant and stores the result in an output array...
kStatus kMath_Abs64f(const k64f *vIn, k64f *vOut, kSize count)
Calculates the absolute value of each element in an input array and stores the result in an output ar...
kStatus kMath_FindMax64f(const k64f *v, kSize count, kSize *index)
Finds the index of the maximum value within a numerical array.
kStatus kMath_MovingAvg64f(const k64f *vIn, k64f *vOut, kSize count, kSize window)
Calculates the moving average over an input array and stores the result in an output array...
kStatus kMath_MulC64f(const k64f *vIn, k64f *vOut, kSize count, k64f value)
Multiplies each element in an input array by a constant and stores the result in an output array...
kStatus kMath_Sum32s(const k32s *v, kSize count, k64s *sum)
Calculates the sum of a numerical array.
kStatus kMath_Sum64f(const k64f *v, kSize count, k64f *sum)
Calculates the sum of a numerical array.
kStatus kMath_FindMax32u(const k32u *v, kSize count, kSize *index)
Finds the index of the maximum value within a numerical array.
Represents an unsigned integer that can store a pointer address.
kStatus kMath_Step64f(k64f *v, kSize count, k64f startValue, k64f increment)
Sets values in a numerical array to increment from the specified starting value.
kStatus kMath_AddC32s(const k32s *vIn, k32s *vOut, kSize count, k32s value)
Adds a constant to each element in an input array and stores the result in an output array...
#define kInlineFx(TYPE)
Inline method declaration helper.
Definition: kApiDef.h:26
kStatus kMath_ReplaceC64f(const k64f *vIn, k64f *vOut, kSize count, kComparison comparison, k64f value, k64f replacement)
Compares each element in an input array with a specified value, and replaces all matching values with...
kStatus kMath_Max32u(const k32u *v, kSize count, k32u *maxValue)
Reports the maximum value in a numerical array.
kStatus kMath_Mul64f(const k64f *vIn1, const k64f *vIn2, k64f *vOut, kSize count)
Multiplies the values in two input arrays and stores the result in an output array.
k32s kMath_Sign(k32s a)
Returns the sign.
Definition: kMath.h:742
kStatus kMath_Step32s(k32s *v, kSize count, k32s startValue, k32s increment)
Sets values in a numerical array to increment from the specified starting value.
kStatus kMath_ClampC32s(const k32s *vIn, k32s *vOut, kSize count, k32s minValue, k32s maxValue)
Limits each element in an input array using a minimum and maximum value.
kStatus kMath_Sub32s(const k32s *vIn1, const k32s *vIn2, k32s *vOut, kSize count)
Subtracts the values in two input arrays and stores the result in an output array.
kStatus kMath_Gcd32s(k32s a, k32s b, k32s *result)
Returns the greatest common divisor of two integers.
kStatus kMath_FindMin32s(const k32s *v, kSize count, kSize *index)
Finds the index of the minimum value within a numerical array.
kStatus kMath_Max64f(const k64f *v, kSize count, k64f *maxValue)
Reports the maximum value in a numerical array.
kBool kMath_NearlyEquals32f(k32f a, k32f b)
Reports whether two single-precision values are approximately equal.
Definition: kMath.h:777
kStatus kMath_DivC32s(const k32s *vIn, k32s *vOut, kSize count, k32s value)
Divides each element in an input array by a constant and stores the result in an output array...
kStatus kMath_CompareC64f(const k64f *vIn, kBool *vOut, kSize count, kComparison comparison, k64f value)
Compares each element in an input array with a specified value and stores the results in an output ar...
kStatus kMath_Mul32s(const k32s *vIn1, const k32s *vIn2, k32s *vOut, kSize count)
Multiplies the values in two input arrays and stores the result in an output array.
kStatus kMath_Div32s(const k32s *vIn1, const k32s *vIn2, k32s *vOut, kSize count)
Divides the values in two input arrays and stores the result in an output array.
kStatus kMath_FindFirst32s(const k32s *v, kSize count, kComparison comparison, k32s value, kSize *index)
Compares each value in a numerical array with a specified value and returns the index of the first ma...
kStatus kMath_Div64f(const k64f *vIn1, const k64f *vIn2, k64f *vOut, kSize count)
Divides the values in two input arrays and stores the result in an output array.
kStatus kMath_DivC64f(const k64f *vIn, k64f *vOut, kSize count, k64f value)
Divides each element in an input array by a constant and stores the result in an output array...
kStatus kMath_Set32s(k32s *v, kSize count, k32s value)
Sets all values in a numerical array to the given value.
k64f kMath_Round64f(k64f a)
Rounds the input up to the nearest integer.
k32u kMath_Log2Ceil32u(k32u a)
Calculates the base-2 logarithm of the input, rounded up to the nearest integer.
kBool kMath_NearlyEquals64f(k64f a, k64f b)
Reports whether two double-precision values are approximately equal.
Definition: kMath.h:801
kStatus kMath_Min64f(const k64f *v, kSize count, k64f *minValue)
Reports the minimum value in a numerical array.
kStatus kMath_Min32u(const k32u *v, kSize count, k32u *minValue)
Reports the minimum value in a numerical array.
Core Zen type declarations.
Represents a 32-bit signed integer.
kStatus kMath_SubC64f(const k64f *vIn, k64f *vOut, kSize count, k64f value)
Subtracts a constant from each element in an input array and stores the result in an output array...
kStatus kMath_MovingAvg32s(const k32s *vIn, k32s *vOut, kSize count, kSize window)
Calculates the moving average over an input array and stores the result in an output array...
kStatus kMath_FindFirst64f(const k64f *v, kSize count, kComparison comparison, k64f value, kSize *index)
Compares each value in a numerical array with a specified value and returns the index of the first ma...
kStatus kMath_AddC64f(const k64f *vIn, k64f *vOut, kSize count, k64f value)
Adds a constant to each element in an input array and stores the result in an output array...
kStatus kMath_Stdev64f(const k64f *v, kSize count, k64f *stdev)
Calculates the standard deviation for a numerical array.
kStatus kMath_Max32s(const k32s *v, kSize count, k32s *maxValue)
Reports the maximum value in a numerical array.
kStatus kMath_ClampC64f(const k64f *vIn, k64f *vOut, kSize count, k64f minValue, k64f maxValue)
Limits each element in an input array using a minimum and maximum value.
Represents a 64-bit signed integer.
kStatus kMath_Average64f(const k64f *v, kSize count, k64f *average)
Calculates the average value for a numerical array.
kStatus kMath_Add32s(const k32s *vIn1, const k32s *vIn2, k32s *vOut, kSize count)
Adds the values in two input arrays and stores the result in an output array.
kStatus kMath_Set64f(k64f *v, kSize count, k64f value)
Sets all values in a numerical array to the given value.
kStatus kMath_ReplaceC32s(const k32s *vIn, k32s *vOut, kSize count, kComparison comparison, k32s value, k32s replacement)
Compares each element in an input array with a specified value, and replaces all matching values with...
kStatus kMath_Span64f(k64f *v, kSize count, k64f startValue, k64f endValue)
Sets values in a numerical array to step between the specified start and end values.
Represents a 32-bit floating-point number.
Root of all Zen classes.
kStatus kMath_FindLast32s(const k32s *v, kSize count, kComparison comparison, k32s value, kSize *index)
Compares each value in a numerical array with a specified value and returns the index of the last mat...
kStatus kMath_Centroid64f(const k64f *v, kSize count, k64f *centroid)
Calculates the center of gravity for a numerical array.
Represents an error code.
kStatus kMath_Span32s(k32s *v, kSize count, k32s startValue, k32s endValue)
Sets values in a numerical array to step between the specified start and end values.
kStatus kMath_Stdev32s(const k32s *v, kSize count, k64f *stdev)
Calculates the standard deviation for a numerical array.
kStatus kMath_Sub64f(const k64f *vIn1, const k64f *vIn2, k64f *vOut, kSize count)
Subtracts the values in two input arrays and stores the result in an output array.
Collection of mathematical utility functions.
Represents a 64-bit floating-point number.
Represents a comparison type.
kStatus kMath_Abs32s(const k32s *vIn, k32s *vOut, kSize count)
Calculates the absolute value of each element in an input array and stores the result in an output ar...
kStatus kMath_FindMin64f(const k64f *v, kSize count, kSize *index)
Finds the index of the minimum value within a numerical array.
Represents a boolean value.
kStatus kMath_FindLast64f(const k64f *v, kSize count, kComparison comparison, k64f value, kSize *index)
Compares each value in a numerical array with a specified value and returns the index of the last mat...
kStatus kMath_Lcm32s(k32s a, k32s b, k32s *result)
Returns the least common multiple of two integers.
kStatus kMath_CompareC32s(const k32s *vIn, kBool *vOut, kSize count, kComparison comparison, k32s value)
Compares each element in an input array with a specified value and stores the results in an output ar...
kStatus kMath_Centroid32s(const k32s *v, kSize count, k64f *centroid)
Calculates the center of gravity for a numerical array.
kStatus kMath_Min32s(const k32s *v, kSize count, k32s *minValue)
Reports the minimum value in a numerical array.
kStatus kMath_SubC32s(const k32s *vIn, k32s *vOut, kSize count, k32s value)
Subtracts a constant from each element in an input array and stores the result in an output array...
kStatus kMath_FindMin32u(const k32u *v, kSize count, kSize *index)
Finds the index of the minimum value within a numerical array.
kStatus kMath_Add64f(const k64f *vIn1, const k64f *vIn2, k64f *vOut, kSize count)
Adds the values in two input arrays and stores the result in an output array.