ECE 2036 Project

Dependencies:   mbed wave_player 4DGL-uLCD-SE

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers math_extra.cpp Source File

math_extra.cpp

00001 #include "math_extra.h"
00002 #include <stdint.h>
00003 
00004 int in_range(int x, int lower, int upper)
00005 {
00006     return (x > lower && x < upper);
00007 }
00008 
00009 float clamp(float x, float limit)
00010 {
00011     x = (x > limit) ? limit : x;
00012     x = (x < -limit) ? -limit : x;
00013     return x;
00014 }
00015 
00016 int sgn(float x)
00017 {
00018     return (x > 0.0) - (x < 0.0);
00019 }
00020 
00021 float coin_flip()
00022 {
00023     // Compute psuedorandom numbers! Don't worry about how this works.
00024     // If you really must know, search for "Linear-feedback shift register"
00025     static uint16_t rand = 0xA1B2;
00026     uint16_t bit = ((rand >> 0) ^ (rand >> 2) ^ (rand >> 3) ^ (rand >> 5) ) & 1;
00027     rand = (rand >> 1) | (bit << 15);
00028     return (rand & 1) ? 1.0 : -1.0;
00029 }