I follow some beginner tutorials for OpenGL in C ++, but when I started programming in C #, it made me take a lot of things for granted. So my problem arose when I was debugging the printing of my FPS readout output. I think the method was something like DebugPrintString from the top of the head, which took char *, and basically I printed "FPS: x". I used scanf_s to put the fps value in an array of characters, but this is where my problem is. How large should the character array be?
Let me tell you in more detail: FPS reading is stored as a float, since frames / seconds usually end with a bad number. So my number could be 60, or it could be 59.12345. 60 will require only 2 bytes, and 59.12345 - 8 (1 per period). So I thought: "Oh, well, I need to count the number of digits, no problem!" The boy was shocked.
I made a method of counting numbers, counting the left side of the decimal place, it was simple, just first of all, throw it as an int to remove the decimal points and divide by 10 (in fact, I think I had some bit offset there) and count the number of times I can do this until it reaches 0. And now, to count the numbers on the right side, well, I just multiply by 10, subtract the number and do it until it reaches zero. The method usually returns 32, I think it is. So, I am WTF'd and looked at it in debugging, it turns out that when you multiply a float, effectively moving the columns of digits due to a known accuracy problem, it just added another digit!
I did some major searches, but couldn't 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 ran it as an int and allowed enough for 9999 frames per second, also added a check to find out if there are fps> 9999, but I donβt think it will ever happen. Safer than SEG FAULT: (
TL; DR: is there a way to get the number of digits in a float? How does scanf do this ?!
Sorry for the long post, just wanted to share my bite>: D
Edit: spelling errors