Home Forum Community Jumps in measured data Reply To: Jumps in measured data

#5484
Robert
Participant

As suggested per email, but to keep this top compleet: Why not using standard 16-bit signed integers?

Conversions back and forth with a little test program would look like this:


#include <stdio.h>
#include <stdint.h>

#define FIXED_POINT_FACTOR	100.0;

int16_t double2fixed(double val)
{
	val *= FIXED_POINT_FACTOR;

	if (val > INT16_MAX)
		return INT16_MAX;
	if (val < INT16_MIN)
		return INT16_MIN;

	return val;
}

double fixed2double(int16_t val)
{
	return val / FIXED_POINT_FACTOR;
}

int main(int argc, char *argv[])
{
	double  value;

//	for (value = -328.70; value < -326.01; value += 0.1)  /* Test bottom edge case */
//	for (value = 326.00; value < 329.01; value += 0.1)    /* Test top edge case */
	for (value = -2.0; value < 2.01; value += 0.1)        /* Test around zero */
	{
		int16_t  converted = double2fixed(value);
		double   back = fixed2double(converted);

		printf("value = %f, converted = %d, back = %f\n", value, converted, back);
	}
}