111
Dependencies: yezhong_main_controller_copy mbed1-dev
math_ops/math_ops.cpp@0:d80c66cb1b3a, 2020-11-10 (annotated)
- Committer:
- panzhan
- Date:
- Tue Nov 10 09:09:58 2020 +0000
- Revision:
- 0:d80c66cb1b3a
panzhan_main_controller_copy2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
panzhan | 0:d80c66cb1b3a | 1 | |
panzhan | 0:d80c66cb1b3a | 2 | #include "math_ops.h" |
panzhan | 0:d80c66cb1b3a | 3 | |
panzhan | 0:d80c66cb1b3a | 4 | |
panzhan | 0:d80c66cb1b3a | 5 | float fmaxf(float x, float y){ |
panzhan | 0:d80c66cb1b3a | 6 | /// Returns maximum of x, y /// |
panzhan | 0:d80c66cb1b3a | 7 | return (((x)>(y))?(x):(y)); |
panzhan | 0:d80c66cb1b3a | 8 | } |
panzhan | 0:d80c66cb1b3a | 9 | |
panzhan | 0:d80c66cb1b3a | 10 | float fminf(float x, float y){ |
panzhan | 0:d80c66cb1b3a | 11 | /// Returns minimum of x, y /// |
panzhan | 0:d80c66cb1b3a | 12 | return (((x)<(y))?(x):(y)); |
panzhan | 0:d80c66cb1b3a | 13 | } |
panzhan | 0:d80c66cb1b3a | 14 | |
panzhan | 0:d80c66cb1b3a | 15 | float fmaxf3(float x, float y, float z){ |
panzhan | 0:d80c66cb1b3a | 16 | /// Returns maximum of x, y, z /// |
panzhan | 0:d80c66cb1b3a | 17 | return (x > y ? (x > z ? x : z) : (y > z ? y : z)); |
panzhan | 0:d80c66cb1b3a | 18 | } |
panzhan | 0:d80c66cb1b3a | 19 | |
panzhan | 0:d80c66cb1b3a | 20 | float fminf3(float x, float y, float z){ |
panzhan | 0:d80c66cb1b3a | 21 | /// Returns minimum of x, y, z /// |
panzhan | 0:d80c66cb1b3a | 22 | return (x < y ? (x < z ? x : z) : (y < z ? y : z)); |
panzhan | 0:d80c66cb1b3a | 23 | } |
panzhan | 0:d80c66cb1b3a | 24 | |
panzhan | 0:d80c66cb1b3a | 25 | void limit_norm(float *x, float *y, float limit){ |
panzhan | 0:d80c66cb1b3a | 26 | /// Scales the lenght of vector (x, y) to be <= limit /// |
panzhan | 0:d80c66cb1b3a | 27 | float norm = sqrt(*x * *x + *y * *y); |
panzhan | 0:d80c66cb1b3a | 28 | if(norm > limit){ |
panzhan | 0:d80c66cb1b3a | 29 | *x = *x * limit/norm; |
panzhan | 0:d80c66cb1b3a | 30 | *y = *y * limit/norm; |
panzhan | 0:d80c66cb1b3a | 31 | } |
panzhan | 0:d80c66cb1b3a | 32 | } |
panzhan | 0:d80c66cb1b3a | 33 | |
panzhan | 0:d80c66cb1b3a | 34 | |
panzhan | 0:d80c66cb1b3a | 35 | int float_to_uint(float x, float x_min, float x_max, int bits){ |
panzhan | 0:d80c66cb1b3a | 36 | /// Converts a float to an unsigned int, given range and number of bits /// |
panzhan | 0:d80c66cb1b3a | 37 | float span = x_max - x_min; |
panzhan | 0:d80c66cb1b3a | 38 | float offset = x_min; |
panzhan | 0:d80c66cb1b3a | 39 | return (int) ((x-offset)*((float)((1<<bits)-1))/span); |
panzhan | 0:d80c66cb1b3a | 40 | } |
panzhan | 0:d80c66cb1b3a | 41 | |
panzhan | 0:d80c66cb1b3a | 42 | |
panzhan | 0:d80c66cb1b3a | 43 | float uint_to_float(int x_int, float x_min, float x_max, int bits){ |
panzhan | 0:d80c66cb1b3a | 44 | /// converts unsigned int to float, given range and number of bits /// |
panzhan | 0:d80c66cb1b3a | 45 | float span = x_max - x_min; |
panzhan | 0:d80c66cb1b3a | 46 | float offset = x_min; |
panzhan | 0:d80c66cb1b3a | 47 | return ((float)x_int)*span/((float)((1<<bits)-1)) + offset; |
panzhan | 0:d80c66cb1b3a | 48 | } |