This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Problem while passing float as argument to function

Other Parts Discussed in Thread: EK-TM4C1294XL

Hi,

I am using CCS v6.1.0.00104 and TivaWare_C_Series-2.1.1.71 on EK-TM4C1294XL on Windiws 8 64 bit.
I am trying to convert float to string and want to add in to JSON string.
I am passing float as argument to the function, but in the function this float reads as -1.
If I redeclare the value or I define float as global variable and than its working fine.

Following is the code snippet.

uint32_t convertFloat2JSON(float fVal)
{
	int32_t i32Idx = 0;
	if(fVal<1.0 && fVal>-1.0){
		i32Idx = 1;
	}
	uint32_t uVal = (uint32_t) (fVal*powf(10.0, 3));
	UARTprintf("\nuVal:%d!!!!!",uVal);
	int iSize = ftostr(uVal);
	
	int i32Size=0;
	for(i32Size = iSize-1; i32Idx <= iSize; i32Idx++){
		UARTprintf(":%d",i32Idx);
		uint32_t iFPow = (uint32_t) powf(10, i32Size);
		uint32_t iVal = uVal / iFPow;
		UARTprintf(":%d*", iVal, iVal);
		uVal = uVal % iFPow;
		i32Size--;
	}

	return (1);
}

int ftostr(uint32_t fVal)
{
	// Extract integer part
	uint32_t ipart = (uint32_t)fVal;
	if(ipart == 0)	return 1;
	int i = 0;
	while(ipart){
		i++;
		ipart = ipart/10;
	}
	return i;
}

Can you please let me know what is the problem in this?

Thanks,
Bhavesh

  • Hi Bhavesh,
    I don't see anything that stands out as being wrong. Are you saying that when you call convertFloat2JSON passing in any float value as argument, the value of fVal inside the function is always -1?

    Regards,
    Moses
  • Yes Moses,
    Because printing Float to UART is not possible so I multiplied it by 1000 and converted to integer.
    so when I tried to do this step for passed float value it is always -1. But if float value is defined globally or locally than it is working fine.
  • Hi Bhavesh,

          I'm not able to reproduce this. Can you zip your project and attach it here?

    Thanks,

    Moses

  • Hi Moses,

    PFA code and pinout.c which I have modified for the use.

    Thanks,
    Bhavesh

    7612.hello.zip

    3201.pinout.c

  • Hi Bhavesh,

         I believe I've found the problem. I saw a lot of warnings in your code and fixing them made things work better. The compiler complained about a bunch of functions implicitly defined because of missing header files or no function prototype. I expected a compiler error but I guess the result is undefined if the compiler comes across functions that haven't been defined. I you need to add #include <math.h> for powf and also you need function definitions for addFloat2JSON and ftostr above main. Since those functions are implemented below main, the compiler doesn't recognize them when it's compiling main hence the warnings.

    Try adding these changes and let me know how it goes.

    Regards,

    Moses

  • Hi Moses,

    Thanks for your answer and it worked.

    But the thing I noticed is the problem is happening only when Floats are involved in arguments or as a return value. If other variable types are used than its working fine even if functions are in different files. Thing is there are no compilation error makes it everything is working fine. Anyways closing the issue and will force this as coding practice to the project.

    Thanks,
    Bhavesh

  • Bhavesh,
    The compiler behavior is probably undefined when it doesn't find function definitions. There probably isn't any explanable reason why float doesn't work and other types work.

    Regards,
    Moses