POV-Ray : Newsgroups : povray.off-topic : Bounding circle intersection Server Time
5 Sep 2024 03:24:26 EDT (-0400)
  Bounding circle intersection (Message 41 to 43 of 43)  
<<< Previous 10 Messages Goto Initial 10 Messages
From: Invisible
Subject: Re: Bounding circle intersection
Date: 14 Dec 2009 04:17:00
Message: <4b26028c$1@news.povray.org>
Warp wrote:

>   I was curious to see how much of a difference sqrt() really makes compared
> to avoiding it, so I made this C++ file:

>   I compiled with "-O3 -march=native" and got this result:
> 
> circlesCollide1: 200000000 iterations took 16.23 seconds.
> circlesCollide2: 200000000 iterations took 5.42 seconds.

I would imagine this sort of thing varies by processor, but yeah... I'm 
often curious to know how "expensive" various math operations are in 
relation to each other. (Of course, real programs are also affected by 
cache issues and which combinations of math ops you perform together and 
branch prediction issues and so forth... But typically you can't do much 
about that.)


Post a reply to this message

From: John VanSickle
Subject: Re: Bounding circle intersection
Date: 22 Dec 2009 14:00:25
Message: <4b311749$1@news.povray.org>
Invisible wrote:

> If you're that hell-bent on avoiding the square root, it can be 
> approximated as follows:
> 
>   Suppose that the most significant non-zero digit of x is in the 2^n 
> place. Let y = x / 2^(n/2). Now Sqrt(x) is approximately x / y.
> 
> Usually this estimated value is greater than the true square root (but 
> always by less than 45%). However, exact powers of 2 seem to come out 
> slightly below the true value. (By about 10^-16.) I don't know if that's 
> a glitch in my test program or what... It shouldn't be insurmountable 
> though.

One would have to be very hell-bent indeed to employ, in lieu of sqrt(), 
functions that are probably just as computationally expensive, like ln() 
and exp().  Unless you have a quick way of getting n and 2^(n/2) without 
ln() and exp(), you'll need them.

Now if you know the internal representation of your floats (or doubles), 
you can slap together a union like

union MajorKludge {
	double dvalue;
	char carray[sizeof double];
};

and get both n and 2^(n/2) using some very simple bitwise logical 
operations (as well as code whose portability will be highly suspect).

Regards,
John


Post a reply to this message

From: Orchid XP v8
Subject: Re: Bounding circle intersection
Date: 22 Dec 2009 14:16:51
Message: <4b311b23$1@news.povray.org>
John VanSickle wrote:
> Invisible wrote:
> 
>> If you're that hell-bent on avoiding the square root, it can be 
>> approximated as follows:
>>
>>   Suppose that the most significant non-zero digit of x is in the 2^n 
>> place. Let y = x / 2^(n/2). Now Sqrt(x) is approximately x / y.
>>
>> Usually this estimated value is greater than the true square root (but 
>> always by less than 45%). However, exact powers of 2 seem to come out 
>> slightly below the true value. (By about 10^-16.) I don't know if 
>> that's a glitch in my test program or what... It shouldn't be 
>> insurmountable though.
> 
> One would have to be very hell-bent indeed to employ, in lieu of sqrt(), 
> functions that are probably just as computationally expensive, like ln() 
> and exp().  Unless you have a quick way of getting n and 2^(n/2) without 
> ln() and exp(), you'll need them.
> 
> Now if you know the internal representation of your floats (or doubles), 

Indeed, the *only* reason you would ever use a construction like this is 
because it's very fast and efficient to get at these numbers using the 
binary representation of a float.

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

<<< Previous 10 Messages Goto Initial 10 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.