Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 568145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:09:29+00:00 2026-05-13T13:09:29+00:00

I am following some beginner tutorials for OpenGL in c++ but as I started

  • 0

I am following some beginner tutorials for OpenGL in c++ but as I started off as a c# programmer it has made me take a lot of things for granted. So my problem occurred when I was debug printing my FPS reading to the output. I think the method was something like DebugPrintString off the top of my head which took a char* and basically i was printing “FPS: x”. I was using scanf_s to place the fps value into the character array but this is where my problem lies. How big does the character array have to be?

Let me elaborate a bit more: my FPS reading is stored as a float as the frames/seconds usually ends up not being a nice number. So my number could be 60, or it could be 59.12345. 60 would only need 2 bytes and 59.12345 would need 8 (1 for the period). So I thought “Oh ok i need to count the amount of digits it has, no problem!” Boy was I in for a shock.

I made a method to count the digits, counting the left hand side of the decimal place was easy, just first of all cast it as a int to remove the decimal points and divide by 10 (actually I think I had some bitshifting there) and count the amount of times i can do that until i reach 0. And now to count the digits on the right hand side, well i’ll just multiply by 10, subtract the digit, and do this until it reaches zero. The method would usually return 32 i think it was. So i WTF’d and had a look at it in debug, turns out when you multiply the float effectively moving the digit columns up because of the well known precision issue it just appended another digit!

I did some major googling, but couldn’t really find anything above char str[128] and scanf if in then do strlen(str) minus 1 (null terminator). But i was hoping for a more elegant solution. In the end i just casted it as an int and allowed enough for 9999 fps, also added a check to see if the fps > 9999 but I don’t think thats ever going to happen. Better safe than SEG FAULT 🙁

TLDR: Is there a way to get the amount of digits in a float? How does scanf do it?!

Sorry for long post, just wanted to share my frustation >:D

Edit: spelling errors

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:09:29+00:00Added an answer on May 13, 2026 at 1:09 pm

    Considering it’s C++ we’re talking about, why not go the STL way?

    Precision 5 places after decimal dot, may be variable amount of characters:

    std::stringstream ss;
    ss << std::setprecision (5) << std::fixed << f; 
    std::string fps = ss.str();
    

    Precision maximum 5 significant digits:

    std::stringstream ss;
    ss << std::setprecision (5) << f; 
    std::string fps = ss.str();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 268k
  • Answers 268k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have conceded defeat and will just change the combo… May 13, 2026 at 1:10 pm
  • Editorial Team
    Editorial Team added an answer setTimeout(function() { Tableslide_down(content, height); },10); First parameter of setTimeout can… May 13, 2026 at 1:10 pm
  • Editorial Team
    Editorial Team added an answer I think there's no way to make that ajax call… May 13, 2026 at 1:10 pm

Related Questions

Can anyone list the requirements (i.e. any books, tutorials, libraries etc) to build an
I am following a tutorial from nettuts, specifically here: http://blog.themeforest.net/tutorials/jquery-for-absolute-beginners-day-11/ I am 3/4's of
I'm not sure what it is about me but I seem to learn and
I recently decided to try some java web development, so I installed xampp, the
I am a beginner in SVN.I have the SVN directory structure like this: |-trunk

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.