7 Replies Latest reply: Apr 2, 2007 11:22 AM by 796447

# Broken Booleans

I am very confused to say the least with my current assignment in class.

I am making a Ray Tracer that can see a sphere and only a sphere, so far. So here is my code for detecting an intersection with a ray and my sphere
``````double b = 2 * ((ray.rayD.x * (ray.rayO.x - center.x)) + (ray.rayD.y * (ray.rayO.y - center.y)) + (ray.rayD.z *(ray.rayO.z - center.z)));
double c = Math.pow(ray.rayO.x - center.x, 2) + Math.pow(ray.rayO.y - center.y, 2) + Math.pow(ray.rayO.z - center.z, 2) + Math.pow(radius, 2);
double ct = (b*b)-4*c;
if(ct < 0)
return ScenceObject.INFINITY;
else
return Math.sqrt(ct/2);``````
The problem is that if(ct < 0) always returns false. And the program returns NaN or zero all the time.

Any help will be appreciated. If it makes a difference I run Ubuntu 6.11 with netbeans. I have to use the debian version of Java 6, apt-get java 6. It is the only way I can get java to work on this machine.
• ###### 1. Re: Broken Booleans
Check Double.isNaN(ct)
• ###### 2. Re: Broken Booleans
Check Double.isNaN(ct)
I have already fixed that bug. When I run it through the debugger in netbeans the value ct comes back as -601.
• ###### 3. Re: Broken Booleans
ScenceObject.INFINITY
??? The square root of a negative number is infinity? Why not Double.NaN?
• ###### 4. Re: Broken Booleans
Check Double.isNaN(ct)
I have already fixed that bug. When I run it through
the debugger in netbeans the value ct comes back as
-601.
Then obviously your math logic is wrong if that is not the value you expected, not a "broken booleans" problem.
• ###### 5. Re: Broken Booleans
ScenceObject.INFINITY
??? The square root of a negative number is infinity?
Why not Double.NaN?
That is what my professor asked for that is why.

Check Double.isNaN(ct)
I have already fixed that bug. When I run it
through
the debugger in netbeans the value ct comes back
as
-601.
Then obviously your math logic is wrong if that is
not the value you expected, not a "broken booleans"
problem.
I am just curious as to why -601 is not less than zero that is all.
• ###### 6. Re: Broken Booleans
I'm curious, too. Can you create a small (<1 page) program that demonstrates your problem?
• ###### 7. Re: Broken Booleans
I am just curious as to why -601 is not less than
zero that is all.
Since -601 is in fact less than zero, you must have misdiagnosed the issue. The "if (ct < 0)" statement in this case (if ct is -601) would in fact evaluate to true.