View Full Version : How to show float numbers?

05-02-2002, 02:43 PM
Hi, my teacher,

I can show texts of char type using
But how to show float number in OpenGL?


05-02-2002, 02:48 PM
You use the same technic, but you convert the float number to a string. Use your favorite library or your own code to do that.

NAOQ - not a advanced opengl question


05-02-2002, 02:56 PM
[QUOTE]Originally posted by V-man:
[B]You use the same technic, but you convert the float number to a string. Use your favorite library or your own code to do that.

I have convert it to a string. But how to show decimal's position. I want to know whether to show float number directly,which needn't convert or use own code.

05-02-2002, 06:54 PM

05-03-2002, 01:32 AM
NEVER use sprintf with floating point numbers. Always a risk of a crash. Use snprintf instead to guard your buffer sizes.

!!!!! (speaking from experience of course).

Michael Steinberg
05-03-2002, 01:38 AM
As you can use %5.5 you can avoid the crash.

05-03-2002, 02:06 AM
Originally posted by Michael Steinberg:
As you can use %5.5 you can avoid the crash.

I agree with mike, you simply have to make sure your buffer can accomidate the number of digits you are going to generate. In reality this isn't a big deal. Just make your buffer a resonable size and its an issue that should never come up. I really don't think that this borders on the line of a buffer overrun type of hack.



05-03-2002, 03:07 AM
5.5 won't limit you to 5 output characters.
It simply means miniumum of 5 characters, with a precision of 5 decimal places.

05-03-2002, 05:43 AM
#include <sstream>
using namespace std;
ostringstream s;
s << myFloat;
// the number as a string is now available in s.str()

... to avoid buffer overruns.

05-03-2002, 07:08 AM
Paranoia! Will destroy ya!

(By the way, printf and sprintf are so incredible to use compared to C++ formatting statements. You want to talk about one major step backwards in the name of progress, this is it)

05-03-2002, 09:23 AM
You mean a type-safe stream is a step backwards from a variable argument, buffer overflowing, type-ignorant, mark-up language?

-- Zeno

[This message has been edited by Zeno (edited 05-03-2002).]

05-03-2002, 09:52 AM
no he means that with all those new features added, simple formating of the output got lost (it _IS_ still possible, but not at all that nice as before..)

that is true..

05-03-2002, 01:33 PM
Exactly. printf and sprintf are so immensly easy and efficient to use.
I tried to learn how to use the C++ formatters once and just laughed. It would take 1000 characters of code to do what takes 20 with sprintf.
I had a job where they tried to make me use that style of output and I quit, got a new job (not just because of that, but because of general C++ overzealous religousness).

05-03-2002, 01:53 PM

Sorry guys but you are really stressing a trivial issue.

Yes using streams is SAFE but you are buying it with a lot of overhead.

If you are doing a %1.5f sprintf on a char[256] buffer there is NO way you will EVER over run the buffer and its fast and easy too....

BTW: Knackered is right the width.precision only specifies the MININUM length of the result.


LG http://www.opengl.org/discussion_boards/ubb/biggrin.gif

EDIT: Fixed some typo's

[This message has been edited by lgrosshennig (edited 05-03-2002).]

05-03-2002, 03:23 PM
my teachers,


I want to draw some objects with float numbers at (x,y,z). If using glListBase(1000); glRasterPos3f(x,y,z);glCallLists(Twidth,GL_UNSIGNE D_BYTE,Text);,I have to use own code to convert it to a string with finding a decimal position (_fcvt(...)).If using sprintf/printf(), I can't see the float numbers near objects drawed, namely I can't show the float numbers at given (x,y,z).

How to directly show float numbers at (x,y,z) in OpenGL with drawing objects?


05-04-2002, 09:27 PM
You're misuberstanding the advice, ding http://www.opengl.org/discussion_boards/ubb/wink.gif

Used sprintf to format the output string (not printf!), then draw the formatted string using your existing calls.


char buffer[512];
sprintf (buffer, "%3.5f", output_string);


You don't need to do anything special with OpenGL to draw floats. It's all about formatting the output.

Michael Steinberg
05-04-2002, 10:10 PM
If you're totally into safety then you might wanna use _ecvt or however it is called, it returns a pointer to a static char array filled with the floating point number.

05-05-2002, 01:50 AM
>>(By the way, printf and sprintf are so incredible to use compared to C++ formatting statements. You want to talk about one major step backwards in the name of progress, this is it)<<

yeah i remember from when i started c++ in about 1996 (c in 1986/7) what struck me with c++.
the c++ way with formatting strings,
i thought it was a joke?
no i mean seriously!
i still think it nowadays, its a joke right?

btw i love c++

05-05-2002, 08:09 AM
I am not sure that :

ostringstream os;
float t;

os << fixed() << setw(3) << setPrecision(2) << t;

is weirder or uglier than

char buffer[256];
sprintf( buffer, "%3.2f", &t );

I will admit it is longer to type though.

[This message has been edited by Gorg (edited 05-05-2002).]

05-07-2002, 06:24 PM
humm....I have something to say to this non-opengl debate hehehe

When I started learning c++ and used nothing of the printf family i thought that it was great.

But when I discovered that I could use sprintf and printf and all that stuff, I said to myself "f*ck the c++ text formatting method it's ugly" ! hahaha

maybe(probably) it's more safe though....
well...I love risks...

*Let's have a funny day*

05-08-2002, 04:08 AM
float t;
char buffer[256];
sprintf( buffer, "%3.2f", &t );
BUG http://www.opengl.org/discussion_boards/ubb/smile.gif
printf("%f", ...) expects double, not a float.

sprintf( buffer, "%3.2f", (double)t );
Ding, maybe you too overlooked it in your code?

[This message has been edited by Carmacksutra (edited 05-08-2002).]

05-08-2002, 04:34 AM

For me the main bug is the use of '&' !

You give a pointer when using scanf, not printf !!!!!

The "printf" takes the value itself and it is defined as using the ellipsis "..." to tell that there is an unknown number of parameters.

All floating points values are converted to doubles when using the ellipsis.

Anyway, doing:

float t=2;


double t=2;

will produce the same result (and in the first case, t will be converted to a double when passed to printf !).

Now, when you use scanf, that's another matter !

float f;
double d;

And the "l" indicates that the pointer points to a "long value" (i.e. long int or double).

Anyway, to go back to Gorg's piece of code, it should be:




05-08-2002, 05:14 AM
For me the main bug is the use of '&'
I thought that "bug" was so obvious that it had to be a typo.

All floating points values are converted to doubles when using the ellipsis. http://www.opengl.org/discussion_boards/ubb/redface.gif

}Thanks for enlightment...
(i was really consequent with this "(double)t" in my code http://www.opengl.org/discussion_boards/ubb/frown.gif )

05-08-2002, 06:32 AM

To be honest, I found that only some weeks ago while I was looking at a way to create a function with an unknown number of arguments and it stayed somewhere in my memory...

It's actually interesting to read the docs about the ellipsis in MSDN (ok, it has nothing to do with OpenGL but anyway !).



05-08-2002, 03:30 PM
For a lot of compilers printf is type safe:
(e.g. they will bark if you try)

char *mychar = "blah";

printf("%f", mychar);

Warning:Officious compiler thinks you are not very smart and is warning you that there is a type-mismathc in line 3 (printf).

This is usually ok, if you want to do this on purpose you can use a cast and get what you want:

float var;

printf("The dang address of the float is:%08x\n", (int) &var);

05-10-2002, 09:05 AM
To position a string correctly, you need to call GetTextExtendPoint() to find out the size of the string.

But there is still a problem: different cards have different implementation on text drawing. I develeped my OpenGL program on ATI Radeon 8500. The text position is always wrong when running on any VisionTek-nVidia cards.

05-10-2002, 11:32 AM
Eh? What's text formatting got to do with what card you're using?