Matlab fix Function to C

I get function fix in Matlab. fix(X) rounds the elements of X to the nearest integers towards zero. For example if we have data with value :

X = [-1.9, -0.2, 3.4, 5.6, 7.0]

When we use fix function with this data, so we get output :

Y = [1.0000        0             3.0000        5.0000 7.0000]

I try to create C function to convert this fix Matlab function. This my C code to convert fix Maltab function :

int fix(float A)
{
	int sign;
	int fout, out;

	sign = A<0 ? -1:1;
	fout = abs(A);
	out = floor(A) * sign;

	return (out);
}

float abs(float x)
{
	if(x<0)
		return(x*(-1));
	else
		return x;
}

This is my simple code to convert fix function Matlab to C.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.