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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C++ floats

  • 06-11-2003 6: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,563 ✭✭✭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, Registered Users 2 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, Registered Users 2 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