• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ectara

Getting number of significant digits in float

6 posts in this topic

Is it possible to predict or count the number of significant decimal digits in a floating point number? For instance, if I were to output as a simple decimal number string, how would I count the number of digits before and after the radix point without actually counting and printing them? I'd like to count the number of decimal digits before actually parsing and outputting them. This would be useful for things like the %g fomat mark in a printf() format string, which decides whether %f or %e would be shorter, then outputs the shortest one, or deciding the exact size of a buffer to be written into before so that it can be preallocated. Simply not doing it is not an option.
0

Share this post


Link to post
Share on other sites
Significant decimal digits in base 10, or base 2?

For base 2, yes, you can tell how many significant digits are present trivially: there are 23 in single precision IEEE floats, and 52 in double-precision. You can use simple bit-counting algorithms to count how many of them are relevant in a particular value.

For base 10... well, not really. You have to convert the fraction to base 10 to know how many significant digits it represents (if it can even represent the base 10 value precisely!) at which point you've basically done a string format anyways. Edited by ApochPiQ
0

Share this post


Link to post
Share on other sites
Yeah, I was afraid of that. In base 10 is for what I am looking; I can easily extract the components of a float in an endian-safe manner. However, I was hoping to avoid having to do a dry run to calculate the digit count; if I hit the maximum precision and it can't be represented exactly, that's fine too. I'm mostly going for parsing to a string, so what data comes in and its accuracy is of little concern at this stage. If there is no other way, then I'll do that.
0

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1336185352' post='4937525']
For base 2, yes, you can tell how many significant digits are present trivially: there are 23 in single precision IEEE floats, and 52 in double-precision. You can use simple bit-counting algorithms to count how many of them are relevant in a particular value.
[/quote]

Ignoring denormalized floats, I think there's 24 bits of precision, since there's an [url="http://en.wikipedia.org/wiki/Single-precision_floating-point_format#IEEE_754_single-precision_binary_floating-point_format:_binary32"]implicit leading one[/url] that is not stored.

That aside, Bruce Dawson digs into floats in a very nice manner, and has got a [url="http://randomascii.wordpress.com/2012/03/08/float-precisionfrom-zero-to-100-digits-2/"]blog post about float precision in base 10[/url]. The TL;DR simplified answer is "... range from 6-9 decimal digits" and "Use 9 to play it safe."
0

Share this post


Link to post
Share on other sites
[quote name='clb' timestamp='1336204083' post='4937564']
[quote name='ApochPiQ' timestamp='1336185352' post='4937525']
...
[/quote]

Ignoring denormalized floats, I think there's 24 bits of precision, since there's an [url="http://en.wikipedia.org/wiki/Single-precision_floating-point_format#IEEE_754_single-precision_binary_floating-point_format:_binary32"]implicit leading one[/url] that is not stored.

That aside, Bruce Dawson digs into floats in a very nice manner, and has got a [url="http://randomascii.wordpress.com/2012/03/08/float-precisionfrom-zero-to-100-digits-2/"]blog post about float precision in base 10[/url]. The TL;DR simplified answer is "... range from 6-9 decimal digits" and "Use 9 to play it safe."
[/quote]

Right, I know the maximum decimal precision for a given float. I am looking for the number of significant digits in any given float. 1.0 would be one, 12321.4535 would be nine, 0.145 would be three, etc.
0

Share this post


Link to post
Share on other sites
In that case I believe the FLT_DIG constant is the one you are looking for, found in float.h. It is platform dependant, on most PCs it is 6.

That is the best precision that you can convert to decimal digits and back without loss.

Otherwise, the answer is that you ALWAYS have 23 bits of precision. The FPU doesn't arbitrarily stop midway, it carries out the entire operation.

If you want to keep track of how much of that answer you care about, you'll need to track it on your own in some other variable.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0