**0**

###
#1
Members - Reputation: **126**

Posted 14 September 2012 - 06:15 AM

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.

###
#2
Crossbones+ - Reputation: **12883**

Posted 14 September 2012 - 06:30 AM

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.

*“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”*

###
#3
Members - Reputation: **126**

Posted 14 September 2012 - 06:47 AM

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?

###
#4
Members - Reputation: **1058**

Posted 14 September 2012 - 07:03 AM

I was wondering if using decimal here would be preferable?

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: http://www.codeproje...int-Programming

**Edited by laztrezort, 14 September 2012 - 07:04 AM.**

###
#5
Moderators - Reputation: **9916**

Posted 14 September 2012 - 07:05 AM

The result is in fact zero... to seven digits of accuracy.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?

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

*exact*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

*xxxx*, where

*xxxx*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.

###
#6
Crossbones+ - Reputation: **13441**

Posted 14 September 2012 - 07:15 AM

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

###
#7
Members - Reputation: **126**

Posted 14 September 2012 - 07:18 AM

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.

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.

###
#8
Members - Reputation: **1772**

Posted 14 September 2012 - 02:58 PM

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: http://www.binaryconvert.com

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

###
#9
Crossbones+ - Reputation: **1200**

Posted 24 September 2012 - 02:36 PM

https://sites.google.com/site/customprog/

###
#10
Crossbones+ - Reputation: **7020**

Posted 24 September 2012 - 03:43 PM

long double, in C and C++, may provide more precision than double, but it may also be the same (so you should check your implementation) (it will not have less precision than double, 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 float, double, and long double all suffer from floating point error. Just using a different data type doesn'tso maybye use long double?

*really*solve the problem. I'm surprised nobody's linked to this: What Every Computer Scientist Should Know About Floating-Point Arithmetic. 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.