Rounding/truncating float to int

This is a discussion on Rounding/truncating float to int within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm new here and to C++ in general. I'm working on my first program, which is basically just a ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Rounding/truncating float to int

    Hello, I'm new here and to C++ in general. I'm working on my first program, which is basically just a simple formula we use for determining feedrates on our CNC machines.. Here is the code, I'll try to use the HTML format here.. (first try for this particular code).

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main(int nnumberofargs, char* pszargs[])
    {
        int flutes, rpm;
        
        float material, chipload, bitsize, product, converted;
        
        cout << "Enter size of bit: ";
        cin >> bitsize;
        cin.ignore();
        cout << "Enter size of material: ";
        cin >> material;
        cin.ignore();
        product = material / bitsize;
        converted = static_cast<int>(product);
        cout << "Enter nominal chipload: ";
        cin >> chipload;
        cin.ignore();
        if ( converted > 2 ){
        chipload = chipload * 0.5;
        }
        if ( converted >  3 ) {
        chipload = chipload * 0.33;
        }
        if ( converted > 4 ) {
             chipload = chipload * 0.25;
             }
        if ( converted > 5 ) {
             chipload = chipload * 0.125;
             }
        cout << "Enter number of flutes: ";
        cin >> flutes;
        cin.ignore();
        cout << "Enter desired RPM: ";
        cin >> rpm;
        cin.ignore();
        
        
        
        cout << "Your maximum feedrate is: " << flutes * rpm * chipload << endl;
        
        
        system("pause");
        return 0;
    }
    I know there is alot of garbage in there, since I used a template from some other program and have tried several things that I got from online. Basically I need to either turn the PRODUCT symbol into an interger, or get tricky with the IF statements. The final product is this formula:

    If your nominal chipload is .02, and you are using a 1/2" bit to go through 1.125" material, then the chipload must be reduced by 50&#37; (actually, it is bit diameter/board thickness = percentage to reduce chipload)

    thanks!

    Zzaacchh

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > if ( converted > 5 )
    If this is true, then it's also >2 >3 and >4
    That's a lot of multiplying...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Like Salem said. If the converted value is greater than 5, it's going through each one of those if statements. I'm thinking it should only go through one? Try something like...

    Code:
    if(converted > 5){ /*do something*/ }
    else if(converted > 4){ /*do something*/ }
    else if(converted > 3){ /*do something*/ }
    else if(converted > 2){ /*do something*/ }
    else { /*do something in case it's smaller than 2 }
    Also, on the following section, is converted suppose to be an integer? Why declare it as a float/double when you are casting an int to it (which is really a float).
    Code:
    product = material / bitsize;
    converted = static_cast<int>(product);

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Well.....

    Quote Originally Posted by scwizzo View Post
    Like Salem said. If the converted value is greater than 5, it's going through each one of those if statements. I'm thinking it should only go through one? Try something like...

    Code:
    if(converted > 5){ /*do something*/ }
    else if(converted > 4){ /*do something*/ }
    else if(converted > 3){ /*do something*/ }
    else if(converted > 2){ /*do something*/ }
    else { /*do something in case it's smaller than 2 }
    Also, on the following section, is converted suppose to be an integer? Why declare it as a float/double when you are casting an int to it (which is really a float).
    Code:
    product = material / bitsize;
    converted = static_cast<int>(product);
    Well, like I say, there was some garbage in there from me just trying a various amount of different things. I thought maybe I would just convert the CONVERTED symbol to an interger.. (which didn't work since it's still a float number, albeit rounded off) Anyways, I came up with this, and it seems to work how I want.. though not *quite* as accurate as it could be. I cleaned it up a little too.

    Thanks for replies, if there are any suggestions on how to make it better/ more accurate, I'd love them. At least here you can kindof see what I want to do.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int flutes, rpm;
        float material, chipload, bitsize, product;
        cout << "Enter size of bit: ";
        cin >> bitsize;
        cin.ignore();
        cout << "Enter size of material: ";
        cin >> material;
        cin.ignore();
        cout << "Enter nominal chipload: ";
        cin >> chipload;
        cin.ignore();
        
        product = material / bitsize;
        
        if (( product >  1.5) & (product < 3 )) {
        chipload = chipload * 0.5;
        }
        else if (( product > 2.9) & (product < 4)) {
             chipload = chipload * 0.33;
             }
        else if (( product > 3.9) & (product < 5)) {
             chipload = chipload * 0.125;
             }
        cout << "Enter number of flutes: ";
        cin >> flutes;
        cin.ignore();
        cout << "Enter desired RPM: ";
        cin >> rpm;
        cin.ignore();
        cout << "Your maximum feedrate is: " << flutes * rpm * chipload << endl;
        system("pause");
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Ahh..

    Quote Originally Posted by Salem View Post
    > if ( converted > 5 )
    If this is true, then it's also >2 >3 and >4
    That's a lot of multiplying...
    That is the problem I finally realized, which is why I wanted to convert the product to an interger so I could just say if ( converted == 5) instead of if ( converted > 5) because that wouldn't work.

    thanks,

    Zzaacchh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21