View Full Version : How to show float numbers?
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
[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).
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.
using namespace std;
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)
You mean a type-safe stream is a step backwards from a variable argument, buffer overflowing, type-ignorant, mark-up language?
[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 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.
EDIT: Fixed some typo's
[This message has been edited by lgrosshennig (edited 05-03-2002).]
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.
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.
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.
>>(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++
I am not sure that :
os << fixed() << setw(3) << setPrecision(2) << t;
is weirder or uglier than
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
sprintf( buffer, "%3.2f", &t );
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).]
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.
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 !
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 )
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";
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:
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?
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.