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.
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
form problem
Hi all, I think this is going to be easy to resolve but for I have been looking at it to long and I
Add a sign-up feature to a flat file login script
I'm working on a flat file login script and I would like to add a sign-up feature to it with a email
updating a single value to multiple non consecutive rows
I need to update the same value to multiple non consecutive rows. I need to add an experation date t
close site for maintenance
i get a tutorial, saying the following code can put our site offline, and only the developer can vie
PHP code for a proxy
I was wondering which function I could use to create an online proxy tool. I was thinking cURL would
media recovery
Hi,
i startup the database i got the error
SQL> startup
ORACLE instance started.
asp authentication problem
Hello all,
I want to use the asp authentication (from asp.net configuration) in my web site. there
Dynamically allocating the number of rows in a table based on a variable value
Hi,
I have situation here which i cannot resolve, I have a variable temp which stores the num
Map.php
I am a complete beginner to PHP and am looking for some help with a program I am messing around with
Google Wave
So... has anyone used it yet? Is it any good?
http://wave.google.com/help/wave/about.html#vid