1. ## Floats

I was writing my own header for OpenGl rotation, but had alot of hiccups so I decided to take all the code to console and just cout my calculations to see where the problems were occuring. I finally started to notice some of the weird things that floats do, and I don't like it. Basicly I take an objects current angle from a Standard 0 that I decided (using the unit circle it is located at 1, 0, 0)

From this angle I add the change in angle to that then use the following formulas;

Code:
```float DeltaZ = CurrentAngle + NewAngle;
The degrees are converted to radians before the cos and sin function calls. (I found that the math wouldn't work if I gave it degrees). When cos and sin are called at angles that should put their value to 0, (ex. 0 degrees should cause Y to equal 0 on coordinates) the float goes into what appears to be scientific notation mode, and for some reason comparisons with the notation don't work consistently.

Using these functions I can't get X or Y to ever equal 0 but instead end up as
something like 3.42456328e-006. How can I stop the float from going into a scientific notation representation?

Second if I tell it to print a table such as
Degrees X Coordinate Y Coordinate Z Coordinate
0 1 0 0

then change the incriment of degrees change to .001 for a little while degrees goes up as normal but the longer this loop is the more likely I'll end up getting
.001999 instead of .002 and once it gets to that point then it keeps counting like that.

Knowing how important significant digits are in mathematic and scientific calculations I'm wondering if there is a way to control these floats to ONLY hold a number of digits equal to the change you are making. So if I'm increasing by
.001 then I never want to hold more than 3 digits after the decimal point. Any suggestions would be greatly appreciated.

2. Have you considered using a fixed-point representation?

3. >>something like 3.42456328e-006. How can I stop the float from going into a scientific notation representation?
I know I'm probably being curt or rude but, you can't, except for display purposes only. Scientific notation does not change something's value. And the internal representation depends on it.

>>then change the incriment of degrees change to .001 for a little while degrees goes
>>up as normal but the longer this loop is the more likely I'll end up getting
>>.001999 instead of .002 and once it gets to that point then it keeps counting like that.

There are a lot of real numbers that have approximations in floating point due to the space restrictions. If you just keep incrementing you are bound to accrue some rounding errors and end up with very different numbers. To add to that, some numbers in decimal do not have an exact representation in binary anyway like how say 0.3333.... isn't exact in decimal. There will always be numbers like that. Because of this, you don't want to use them to count if you can help it.

I can understand your frustration, but at the precision you're seeking you should not have that much of a problem. I think understanding a float's representation will help you fix the problems you have. If only because that is when people take the time to teach about its problems.

This document breaks down what a float actually is:
IEEE Standard 754 Floating-Point
and there is a section on the site about floating point that should be pretty approachable:
Cprogramming.com - Tutorials - Advanced C, C++, Graphics, Computer Science Tutorials
and this document is a lot more math heavy, but it explains basically everything about them:
What Every Computer Scientist Should Know About Floating-Point Arithmetic

But BMJ is right about fixed point being an answer as well. C++ doesn't have it built in I think.

4. Hmm seems from the information I've read that using fixed-point representation on any machine newer than a Pentium II is going to actually slow down the code. Thats concerning. I was hoping there was a built in C++ function for tracking significant digits that I wasn't aware of. Thnx for the info, not sure if I want to get side tracked on trying to write a library for it.

5. Google for "what every computer scientist should know about floating point".

Cutting a long story short:
Fractions are represented in binary. Just as 10 is represented as:
1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 0 * 2^0
I.e. 8 + 0 + 2 + 0
A value of 0.625 is represented as:
1 * 2^-1 + 0 * 2^-2 + 1 * 2^-3
I.e. 0.5 + 0 + 0.125
Cause in a computer everything is most efficient using powers of two.

Now try representing 0.001 in binary, like I did with 0.625 above and you'll see where the problem is.
Or if you'd like a more interesting one to work out, try representing one-third, i.e. 0.333333... in binary!

Edit: Damn, too slow.

6. lol.... and we fall to where the real problem lies... my bianary understanding is non-existent. I've taught myself everything I know about C++ through the web and books (which explains the lack of understanding if have for certain areas of computer science) I do appreciate the information. Sucks lately seems none of the questions I ask have simple answers anymore! I'll be wading through the links you posted and attempting to understand binary to the wee hours of the morning I guess.

>>something like 3.42456328e-006. How can I stop the float from going into a scientific notation representation?
I know I'm probably being curt or rude but, you can't, except for display purposes only. Scientific notation does not change something's value. And the internal representation depends on it.
I know scientific notation doesn't change value, but my comparisons aren't working.
Well is it normal that I tried something like
Code:
```if(Vector.x < .0001 && Vector.x > -.0001)
Vector.x = 0;```
and the result still comes out as 3.42356e-006. Is it not possible to use comparisons at this range or am I doing something wrong?
Thanks again ;o).

7. Originally Posted by Lesshardtofind
Well is it normal that I tried something like
Code:
```if(Vector.x < .0001 && Vector.x > -.0001)
Vector.x = 0;```
and the result still comes out as 3.42356e-006.
Thanks again ;o).
I'd guess you're doing something wrong that you haven't shown.

There is the concern that negative powers of 10 (0.1, 0.01, etc) can be represented exactly in binary, however that won't cause what you're describing.

My guess would be that you either have a typo in your code so it isn't quite what you've posted here, or there is some other logic error that is preventing the code you're showing being executed.

Without seeing a small but representative example of actual code (by which I mean something that is a verbatim copy of code fed to the compiler and that then exhibits your problem) we probably can't help further. One of the joys of forums, when you tell us where you think the problem is, is that there is a good chance you leave out something relevant.

8. Yea woke up this morning and the typo became obvious. Thnx for the comment Grumpy. And thnx to everyone else who gave me info on this.