Couldnt store floating point result


(Daniel kirubakaran) #1

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?


(Bruce Hoult) #2

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.


(Daniel kirubakaran) #3

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.


(Bruce Hoult) #4

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


(Daniel kirubakaran) #5


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).


(Bruce Hoult) #6

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.


(Daniel kirubakaran) #7

I dont know why!!


(Bruce Hoult) #8

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


(Daniel kirubakaran) #9

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;

}

(Bruce Hoult) #10

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:


(Daniel kirubakaran) #11

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!!..


(Daniel kirubakaran) #12

Ok whatever! let me try to solve this issue.


(Bruce Hoult) #13

What happens if you add …

asm (".global _printf_float");

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


(Daniel kirubakaran) #14

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


(Daniel kirubakaran) #15

What was the actual problem?


(Bruce Hoult) #16

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()


(Daniel kirubakaran) #17

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.


(Bruce Hoult) #18

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.


(Daniel kirubakaran) #19

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