Unable to print Floating point number

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Am using Atmel Studio 7 to program my Atmel SAM E70 Xplained Development board.

Am unable to print floating point numbers.

 

printf("Float : %f",11.11);

Its output will be

Float : f

Don't know why.

Please Help.

This topic has a solution.

Silly beans are always silly
:)

Last Edited: Sat. Jan 14, 2017 - 06:36 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It also gives strange results while multiplying a floating point number.

 

11*2

gives

22

And

11.0*2

gives

13

 

I wonder what is happening.

Silly beans are always silly
:)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

Have you selected print with floating point options? You get a choice as to what features you want with printf to optimise size.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How to do that?

Silly beans are always silly
:)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, Please tell me how to get those floating point variable working.

Silly beans are always silly
:)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, Please let me know how to enable this option.

Silly beans are always silly
:)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

10 seconds of googling:
http://community.atmel.com/forum/samc21-printf-not-printing-float-values
I'm staggered that you waited over a week for me to google the answer for you! The keywords were: atmel studio printf float

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have already done these stuffs before.
But t didn't work for me.

Silly beans are always silly
:)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

"didn't work" - it's hard to move forward with that response. What precisely did you do and what "didn't work".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So far, this is what I have come to know.

 

Someone from Atmel asked me to following steps in my project to get floating point working.

1. Include

#include "arm_math.h"

in ‘main.c’ file.

2.Enable FPU unit -> call

fpu_enable()

function in main() in main.c

3. In Atmel Studio

Project -> ProjectName Properties … -> Toolchain -> ARM/GNU C Compiler -> Symbols

Please ensure that the following symbol has been added.

ARM_MATH_CM7 = true

4. Add the following two flags in compiler settings

Project -> ProjectName Properties … -> Toolchain -> ARM/GNU C Compiler -> Miscellaneous -> Other Flags ->

-mfloat-abi=hard -mfpu=fpv5-d16

5. Add the following two flags in linker settings

Project -> ProjectName Properties … -> Toolchain -> ARM/GNU Linker->
Miscellaneous -> Linker Flags -> -mfloat-abi=hard -mfpu=fpv5-d16

6. Change the optimization to ‘O0’

Project -> ProjectName Properties … -> Toolchain -> ARM/GNU C Compiler-> Optimization -> Optimization Level -> None(-O0)

 

 

I modified my project properties as per their suggestion.

 

Then I included below mentioned strand in my project.

float x =3.2,y=7.1,z=0;
z= x+y;

But it seems like the code is not working even though it got compiled & uploaded successfully.

 

I did add a few more line to the code to check its working.

Firstly, I enabled serial communication & the then did give a printf command.

printf("z=%f",z)

What I saw in the serial terminal was
z=f

Since x=3.2 & y=7.1
expected value of z is 10.3

Assuming something went wrong with printf, I did add a few more lines like

if(z==10.3)
  ioport_set_pin_level(LED0_GPIO,LOW);
else
  ioport_set_pin_level(LED0_GPIO,HIGH);

But that too failed to respond.

So, what is happening?
Any Idea?

Silly beans are always silly
:)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Since floats may not be exactly represented,  try something like:

if (fabsf(10.3 - z) < 0.001) {

 

Edit: abs to fabsf

David (aka frog_jr)

Last Edited: Thu. Jan 12, 2017 - 01:53 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Came to know about few more details.

 

To print floating point numbers, we are supposed to remove two lines from the project properties toolchain.

 

Project -> ProjectName Properties … -> Toolchain -> ARM/GNU C Compiler -> Symbols

(Remove)

scanf=iscanf
printf=iprintf

With this symbol,printf() uses only iprintf (integer printf() version - reduced version) by default from library.

 

This is the reason why I used to get

z=f

Other modifications are just like the ones mentioned above.

 

Project -> ProjectName Properties … -> Toolchain -> ARM/GNU C Compiler -> Miscellaneous -> Other Flags ->

-mfloat-abi=softfp -mfpu=fpv5-sp-d16

Do not simply paste it in there, to the end of all the flags, you need to make sure that these flags are defined properly.

 

Again, make sure that

 

Project -> ProjectName Properties … -> Toolchain -> ARM/GNU Linker-> Libraries->

 

contain the following line.

libarm_cortexM7lfsp_math_softfp

Thank you for the help guys.

Thanks to guys in Atmel Support Team as well.

Silly beans are always silly
:)

Last Edited: Sat. Jan 14, 2017 - 06:36 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The critical thing for me, using V71, is to remove

 

printf=iprintf

 

in ARM/GNU Compiler, Symbols options

 

Best,
Andrew