Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
21611 Discussions

printing floating point

Altera_Forum
Honored Contributor II
4,722 Views

Hi, is there any ways to print float number in Nios II? Example: 

 

a = 100, b = 195 

 

c = (b-a)/100, so c = 0.95 

 

so, how to print c = 0.95? because when i print, the answer is 0.
0 Kudos
11 Replies
Altera_Forum
Honored Contributor II
3,256 Views

How are you trying to print it? I believe the libc employed by Nios II supports %f, %e, and %g. Keep in mind that the "small" or code reduced versions won't support floating point at all... 

 

Regards, 

 

slacker
0 Kudos
Altera_Forum
Honored Contributor II
3,256 Views

portion of my code: 

 

 

--- Quote Start ---  

 

dma_tick = (unsigned int)(end_dma - start_dma); 

pio_tick = (unsigned int)(end_pio - start_pio); 

freq = (unsigned int)(alt_timestamp_freq()); 

improve = (((pio_tick - dma_tick)/pio_tick) *100); 

 

printf("Printing report..\n\n"); 

printf("******* System Performance Analysis *******\n"); 

printf("Data size tested : %d Bytes\n", (length*4)); 

printf("Number of ticks per second : %u\n", freq); 

printf("Without DMA: Number of ticks: %u\n", pio_tick); 

printf("DMA: Number of ticks : %u\n", dma_tick);  

printf("* System Performance Improvement: %lf\n", improve); 

 

--- Quote End ---  

 

 

 

i cant print improve... 

i am using full lib, not small or reduced..
0 Kudos
Altera_Forum
Honored Contributor II
3,256 Views

how have you defined pio_tick and dma_tick? if you have non floating point in a division in C the integer aritmetic is used. 

 

int a = 195; 

int b = 100; 

float c; 

 

a - b=95 

c = 95/100 = 0 

 

try putting casts to float  

(float)(a-b)/(float)a
0 Kudos
Altera_Forum
Honored Contributor II
3,256 Views

Yeah! it works! Thanks!

0 Kudos
Altera_Forum
Honored Contributor II
3,256 Views

...what gabrigob wrote... It's a type issue that you're hitting here. I'd play around with that until you get it to work. 

 

Note that it is _definitely_ possible. I was the maintainer of the software for a 10g design (https://www.altera.com/support/software/download/refdesigns/ip/interface/dnl-10gbe-hardware-demo.jsp), for a bit, and did just this successfully in order to display the massive numbers related to packet-based stats. 

 

Also, I wasn't aware that the %lf was valid. I'm pretty sure the length modifier is only valid when printing integer values. 

 

Regards, 

 

slacker
0 Kudos
Altera_Forum
Honored Contributor II
3,256 Views

An integer divided by another integer regardless of how it's stored will be an integer result typecast to whatever the storage variable is after the operator completes. So after the division you would be left with an integer value of 0 which converted to floating point is just 0.0. 

 

More info: http://en.wikipedia.org/wiki/type_conversion
0 Kudos
Altera_Forum
Honored Contributor II
3,256 Views

slacker: yeah... i changed lf to f... thanks! 

 

BadOmen: thanks for the info!
0 Kudos
Altera_Forum
Honored Contributor II
3,256 Views

Hi, 

 

I have the problem that the floating point numbers are always printed as -ÿÿÿ.ÿÿÿÿÿÿ on the console window :( 

 

My code is, where f[z] is an array of floats 

 

f[z] = 2.0*f[z]/(412.0) - 1.0; 

f[z] = f[z] * 1024.0; 

printf ("\n %0f", (float)f[z]); 

 

the printf prints all other formats correctly like integers and chars ... 

 

thanks
0 Kudos
Altera_Forum
Honored Contributor II
3,256 Views

Are you using the small newlib C library? The small library doesn't support printing floating point values. Search for "footprint" in the Nios II software developer's handbook for more details. 

 

If that's not the case perhaps you are running low on memory and the stack and heap are colliding.
0 Kudos
Altera_Forum
Honored Contributor II
3,256 Views

Hi, 

I am not using the small c library! 

 

How can I increase the amount of memory allocated for the stack, heap? 

 

thanks
0 Kudos
Altera_Forum
Honored Contributor II
3,256 Views

Use a bigger memory.... The stack and heap assuming you assigned all the linker sections to the on-chip memory will be located at the end of the memory. If you don't have enough room to double the memory you could try using separate smaller memories and assigning the linker sections manually using the BSP Editor.

0 Kudos
Reply