Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ floats

  • 06-11-2003 07: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