Couldnt store floating point result

I want to hold a floating point data in a variable ,when i use float data type it is not working it seems!. when i change it to an integer i get the values.But i need exact values.What should i do?

1 Like

This is not very clear.

Floating point values are not exact, they are approximate. Integer values are exact.

But please show your source code, and tell what you are expecting and what you are getting.

like ,for instance if i want to store a value like this 27.6754 into a variable , then i should use float as data type right?
to get only 27 integer is fine.

here is my code

float celsius(int fahrenheit){

float cel = ((fahrenheit-32) * 0.5556);

return cel ;

}

but i am not getting the return value.

How do you know you are not getting the return value?

…
float result2 = celsius(result1);
printf(“Temperature : %dF / %f C\n”,result1,result2);
…

Through this.

I tried by changing the data type into integer and i am getting the values(only the exact value, not the values after decimal point).

In freedom-e-sdk I modified software/hello/hello.c as follows:

#include <stdio.h>

float celsius(int fahrenheit){
    float cel = ((fahrenheit-32) * 0.5556);
    return cel;
}

int main()
{
    int F = 70;
    printf("hello world! %d F is %f C\n", F, celsius(F));
    return 0;
}

The result:

core freq at 260561306 Hz
hello world! 70 F is 21.112801 C

Progam has exited with code:0x00000000

Looks fine to me.

I dont know why!!

It’s impossible to say without seeing your whole program.

here is my code

#include <platform.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utility/delay_s.c"
#include "utility/adc_MCP3008.c"


	    
#define _CS 10            
#define _MOSI 11           
#define _MISO 12      
#define _CLK 13   


float celsius(int fahrenheit);

uint32_t  _cs = _CS, _mosi = _MOSI,_miso = _MISO ,_sclk = _CLK ;
 


int main(){
	
	while(1){

	int result1=mcp3008_value(0,_sclk,_miso,_mosi,_cs);
	float result2 = celsius(result1);
	printf("Temperature : %dF / %fC\n",result1,result2);
	delay(2);

	}
	
   
}

float celsius(int fahrenheit){

 float cel = ((fahrenheit-32) * 0.5556);
 
   return cel;

}

What’s very strange is that the spaces around the “/” in your output have disappeared.

Are you sure the code you’re running is the code you’re looking at? Add my name into the printf string and see if it prints when you run it :slight_smile:

ha ha!..

i tried all the possibilities, and yes i am sure about this code.
when i change it to 'int ’

int main(){

while(1){

int result1=mcp3008_value(0,_sclk,_miso,_mosi,_cs);
int result2 = celsius(result1);
printf("Temperature : %dF/%dC\n",result1,result2);
delay(2);

}

}

int celsius(int fahrenheit){

int cel = ((fahrenheit-32) * 0.5556);

return cel;

}

its working!!..

Ok whatever! let me try to solve this issue.

What happens if you add …

asm (".global _printf_float");

… in the top level of your program? For example just after the #includes.

1 Like

yahoo!! Its working Mr.Bruce. you are great !

What was the actual problem?

The problem is you have built “Newlib nano” instead of the standard “Newlib” built by our standard instructions, and it doesn’t include support for floating point in printf() or scanf()

I couldn’t understand actually, since i am a beginner.If you don’t mind can you tell me where i could find that Newlib nano and Newlib?
And in which file i should keep this code " asm (".global _printf_float");" as a common one.So that i may not be getting this same issue again in future.

I don’t even know how to get the freedom-e-sdk build to generate or use Newlib nano. I’ve never done it.

It’s not something that can happen by accident!

It doesn’t matter what file you put that asm() in, as long as it’s linked.

Oh okay.
Anyways thanks a lot.
To me its a big thing, Thanks for your help.

I also got this issue, cannot print a float variable. I used Freedom Studio to generate a Freedom E SDK Software Project.
My example code:

float a = 12.34;
printf("a = %f", a);

Output

a = 

I followed @bruce comments and added asm (".global _printf_float"); on top of my program. Then it could print variable with float type.

1 Like