Results 1 to 3 of 3

Thread: seems simple but not.

  1. #1

    Thread Starter
    Lively Member Optic's Avatar
    Join Date
    Mar 2001
    Location
    I'm everywhere and nowhere, I'm inside your computer and inside your mind. You can't escape me for I am vision.
    Posts
    117

    seems simple but not.

    This looks like it would be a very simple task but is a lot harder than I thought. I have a double of a value say 2.999999. I want to cast this value into an integer and get the value 3. You would think that all you would have to do is to have an interer and make it equal to the double. This on the other hand makes the integer value 2. It automatically rounds down. I have looked in a c ouple of books and none of them mention this. Does anyone know a way to cast a double to the properly rounded int.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    When you cast to an integer, it doesn't round down, it truncates. Same effect, different method -- it just removes the fractional portion. To round to the nearest number, add 0.5 and cast.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Faust
    Guest

    Just a suggestion

    What you are getting is exactly what you are supposed to get as parksie mentioned. What I would do is have a look at the Floor and Ceiling functions although I am not sure whether the math.h has those included.

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