• 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
EvilWeebl

floating point precision problem

9 posts in this topic

Hi all,
More than just solving this problem I'd love if someone could explain to me the cause for this:

I'm doing a winforms projects and have a picture box with an image. I drag a rectangle over that image and get the bounds of that rectangle. The thing is that I need to store these bounds as UV coordinates and as the picture box Y is at the top and UV's Y(or V) starts at bottom I need to flip them. I do this like so :

return new RectangleF(bounds.X, 1.0f - (bounds.Height + bounds.Y), bounds.Width, bounds.Height);

I should mention that X and Y range from 0.0000 - 1.0000.

Now the problem occurs when I select most if not all the box and the equations looks something like:

return new RectangleF(bounds.X, 1.0f - (0.9886 + 0.0114), bounds.Width, bounds.Height);

Now obviously 1.0f minus this sum should make 0, but instead I'm getting -0.0000000158324838.

Now I've seen that a float has a 7point accuracy but as you can see I'm not using that, so what is the problem?
Any help would be much appreciated.
0

Share this post


Link to post
Share on other sites
Your operands get converted to double since 0.9886 and 0.0114 are doubles (no "f" at the end). Double also has a limited precision, you just get roughly twice as many digits. Sure, 1.0f - 1.0f = 1.0 - 1.0 = 0, but the intermediate addition you do with bounds.Height and bounds.Y will almost never sum up to perfectly one because of limited precision.

Normally such values should be indistinguishable from zero in almost every situation, is it actually a problem in your case? If everything else fails, just saturate the resulting value to clamp it in the [0, 1] range, but really, you shouldn't need to in general.
0

Share this post


Link to post
Share on other sites
Thanks for the reply,
Sorry I should have been more specific, the numbers I provided are examples of my bound.Height and bounds.Y values, bounds being another RectangleF so they will be floats and not doubles. Well It's not too important its just causing it to crash when I set some other values that rely on it clamped between 0 and 1 but as you say I can just clamp that myself. I was wondering if using decimal here would be preferable? Or at least casting them to decimal and then the resulting answer back to a float?
0

Share this post


Link to post
Share on other sites
[quote name='EvilWeebl' timestamp='1347626868' post='4980035']
I was wondering if using decimal here would be preferable?
[/quote]

AFAIK, Decimal is used mainly for financial calculations - i.e. I believe it works best for base 10 numbers in a relatively limited range, and even it will not be infinitely precise of course.

Here is an article with some basic tips on using floating point: [url="http://www.codeproject.com/Articles/29637/Five-Tips-for-Floating-Point-Programming"]http://www.codeproje...int-Programming[/url] Edited by laztrezort
1

Share this post


Link to post
Share on other sites
[quote name='EvilWeebl' timestamp='1347624936' post='4980027']
Now I've seen that a float has a 7point accuracy but as you can see I'm not using that, so what is the problem?
[/quote]
The result is in fact zero... to seven digits of accuracy.

The problem is, just as you appear to be aware of, that you don't have infinite precision. A rule of thumb is roughly seven significant numbers for a float, so you have that quite correct. Any value you store is only valid to that many significant digits. The values 0.9886 and 0.0114, while having 4 significant numbers, actually require infinite precision to store the values exactly. Actual values, when stored in a truncated binary form, may not be those [i]exact[/i] number, but as close as a truncated binary representation can provide. the number are, however, accurate to seven digits, as you mentioned.

That means that 0.9886 is not exactly 0.9886, but something very close, and accurate to seven digits. That is, 0.988600[i]xxxx[/i], where [i]xxxx [/i]is some residue, but small enough such that, when rounded to seven digits, the value is 0.9886000.

This residue is present in pretty much any number and will accumulate, because it is a part of the actual value being added.

To summarize; seven digits of accuracy does not mean that any value with seven or less significant digits can be perfectly represented, it means that values are not exact but accurate to seven digits.
1

Share this post


Link to post
Share on other sites
The problem is, that a decimal fraction (base 10) can't always be displayed as a finit sequence of binary numbers (base 2). Therefore the problem is, that you write a decimal fraction which seems ok, but the compiler have to use a binary represention which often only an approximation of the decimal number. As example 0.1 is already an infint sequence when using a binary representation.

When you do some calculations with these approximation and display it (converted back to decimal/base 10) you will see the approximation error.
1

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1347627942' post='4980041']
To summarize; seven digits of accuracy does not mean that any value with seven or less significant digits can be perfectly represented, it means that values are not exact but accurate to seven digits.
[/quote]

Ahh I see where I went wrong now, thanks for summing that up for me.

Also laztrezort that link was a good read so thanks for that.
0

Share this post


Link to post
Share on other sites
To add some details...

In IEEE single precision floating point the closest you can get to the two numbers:

9.886000156402587890625E-1
1.1400000192224979400634765625E-2

When you add them you get:

1.0000000158324837684621

Here's a converter website that is quite useful: [url="http://www.binaryconvert.com"]http://www.binaryconvert.com[/url]

I can go into further details for just why this limitation arises if you want.
0

Share this post


Link to post
Share on other sites
[quote name='___' timestamp='1348518982' post='4983350']
so maybye use long double?[img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]
[font=courier new,courier,monospace]long double[/font], in C and C++, may provide more precision than [font=courier new,courier,monospace]double[/font], but it may also be the same (so you should check your implementation) (it will not have less precision than [font=courier new,courier,monospace]double[/font], though). However, that doesn't get rid of the fact that you have to deal with floating point error, so the real answer is "understand floating point error and how to work with it," because [font=courier new,courier,monospace]float[/font], [font=courier new,courier,monospace]double[/font], and [font=courier new,courier,monospace]long double[/font] all suffer from floating point error. Just using a different data type doesn't [i]really[/i] solve the problem. I'm surprised nobody's linked to this: [url="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html"]What Every Computer Scientist Should Know About Floating-Point Arithmetic[/url]. It's rather long, but there's a lot of good stuff to learn in there, and you don't have to read it all to learn something useful.
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