Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ floats

Options
  • 06-11-2003 7:06pm
    #1
    Closed Accounts Posts: 59 ✭✭


    Hi everybody,

    I have declared a float sampleSize;
    int ii is incremeneting in a for loop so is gonna be numbers like 1, 2, 3, 4, 5, ..etc

    float sampleSize = ((ii/1000)*100);

    any idea why sampleSize is alwys zero?
    I should be getting like .4, .5, etc ...shouldn't I??
    please help
    luv Fi**


Comments

  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    integer division of ii maybe?


  • Closed Accounts Posts: 59 ✭✭Fi_C**


    thanks , I love you,

    float sampleSize = ((ii/1000.0)*100);
    that works
    :)
    Fi


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    In (ii/1000) ii and 1000 are both ints, so integer division is performed and you get 0. This is then multiplied by 100 (giving you 0) and then finally this is cast to float to give you 0.0.

    float sampleSize = ((ii/1000.0)*100); would work (since 1000.0 is a float it with cause ii to be cast to float before the calculation).

    For that matter float sampleSize = ii/10.0; would work, and be clear.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Darn my verbose reply and how long it took! :)


  • Closed Accounts Posts: 59 ✭✭Fi_C**


    well thank you anyway Talliesin
    Fi**


  • Advertisement
  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    float sampleSize = ((float)ii/1000.0f)*100.0f;
    

    You don't want to be using doubles! :)


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    D'oh!

    I'd tested with float sampleSize = ((ii/1000.0f)*100), but that wasn't what I posted :(


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    Originally posted by DeadBankClerk
    float sampleSize = ((float)ii/1000.0f)*100.0f;
    

    You don't want to be using doubles! :)

    I am also silly.
    float samplesize = (float)ii / 10.0f;
    


Advertisement