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