Re: strange behavior, out of range?
Re: strange behavior, out of range?
- Subject: Re: strange behavior, out of range?
- From: Fritz Anderson <email@hidden>
- Date: Mon, 10 May 2010 18:40:39 -0500
On 10 May 2010, at 4:12 PM, Ariel Feinerman wrote:
> There is float vars, I check them values with gdb:
>
> p_object->aabb[6].z = 7.00061798;
>
> p_object->aabb[0].z = - 7.00061846;
>
> p_object->bsphere_center.z = (p_object->aabb[6].z + p_object->aabb[0].z)/2.0;
>
>
> Xcode 3.2.1, llvm-gcc 4.2
> What is this? I cannot understand
>
>
> 7.00061798 - 7.00061846 = -2.38418579e-07
I don't know what you're asking. You seem to be asking what Xcode and llvm-gcc are (they are the editor/toolset and the compiler you are using), but I don't think you are asking that.
Are you asking about the precision of the arithmetic? I am guessing you are using 32-bit floats. They can't distinguish more than 7 significant decimal digits, and the difference between your two numbers is down in the 7th significant digit. The best you can hope for is that the result is on the right order of magnitude.
Some thoughts:
* You can relieve the problem by using double variables instead of float. The problem will still be there, but you won't see it for many more digits. However, if the values of your doubles ever passed through floats, all you'll be doing is high-precision arithmetic on the noise in your data.
* Computer people sometimes call floats, doubles, and the like "real" numbers, but they have almost nothing to do with the mathematical set of real numbers. Instead, they are limited-range integer mantissas with some bookkeeping for placing a decimal point. The mantissas are binary numbers with a constant number of digits. Real numbers have potentially an infinite number of digits. A computer language's floating-point storage cannot contain most real numbers. There will be rounding. If two numbers differ in only the last bit of the mantissa, subtracting them will give you a difference with only one significant bit.
* It appears that you are using 32-bit floats because you are working with UI objects, am I right? If so, get some perspective: No display in the world will render 10^-7 of a pixel, or render one pixel in an image 10^7 pixels wide.
* Even if you aren't working with CGFloats, you're still worrying about being less than 10^-7 off of the mathematical center of something that's 14 units wide. For engineering and scientific purposes, unless you're deriving the cosmological constant, that's negligible. Switching to double precision will probably handle the cosmological constant, too. If you're computing for mathematical purposes, floating-point arithmetic is the wrong tool.
This is a general computing question that doesn't have much specifically to do with the Xcode tools. Googling about floating-point and rounding errors should give you all the education you need. "What Every Computer Scientist should Know about Floating-Point Arithmetic" is the starting point everyone gives for these questions. <http://docs.sun.com/source/806-3568/ncg_goldberg.html>
— F
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden