Results 1 to 10 of 10

Thread: C++ automatically rounding off values

  1. #1

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211

    C++ automatically rounding off values

    how to resolve the case of c++ rounding off values, for example:

    cos(6.2832) i get 1 instead of .99999

    and

    0.1 being only 0... what should i do
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

  2. #2

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211
    forgot to say thannk you!
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's only rounding on output, the variable contains a better value. Use the setprecision modifier from <iomanip> to select how many places after the comma are displayed.

    On a side note, the Windows calculator claims that the result of cos(6.2832) is 0.99399308850723265435071042275478
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211
    thanks... but sometimes it still prints 0 if it is 0.000001. what could be the problem? Thank you for your reply.
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Floating values only store a specific precision. I don't know the details, as they differ from compiler to compiler.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I think that varies from OS too OS too, at least we had that problem in Java at school, on some versions of NT/2000 it wrote 1, and on som e 1.000000000001

  7. #7

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211
    shocks... there are still inconsitencies... anyways.... thanks! at least im able to use <iomanip> even though it doesnt work as always. thanks
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

  8. #8
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    What about in DirectX
    D3DVECTOR struct has a float x,y,z.

    Seeing a vector represents character posistion I need to store movement variables as floats.

    To eliminate warning of double to float, I have to make everything a float which is part of a units movement system.

    Speed, TimeDelta, Velocity, Weight.....the list goes on.


    So you are saying that float precision is different from machine to machine...

    I need integral values, but I also need a definite precision on any CPU, how do I go about that?
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Use a decimal number libray like Gnu MPL.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Halsafar
    What about in DirectX
    D3DVECTOR struct has a float x,y,z.

    Seeing a vector represents character posistion I need to store movement variables as floats.

    To eliminate warning of double to float, I have to make everything a float which is part of a units movement system.

    Speed, TimeDelta, Velocity, Weight.....the list goes on.


    So you are saying that float precision is different from machine to machine...

    I need integral values, but I also need a definite precision on any CPU, how do I go about that?
    Floats in DX is pretty much the same. It is a DX standard. Even tho some HW vendors like to try to change that. So don't worry to much about vectors and matrix math in DX. Your GPU is handeling most of that...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width