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

Turbo C

Options
  • 24-04-2009 6:36pm
    #1
    Registered Users Posts: 382 ✭✭


    Hello all,
    does anyone know how to round a number with decimal places, ie 9.8790, to its nearest integer?
    Tagged:


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Add 0.5 to it and cast to integer.

    float x = 9.8790
    int answer = (int)(x+0.5);

    I think that should work.


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Don't forget the negative values.


  • Registered Users Posts: 382 ✭✭DiarmaidGNR


    Webmonkey wrote: »
    Add 0.5 to it and cast to integer.

    float x = 9.8790
    int answer = (int)(x+0.5);

    I think that should work.



    Thanks!!!!!!
    X = (int)(x+0.5); rounds X to nearest integer!


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    You forgot the negatives even after I warned you!
    if (num >= 0)
        x = (int)(num + 0.5);
    else
        x = (int)(num - 0.5);
    


  • Registered Users Posts: 382 ✭✭DiarmaidGNR


    Thanks Ronivek!!!


  • Advertisement
  • Registered Users Posts: 382 ✭✭DiarmaidGNR


    I dont suppose you know how to get rid of decimal places altogether?!


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Happy to help!
    I dont suppose you know how to get rid of decimal places altogether?!

    What do ya mean?


  • Registered Users Posts: 5,982 ✭✭✭Caliden


    I dont suppose you know how to get rid of decimal places altogether?!

    Thats what casting to an int would do since integers are whole numbers


  • Registered Users Posts: 382 ✭✭DiarmaidGNR


    when i round from lets say 8.4444444 i get 8.000000, I think these decimal places might confuse the program, also it doesnt look neat on the display.
    Any ideas?


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    when i round from lets say 8.4444444 i get 8.000000, I think these decimal places might confuse the program, also it doesnt look neat on the display.
    Any ideas?

    You're casting from a float to an int and back to a float again by the looks of things.

    Make sure you're not using the same variable for the assignment and addition; you'll need two variables as follows;
    float num;
    int x;
    
    x = (int)(num + 0.5);
    


  • Advertisement
  • Registered Users Posts: 382 ✭✭DiarmaidGNR


    I tried doint the int x; or int A1/A2; etc, Here is the working code before second mod-

    #include <conio.h>
    #include <dos.h>
    #include <stdio.h>
    #define PORT 0x378


    int main (void)



    {

    int count;
    {
    float A,A1,A2,B,B1,B2,C,C1,C2,D,D1,D2;






    clrscr();

    printf(" \n POSITION PROGRAM. \n\n\n");
    sleep(1);
    printf(" Enter DEGREES clockwise... ");
    scanf("%f",&A);
    printf(" Enter DEGREES anti-clockwise...");
    scanf("%f",&B);
    printf(" Enter DEGREES tilt... ");
    scanf("%f",&C);
    printf(" Enter DEGREES tilt reverse... ");
    scanf("%f",&D);
    printf(" \n\n");

    {

    A1= (A*(2.96111111));
    A2= (int)(A1+0.5);
    printf(" Steps clockwise = %f steps\n",A2);
    }


    {
    B1= (B*(2.961111111));
    B2= (int)(B1+0.5);
    printf(" Steps anti-clockwise = %f steps\n",B2);
    }


    {
    C1 = (C*(1.1444));
    C2 = (int)(C1+0.5);
    printf(" Steps azimith = %f steps\n",C2);
    }

    {
    D1 = (D*(1.1444));
    D2 = (int)(D1+0.5);
    printf(" Steps reverse azimith= %f steps\n",D2);
    }

    {



    for (count = 1; count<=A1; count++)
    {
    outp (PORT,0x00); /* turn on */
    delay(200);
    outp(PORT,0x01); /*turn off */
    delay(200);

    }


    for (count = 1; count<= B; count++)
    {
    outp(PORT,0x04);
    delay(200);
    outp(PORT,0x05);
    delay(200);
    }


    for (count = 1; count<=C; count ++)
    {
    outp(PORT,0x00);
    delay(3000);
    outp(PORT,0x02);
    delay(3000);
    }


    for (count = 1; count<=D; count ++)
    {
    outp(PORT,0x09);
    delay(3000);
    outp(PORT,0x10);
    delay(3000);
    }

    delay(5000);
    }

    }

    getch();
    return 0;
    }


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Ah apologies; I was on the wrong track.

    Your output format specifiers are incorrect for printing whole numbers only; i.e. you're using %f instead of %d in your printf statements.

    e.g.
    printf(" Steps anti-clockwise = %f steps\n",B2);
    
    Should be the following if you don't want the decimal and trailing zeros;
    printf(" Steps anti-clockwise = %[B]d[/B] steps\n",B2);
    


  • Registered Users Posts: 382 ✭✭DiarmaidGNR


    when I change %f to %d , the output answer is 0, instead of 9.00000, no matter what values i enter into the equation


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Youre A2, B2, C2 and D2 variables are defined as floats instead of integers.
    You are casting to integer and then back to float.

    Change A2, B2, C2 and D2 to int and you'll be sorted.
    int A2, B2,C2,D2;
    


  • Registered Users Posts: 382 ✭✭DiarmaidGNR


    putting int A2, B2 etc at the top of the code along with int count instead of floating did it, and also changing %f to %d.

    printf(" Steps clockwise = %d steps\n",A2);

    Thanks to everyone for their help, I'll need it again soon!


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    And just as an aside; you should really try and extract some functions out of that code to avoid repetition and promote maintainability.

    A perfect choice would be your rounding statements; that should really be a function rather than in-line in the code.


  • Registered Users Posts: 2,921 ✭✭✭Remmy


    yep that sounds right i think.


  • Registered Users Posts: 382 ✭✭DiarmaidGNR


    Don't suppose you know how to write "is not equal to " in turbo c?
    ie.
    if(((value is not equal to 10)))
    {
    do x
    }
    else do y


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    if(value != 10)
    


Advertisement