ECE 2036 Project

Dependencies:   mbed wave_player 4DGL-uLCD-SE

math_extra.cpp

Committer:
abraha2d
Date:
2019-11-21
Revision:
2:2042f29de6b7
Parent:
0:cf4396614a79

File content as of revision 2:2042f29de6b7:

#include "math_extra.h"
#include <stdint.h>

int in_range(int x, int lower, int upper)
{
    return (x > lower && x < upper);
}

float clamp(float x, float limit)
{
    x = (x > limit) ? limit : x;
    x = (x < -limit) ? -limit : x;
    return x;
}

int sgn(float x)
{
    return (x > 0.0) - (x < 0.0);
}

float coin_flip()
{
    // Compute psuedorandom numbers! Don't worry about how this works.
    // If you really must know, search for "Linear-feedback shift register"
    static uint16_t rand = 0xA1B2;
    uint16_t bit = ((rand >> 0) ^ (rand >> 2) ^ (rand >> 3) ^ (rand >> 5) ) & 1;
    rand = (rand >> 1) | (bit << 15);
    return (rand & 1) ? 1.0 : -1.0;
}