HomeForumsWhat's newResources 
 
 
How to format numbers properly?
slinga - Aug 26, 2003
   slinga Aug 26, 2003 
How do I format number properly? I want the output in digit form (ie 1-9) without the excess zeroes that slPrintFX gives me. How do I overcome that? For example, if I have print the number of saves on a device it will give me 0.00030 instead of 30. I know I can multiply by 1000 or so, but is there a better way? Thanks.

   vbt Aug 26, 2003 
why not using a sprintf ?

Code:
  
char toto[50]; sprintf("%d",int_value); slPrint(toto,....);
!

   ExCyber Aug 26, 2003 
You could use slDispHex, though it has its own quirks. I think the SGL print routines are really only intended for debug output; developers were probably expected to write their own fancy-pants stuff if they wanted to do any kind of interesting printing.

   vreuzon Aug 27, 2003 
The sprintf is a good solution. You have to

Code:
  
#include <stdio.h>

in order to use it.

random doc : ttp://www.cplusplus.com/ref/cstdio/sprintf.html

   antime Aug 27, 2003 
If you don't need to print floating-point numbers, use iprintf instead. The floating-point support in sprintf and friends mean the floating-point emulation library needs to be linked as well, which will grow your binaries considerably. (iprintf is a newlib function, not standard C.)

   vreuzon Aug 29, 2003 
What is this floating point emulation library ? I can compile

Code:
  
float a = 19.7;

without linking to anything particular.

Am I missing something ?

   antime Aug 29, 2003 
It's automagically linked, so you don't have to do anything.

Also, your example will not link the library, it'll just put the floating-point constant "19.7" into the object file. The difference is when you actually perform operations using floating-point numbers.

This compiles into an 11093 byte object file: Code:
  
int main(void) { float a = 19.4; float b = 5.3; return 0; }

This, on the other hand, becomes a 29830 byte file: Code:
  
int main(void) { float a = 19.4; float b = 5.3; float c = a + b; return 0; }

Looking at the produced code with objdump we see that the difference in size is made up by the inclusion of soft-float routines. (IIRC, the soft-float code along with lots of other support code is located in libgcc.)

   vreuzon Aug 29, 2003 
Yes, but it is hard not to use floats, as it is even used in sl macros :

from sl_def.h

Code:
  
#define toFIXED(a) ((FIXED)(65536.0 * (a)))

   slinga Aug 29, 2003 
Thanks for your responses guys.

sldisphex: displays hex argh.

sprintf: works, but the text seems to be in slightly different positions. Oh well I'll fomart it again.

iprintf: I couldn't ge this to work.

   antime Aug 30, 2003 

  
Originally posted by vreuzon@Aug 29, 2003 @ 03:22 PM

Yes, but it is hard not to use floats, as it is even used in sl macros


As long as those macros are used only in constant expressions, they should be optimized away at compile-time.