Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 3 of 3 FirstFirst 123
Results 21 to 27 of 27

Thread: How to show float numbers?

  1. #21
    Intern Contributor
    Join Date
    May 2001
    Location
    Warsaw Poland
    Posts
    73

    Re: How to show float numbers?

    float t;
    char buffer[256];
    sprintf( buffer, "%3.2f", &t );
    BUG
    printf("%f", ...) expects double, not a float.
    Code :
    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).]

  2. #22
    Senior Member OpenGL Pro
    Join Date
    Feb 2000
    Location
    France
    Posts
    1,118

    Re: How to show float numbers?

    Carmacksutra,

    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:

    Code :
    float t=2;
    printf("%f",t);
    or

    Code :
    double t=2;
    printf("%f",t);
    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 !

    Code :
    float f;
    double d;
    scanf("%f",&f);
    scanf("%lf",&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:

    Code :
    sprintf(buffer,"%3.2f",t);
    Regards.

    Eric

  3. #23
    Intern Contributor
    Join Date
    May 2001
    Location
    Warsaw Poland
    Posts
    73

    Re: How to show float numbers?

    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.
    Code :
    {
      m_pMouth->Open();
      m_pMouth->Insert(m_pFoot);
      sleep(10000);
    }
    Thanks for enlightment...
    (i was really consequent with this "(double)t" in my code )

  4. #24
    Senior Member OpenGL Pro
    Join Date
    Feb 2000
    Location
    France
    Posts
    1,118

    Re: How to show float numbers?



    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 !).

    Regards.

    Eric

  5. #25
    Member Regular Contributor nickels's Avatar
    Join Date
    Feb 2000
    Location
    Colorado
    Posts
    298

    Re: How to show float numbers?

    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);

  6. #26
    Junior Member Newbie
    Join Date
    Apr 2001
    Location
    Palo Alto, CA, USA
    Posts
    2

    Re: How to show float numbers?

    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.

  7. #27
    Senior Member OpenGL Guru knackered's Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    3,032

    Re: How to show float numbers?

    Eh? What's text formatting got to do with what card you're using?
    Knackered

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •