Floating Point Precision Loss
Posted on
16th Feb 2014 07:03 pm by
admin
I'm writing a program to draw a three-dimensional cube (with a corner cut off) without using any 3D graphics APIs. The only API call I make is win32's SetPixel()
Everything is going great. I've drawn lines, faces, and even the entire cube. Then rotation came along.
Did you know?Explore Trending and Topic pages for more stories like this.
For some reason, when I rotate my cube, it begins to shrink. I became confused, then I realised something. Floating point numbers are truncated.
Here's some code that is fired everytime the user presses 'X' on the keyboard.
Code:
p->setY( p->getY() * cos(5.0/180.0*PI) - p->getZ() * sin(5.0/180.0*PI));
p->setZ( p->getY() * sin(5.0/180.0*PI) + p->getZ() * cos(5.0/180.0*PI));
p is a Point object that I created. Here the Y value and the Z value of the Point are being set. The problem is everytime X is pressed, and this code is fired, a small amount of precision is lost. As a result, the cube shrinks.
I'm really completely stumped. I remember when studying assembler that floating point numbers can be truncated or rounded. I think rounding would solve my problem. Since 50% of the time the value would round up, and 50% of the time the value would round down.
Whereas with truncation values are always lost, and never gained.
No comments posted yet
Your Answer:
Login to answer
213
53
Other forums
isset undefined variable
Hi all,
Hope someone can point out the obvious. I've a log in script, if you dont enter a use
How to write on database when a link is clicked?
Hi,
I have a list of products in a gridview. When the link is clicked currently it takes to an ext
Parse error: syntax error, unexpected T_ELSEIF in /home/......html/item.php on l
I do not know what is wrong with this. Hope some one can help. I do nto want to post the entire site
Anti Spam Code
Ok where do i start? Probably by telling you I have very little working knowledge of PHP and that I
the problem with str_replace
$str="hahahahahahahahahahahahahaha";
$nn=1;
$str=str_replace('ha','MyGod',$str,$nn);
How to ... (FAQs)
... get e-mail notifications
As several people asked how to get e-mail notifications when new posti
question about header() security
is is safe to just use the header() function to redirect someone if they are, say, not logged in? or
How to ask a regex question?
Hi, I thought I'd share some of my insight after seeing too many bad questions that either get ignor
Auto-populating dropdowns and multiple forms.
Here's what I have so far:
First drop down = select a state (works)
This populates the sec
Extending Exception to contain method name
I was wondering, if there's a way to extend Exception in such a way, that it would contain name of a