gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > printing a float var with iprintf()

#117325 - Yooser Naim - Sat Feb 03, 2007 9:18 pm

I'm messing around with a helloworld example program from devkitPro, and I've run into a problem:

When I try to print a float variable to screen using iprintf(), like this:

Code:
iprintf() ("blah blah %f", floatnumvar);


the emulator outputs:

Quote:
blah blah f


Would anybody know why? Every C/C++ reference I've looked at refers to iprintf() as simply printf(), and says that I should be able to print a float by inserting %f in the string constant parameter. Where is there documentation on these sort of deviations from standard C/C++ libraries?
How else could I display a float in a similar manner?

#117326 - Sausage Boy - Sat Feb 03, 2007 9:30 pm

Try with regular printf.
_________________
"no offense, but this is the gayest game ever"

#117329 - Yooser Naim - Sat Feb 03, 2007 10:42 pm

Yeah, I tried that too, but then printf() just displays "0.000000"
instead of the value assigned to the float variable. So I still haven't found a similar way of displaying a float. Thanks for the response, though; every bit of help gets me a little closer to clearing things up.

#117336 - Yooser Naim - Sat Feb 03, 2007 11:08 pm

Well, it turns out, printf() doesn't just display zeros;
I had actaully assigned the value of the expression 1/3 to the float variable(which was initialized to zero), but changing the expression to 1/3.0 causes it to be displayed. Why is this? Does it think of it as assigning an integer to a float, so it just doesn't assign anything at all?

#117341 - gmiller - Sat Feb 03, 2007 11:38 pm

if you use 1/3 then the expression is evaluated as integer and the interger part of 1/3 is zero. If you use 1/3.0 then the expression is mixed type so it is evaluated as real so you get .3333333333333 and so on. Standard rules for expressions.

#117343 - sgeos - Sun Feb 04, 2007 12:36 am

Try something like this:
Code:
char buffer[BUFFER_SIZE];
float myFloat = VALUE;

sprintf(buffer, "%f", myFloat);
iprintf("myFloat == %s", buffer);


-Brendan

#117441 - Quirky - Sun Feb 04, 2007 9:58 pm

Keep in mind that the DS (or GBA) does not have hardware floating points, so any floating point stuff has to be done in software. This means that printf and friends bring in all the floating point functions when linked to your binary. To keep the binary size down, newlib's iprintf can be used since it can perform integer operations only. Here is a iprintf man page: http://vmlinux.org/crash/mirror/www.objsw.com/docs/libc_65.html