Treehopper C# API
Numbers Class Reference

Utilities for dealing with numbers More...

Static Public Member Functions

static double Map (double input, double fromMin, double fromMax, double toMin, double toMax)
 Map an input value from the specified range to an alternative range. More...
 
static double Constrain (this double x, double a=0.0, double b=1.0)
 Constrains a number to be within a range. More...
 
static T Constrain< T > (T x, T a, T b)
 Constrains a number to be within a range. More...
 
static float Lerp (float value1, float value2, float amount)
 Linearly interpolates between two values. More...
 
static int BcdToInt (int val)
 Convert 4-bit BCD nibbles into an integer More...
 
static bool CloseTo (this double val, double comparedTo, double error=0.0001)
 Determines if this number is close to another. More...
 
static bool CloseTo (this float val, float comparedTo, double error=0.0001)
 Determines if this number is close to another. More...
 
static bool CloseTo (this int val, int comparedTo, double error=0.0001)
 Determines if this number is close to another. More...
 

Detailed Description

Utilities for dealing with numbers

Member Function Documentation

◆ Map()

static double Map ( double  input,
double  fromMin,
double  fromMax,
double  toMin,
double  toMax 
)
static

Map an input value from the specified range to an alternative range.

Parameters
inputThe value to map
fromMinThe minimum value the input can take on
fromMaxThe maximum value the input can take on
toMinThe minimum output
toMaxThe maximum output
Returns
The mapped value

This is a clone of the Arduino map() function (http://arduino.cc/en/reference/map).

Map the AnalogIn voltage (which ranges from 0-5) to a Pwm.Value (which ranges from 0-1).

double pwmVal = Utilities.Map(myTreehopperBoard.Pin1.AnalogIn.Value, 0, 5, 0, 1);
myTreehopperBoard.Pin2.Pwm.DutyCycle = pwmVal;

◆ Constrain()

static double Constrain ( this double  x,
double  a = 0.0,
double  b = 1.0 
)
static

Constrains a number to be within a range.

Default range is 0-1

Parameters
xthe number to constrain
athe lower end of the range
bthe upper end of the range
Returns
the constrained number

◆ Constrain< T >()

static T Constrain< T > ( x,
a,
b 
)
static

Constrains a number to be within a range.

Parameters
xthe number to constrain
athe lower end of the range
bthe upper end of the range
Returns
the constrained number
Type Constraints
T :IComparable 

◆ Lerp()

static float Lerp ( float  value1,
float  value2,
float  amount 
)
static

Linearly interpolates between two values.

This method is a less efficient, more precise version of Lerp. See remarks for more info.

Parameters
value1Source value.
value2Destination value.
amountValue between 0 and 1 indicating the weight of value2.
Returns
Interpolated value.

This method performs the linear interpolation based on the following formula:

((1 - amount) * value1) + (value2 * amount)

. Passing amount a value of 0 will cause value1 to be returned, a value of 1 will cause value2 to be returned. This method does not have the floating point precision issue that Lerp has. i.e. If there is a big gap between value1 and value2 in magnitude (e.g. value1=10000000000000000, value2=1), right at the edge of the interpolation range (amount=1), Lerp will return 0 (whereas it should return 1). This also holds for value1=10^17, value2=10; value1=10^18,value2=10^2... so on. For an in depth explanation of the issue, see below references: Relevant Wikipedia Article: https://en.wikipedia.org/wiki/Linear_interpolation#Programming_language_support Relevant StackOverflow Answer: http://stackoverflow.com/questions/4353525/floating-point-linear-interpolation#answer-23716956

◆ BcdToInt()

static int BcdToInt ( int  val)
static

Convert 4-bit BCD nibbles into an integer

Parameters
valThe value (concatenated 4-bit nibbles) to convert.
Returns
The integer number

◆ CloseTo() [1/3]

static bool CloseTo ( this double  val,
double  comparedTo,
double  error = 0.0001 
)
static

Determines if this number is close to another.

Useful for comparing doubles and floats

Parameters
valThis number
comparedToThe other number
errorThe allowable error; defaults to something sensible
Returns
True if the two numbers are within "error" of each other; false otherwise

◆ CloseTo() [2/3]

static bool CloseTo ( this float  val,
float  comparedTo,
double  error = 0.0001 
)
static

Determines if this number is close to another.

Useful for comparing doubles and floats

Parameters
valThis number
comparedToThe other number
errorThe allowable error; defaults to something sensible
Returns
True if the two numbers are within "error" of each other; false otherwise

◆ CloseTo() [3/3]

static bool CloseTo ( this int  val,
int  comparedTo,
double  error = 0.0001 
)
static

Determines if this number is close to another.

Useful for comparing doubles and floats

Parameters
valThis number
comparedToThe other number
errorThe allowable error; defaults to something sensible
Returns
True if the two numbers are within "error" of each other; false otherwise

The documentation for this class was generated from the following file: