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.

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.

Anyway the entire project is here

what do you mean it gets fired when x is pressed?
It shouldn't react on any key press unless it's programmed that way.

have you tried debugging?
Try checking out if all of the conversions of data tipes work properly. Do a step by step execution to see where the values change. Then point us that line and we'll see with what can we help. Otherwise i don't think anyone here would want to read all those line of code or do the debugging process. I may try later though... but don't count on that. :) Time is valuable for everyone.

Edited 7 Years Ago by Alex_: n/a

the problem with the line of code you are using is that it is calling multiple functions. when the program runs it will take the return of the function and instead of keeping it in the registry it will put it into the ram which will truncate the number and then it will do the same with the other returns then read them all back and then give you your answer. one way you could solve this would be to make sure to do all the calculations first and then store them so you will have an equal truncation every time. hope this make sense.

the problem with the line of code you are using is that it is calling multiple functions. when the program runs it will take the return of the function and instead of keeping it in the registry it will put it into the ram which will truncate the number and then it will do the same with the other returns then read them all back and then give you your answer. one way you could solve this would be to make sure to do all the calculations first and then store them so you will have an equal truncation every time. hope this make sense.

Do you mean that if i do

float give(){
return 9.44;
}
float take(float f){
return f;
}
void main(){
cout << take(give());
}

The result will be

9

?
From my experience that doesn't happen. But maybe i didn't understood you well. Can you explain to me again why that's happening? And why placing values in ram leads to they're malformation?

This article has been dead for over six months. Start a new discussion instead.