1
Dependencies: mbed-dev-f303 FastPWM3
math_ops.cpp@34:51647c6c500d, 2017-11-08 (annotated)
- Committer:
- benkatz
- Date:
- Wed Nov 08 15:18:18 2017 +0000
- Revision:
- 34:51647c6c500d
- Parent:
- 26:2b865c00d7e9
- Child:
- 45:aadebe074af6
testing new things
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benkatz | 20:bf9ea5125d52 | 1 | |
benkatz | 20:bf9ea5125d52 | 2 | #include "math_ops.h" |
benkatz | 20:bf9ea5125d52 | 3 | |
benkatz | 20:bf9ea5125d52 | 4 | |
benkatz | 20:bf9ea5125d52 | 5 | float fmaxf(float x, float y){ |
benkatz | 25:f5741040c4bb | 6 | /// Returns maximum of x, y /// |
benkatz | 20:bf9ea5125d52 | 7 | return (((x)>(y))?(x):(y)); |
benkatz | 20:bf9ea5125d52 | 8 | } |
benkatz | 20:bf9ea5125d52 | 9 | |
benkatz | 20:bf9ea5125d52 | 10 | float fminf(float x, float y){ |
benkatz | 25:f5741040c4bb | 11 | /// Returns minimum of x, y /// |
benkatz | 20:bf9ea5125d52 | 12 | return (((x)<(y))?(x):(y)); |
benkatz | 20:bf9ea5125d52 | 13 | } |
benkatz | 20:bf9ea5125d52 | 14 | |
benkatz | 20:bf9ea5125d52 | 15 | float fmaxf3(float x, float y, float z){ |
benkatz | 25:f5741040c4bb | 16 | /// Returns maximum of x, y, z /// |
benkatz | 20:bf9ea5125d52 | 17 | return (x > y ? (x > z ? x : z) : (y > z ? y : z)); |
benkatz | 20:bf9ea5125d52 | 18 | } |
benkatz | 20:bf9ea5125d52 | 19 | |
benkatz | 20:bf9ea5125d52 | 20 | float fminf3(float x, float y, float z){ |
benkatz | 25:f5741040c4bb | 21 | /// Returns minimum of x, y, z /// |
benkatz | 20:bf9ea5125d52 | 22 | return (x < y ? (x < z ? x : z) : (y < z ? y : z)); |
benkatz | 20:bf9ea5125d52 | 23 | } |
benkatz | 34:51647c6c500d | 24 | |
benkatz | 34:51647c6c500d | 25 | float roundf(float x){ |
benkatz | 34:51647c6c500d | 26 | /// Returns nearest integer /// |
benkatz | 34:51647c6c500d | 27 | |
benkatz | 34:51647c6c500d | 28 | return x < 0.0f ? ceilf(x - 0.5f) : floorf(x + 0.5f); |
benkatz | 34:51647c6c500d | 29 | } |
benkatz | 20:bf9ea5125d52 | 30 | |
benkatz | 20:bf9ea5125d52 | 31 | void limit_norm(float *x, float *y, float limit){ |
benkatz | 25:f5741040c4bb | 32 | /// Scales the lenght of vector (x, y) to be <= limit /// |
benkatz | 20:bf9ea5125d52 | 33 | float norm = sqrt(*x * *x + *y * *y); |
benkatz | 20:bf9ea5125d52 | 34 | if(norm > limit){ |
benkatz | 20:bf9ea5125d52 | 35 | *x = *x * limit/norm; |
benkatz | 20:bf9ea5125d52 | 36 | *y = *y * limit/norm; |
benkatz | 20:bf9ea5125d52 | 37 | } |
benkatz | 20:bf9ea5125d52 | 38 | } |
benkatz | 26:2b865c00d7e9 | 39 | |
benkatz | 26:2b865c00d7e9 | 40 | int float_to_uint(float x, float x_min, float x_max, int bits){ |
benkatz | 26:2b865c00d7e9 | 41 | float span = x_max - x_min; |
benkatz | 26:2b865c00d7e9 | 42 | float offset = x_min; |
benkatz | 26:2b865c00d7e9 | 43 | return (int) ((x+offset)*((float)((1<<bits)-1))/span); |
benkatz | 26:2b865c00d7e9 | 44 | } |
benkatz | 26:2b865c00d7e9 | 45 | |
benkatz | 26:2b865c00d7e9 | 46 | float uint_to_float(int x_int, float x_min, float x_max, int bits){ |
benkatz | 26:2b865c00d7e9 | 47 | float span = x_max - x_min; |
benkatz | 26:2b865c00d7e9 | 48 | float offset = x_min; |
benkatz | 26:2b865c00d7e9 | 49 | return ((float)x_int)*span/((float)((1<<bits)-1)) + offset; |
benkatz | 26:2b865c00d7e9 | 50 | } |