Floats

This is a discussion on Floats within the C++ Programming forums, part of the General Programming Boards category; Hey! Thanks for reading ;o) I was writing my own header for OpenGl rotation, but had alot of hiccups so ...

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407

    Floats

    Hey! Thanks for reading ;o)

    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;
    float Radius = DistanceFormula(VectorPos, VectorOrigin);
    VectorPos.X = cos(DeltaZ) * Radius;
    VectorPos.Y = sin(DeltaZ) * Radius;
    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.

    Thanks for your time ;o).

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Have you considered using a fixed-point representation?

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    >>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. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    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. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    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).
    Last edited by Lesshardtofind; 10-11-2010 at 12:45 AM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,530
    Quote Originally Posted by Lesshardtofind View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  8. #8
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Faster squaring for a vector of floats
    By onako in forum C++ Programming
    Replies: 6
    Last Post: 09-04-2010, 11:00 AM
  2. A doubt about precision in Floats
    By avi2886 in forum C Programming
    Replies: 3
    Last Post: 11-27-2009, 03:05 PM
  3. variables larger than floats
    By a.mlw.walker in forum C Programming
    Replies: 13
    Last Post: 04-23-2009, 03:28 AM
  4. % and floats
    By confuted in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2003, 10:41 PM
  5. comparing two arrays of floats
    By COBOL2C++ in forum C++ Programming
    Replies: 7
    Last Post: 07-16-2003, 04:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21