Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
math_ops.cpp@2:042ea0741254, 2022-07-26 (annotated)
- Committer:
- qwertyaz
- Date:
- Tue Jul 26 02:52:56 2022 +0000
- Revision:
- 2:042ea0741254
- Parent:
- 0:c725bf9715c4
test
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| MazeTaka | 0:c725bf9715c4 | 1 | |
| MazeTaka | 0:c725bf9715c4 | 2 | #include "../math_ops.h" |
| MazeTaka | 0:c725bf9715c4 | 3 | |
| MazeTaka | 0:c725bf9715c4 | 4 | |
| MazeTaka | 0:c725bf9715c4 | 5 | float fmaxf(float x, float y){ |
| MazeTaka | 0:c725bf9715c4 | 6 | /// Returns maximum of x, y /// |
| MazeTaka | 0:c725bf9715c4 | 7 | return (((x)>(y))?(x):(y)); |
| MazeTaka | 0:c725bf9715c4 | 8 | } |
| MazeTaka | 0:c725bf9715c4 | 9 | |
| MazeTaka | 0:c725bf9715c4 | 10 | float fminf(float x, float y){ |
| MazeTaka | 0:c725bf9715c4 | 11 | /// Returns minimum of x, y /// |
| MazeTaka | 0:c725bf9715c4 | 12 | return (((x)<(y))?(x):(y)); |
| MazeTaka | 0:c725bf9715c4 | 13 | } |
| MazeTaka | 0:c725bf9715c4 | 14 | |
| MazeTaka | 0:c725bf9715c4 | 15 | float fmaxf3(float x, float y, float z){ |
| MazeTaka | 0:c725bf9715c4 | 16 | /// Returns maximum of x, y, z /// |
| MazeTaka | 0:c725bf9715c4 | 17 | return (x > y ? (x > z ? x : z) : (y > z ? y : z)); |
| MazeTaka | 0:c725bf9715c4 | 18 | } |
| MazeTaka | 0:c725bf9715c4 | 19 | |
| MazeTaka | 0:c725bf9715c4 | 20 | float fminf3(float x, float y, float z){ |
| MazeTaka | 0:c725bf9715c4 | 21 | /// Returns minimum of x, y, z /// |
| MazeTaka | 0:c725bf9715c4 | 22 | return (x < y ? (x < z ? x : z) : (y < z ? y : z)); |
| MazeTaka | 0:c725bf9715c4 | 23 | } |
| MazeTaka | 0:c725bf9715c4 | 24 | |
| MazeTaka | 0:c725bf9715c4 | 25 | float roundf(float x){ |
| MazeTaka | 0:c725bf9715c4 | 26 | /// Returns nearest integer /// |
| MazeTaka | 0:c725bf9715c4 | 27 | |
| MazeTaka | 0:c725bf9715c4 | 28 | return x < 0.0f ? ceilf(x - 0.5f) : floorf(x + 0.5f); |
| MazeTaka | 0:c725bf9715c4 | 29 | } |
| MazeTaka | 0:c725bf9715c4 | 30 | |
| MazeTaka | 0:c725bf9715c4 | 31 | void limit_norm(float *x, float *y, float limit){ |
| MazeTaka | 0:c725bf9715c4 | 32 | /// Scales the lenght of vector (x, y) to be <= limit /// |
| MazeTaka | 0:c725bf9715c4 | 33 | float norm = sqrt(*x * *x + *y * *y); |
| MazeTaka | 0:c725bf9715c4 | 34 | if(norm > limit){ |
| MazeTaka | 0:c725bf9715c4 | 35 | *x = *x * limit/norm; |
| MazeTaka | 0:c725bf9715c4 | 36 | *y = *y * limit/norm; |
| MazeTaka | 0:c725bf9715c4 | 37 | } |
| MazeTaka | 0:c725bf9715c4 | 38 | } |
| MazeTaka | 0:c725bf9715c4 | 39 | |
| MazeTaka | 0:c725bf9715c4 | 40 | void limit(float *x, float min, float max){ |
| MazeTaka | 0:c725bf9715c4 | 41 | *x = fmaxf(fminf(*x, max), min); |
| MazeTaka | 0:c725bf9715c4 | 42 | } |
| MazeTaka | 0:c725bf9715c4 | 43 | |
| MazeTaka | 0:c725bf9715c4 | 44 | int float_to_uint(float x, float x_min, float x_max, int bits){ |
| MazeTaka | 0:c725bf9715c4 | 45 | /// Converts a float to an unsigned int, given range and number of bits /// |
| MazeTaka | 0:c725bf9715c4 | 46 | float span = x_max - x_min; |
| MazeTaka | 0:c725bf9715c4 | 47 | float offset = x_min; |
| MazeTaka | 0:c725bf9715c4 | 48 | return (int) ((x-offset)*((float)((1<<bits)-1))/span); |
| MazeTaka | 0:c725bf9715c4 | 49 | } |
| MazeTaka | 0:c725bf9715c4 | 50 | |
| MazeTaka | 0:c725bf9715c4 | 51 | |
| MazeTaka | 0:c725bf9715c4 | 52 | float uint_to_float(int x_int, float x_min, float x_max, int bits){ |
| MazeTaka | 0:c725bf9715c4 | 53 | /// converts unsigned int to float, given range and number of bits /// |
| MazeTaka | 0:c725bf9715c4 | 54 | float span = x_max - x_min; |
| MazeTaka | 0:c725bf9715c4 | 55 | float offset = x_min; |
| MazeTaka | 0:c725bf9715c4 | 56 | return ((float)x_int)*span/((float)((1<<bits)-1)) + offset; |
| MazeTaka | 0:c725bf9715c4 | 57 | } |