Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. truncating float

truncating float

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutoriallounge
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EmbdedCguru
    wrote on last edited by
    #1

    HI , this is a question purely related to C programming. I am calculating a division between two float numbers e.g 4.00 / 3.00. Now if i answer 1.3333 , the functions returns that it is wrong. i just want to know how to truncate in Float values in C ??? here is the copy of the code: float d1,d2,d3; main( ) { srand(time(0)); // Initialize random number generator. ans = 1; while ( ans != 0 ) { /// starting of For loop //////////////// wrg =0; for ( cnt=0;cnt<3;cnt++) { d1 = float(rand()%x+2 ); d2 = float(rand()%x+2) ; printf("\n\t Solve this %f / %f :.........\n", d1, d2 ); scanf("%f", &d3); divid_cheque(d1,d2,d3); }// ending for loop printf("\n\t Do you want to continue........\n"); printf("\n\t Type 1 = continue or 0 = to terminate \n"); scanf("\n\t%d", &ans); } printf("\n Total number of wrong answers : %d out of %d \n\n", wrg, cnt); return 0; } //////////////////////Division(/) Function defined////////////////////////////////////////////////////////////////// //unsigned int division( unsigned int y, unsigned int z, unsigned int x ) // unsigned int divid_cheque( unsigned int j, unsigned int k, float l) // int divid_cheque( float j, float k, float l ) int divid_cheque( float j, float k, float l ) { float as; as=j/k; if ( l== as ) printf("\n \t Right answer..........\n"); if ( l != as ) { printf(" \n \t << WRONG ANSWER >> \n"); printf("\n \t the right answer is = %f",as); wrg++ ; // incrementing counter for wrong answers } else return (wrg); } ////////////////////////////////////////////////////////////////////////////////////////////////

    T C _ B 4 Replies Last reply
    0
    • E EmbdedCguru

      HI , this is a question purely related to C programming. I am calculating a division between two float numbers e.g 4.00 / 3.00. Now if i answer 1.3333 , the functions returns that it is wrong. i just want to know how to truncate in Float values in C ??? here is the copy of the code: float d1,d2,d3; main( ) { srand(time(0)); // Initialize random number generator. ans = 1; while ( ans != 0 ) { /// starting of For loop //////////////// wrg =0; for ( cnt=0;cnt<3;cnt++) { d1 = float(rand()%x+2 ); d2 = float(rand()%x+2) ; printf("\n\t Solve this %f / %f :.........\n", d1, d2 ); scanf("%f", &d3); divid_cheque(d1,d2,d3); }// ending for loop printf("\n\t Do you want to continue........\n"); printf("\n\t Type 1 = continue or 0 = to terminate \n"); scanf("\n\t%d", &ans); } printf("\n Total number of wrong answers : %d out of %d \n\n", wrg, cnt); return 0; } //////////////////////Division(/) Function defined////////////////////////////////////////////////////////////////// //unsigned int division( unsigned int y, unsigned int z, unsigned int x ) // unsigned int divid_cheque( unsigned int j, unsigned int k, float l) // int divid_cheque( float j, float k, float l ) int divid_cheque( float j, float k, float l ) { float as; as=j/k; if ( l== as ) printf("\n \t Right answer..........\n"); if ( l != as ) { printf(" \n \t << WRONG ANSWER >> \n"); printf("\n \t the right answer is = %f",as); wrg++ ; // incrementing counter for wrong answers } else return (wrg); } ////////////////////////////////////////////////////////////////////////////////////////////////

      T Offline
      T Offline
      Tim Craig
      wrote on last edited by
      #2

      When doing comparisons with calculated float values, you generally need to decide how close is close enough. Take the absolute value of the difference between the two and see if it is less than some epsilon value which is your tolerance for "exactness".

      You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

      1 Reply Last reply
      0
      • E EmbdedCguru

        HI , this is a question purely related to C programming. I am calculating a division between two float numbers e.g 4.00 / 3.00. Now if i answer 1.3333 , the functions returns that it is wrong. i just want to know how to truncate in Float values in C ??? here is the copy of the code: float d1,d2,d3; main( ) { srand(time(0)); // Initialize random number generator. ans = 1; while ( ans != 0 ) { /// starting of For loop //////////////// wrg =0; for ( cnt=0;cnt<3;cnt++) { d1 = float(rand()%x+2 ); d2 = float(rand()%x+2) ; printf("\n\t Solve this %f / %f :.........\n", d1, d2 ); scanf("%f", &d3); divid_cheque(d1,d2,d3); }// ending for loop printf("\n\t Do you want to continue........\n"); printf("\n\t Type 1 = continue or 0 = to terminate \n"); scanf("\n\t%d", &ans); } printf("\n Total number of wrong answers : %d out of %d \n\n", wrg, cnt); return 0; } //////////////////////Division(/) Function defined////////////////////////////////////////////////////////////////// //unsigned int division( unsigned int y, unsigned int z, unsigned int x ) // unsigned int divid_cheque( unsigned int j, unsigned int k, float l) // int divid_cheque( float j, float k, float l ) int divid_cheque( float j, float k, float l ) { float as; as=j/k; if ( l== as ) printf("\n \t Right answer..........\n"); if ( l != as ) { printf(" \n \t << WRONG ANSWER >> \n"); printf("\n \t the right answer is = %f",as); wrg++ ; // incrementing counter for wrong answers } else return (wrg); } ////////////////////////////////////////////////////////////////////////////////////////////////

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        truncate at which decimal place ?

        // truncate 3 places to the right of the decimal
        float f1 = 4./3.;
        float f2 = (float)((int)(f1 * 1000.0)) / 1000.0;

        image processing toolkits | batch image processing

        1 Reply Last reply
        0
        • E EmbdedCguru

          HI , this is a question purely related to C programming. I am calculating a division between two float numbers e.g 4.00 / 3.00. Now if i answer 1.3333 , the functions returns that it is wrong. i just want to know how to truncate in Float values in C ??? here is the copy of the code: float d1,d2,d3; main( ) { srand(time(0)); // Initialize random number generator. ans = 1; while ( ans != 0 ) { /// starting of For loop //////////////// wrg =0; for ( cnt=0;cnt<3;cnt++) { d1 = float(rand()%x+2 ); d2 = float(rand()%x+2) ; printf("\n\t Solve this %f / %f :.........\n", d1, d2 ); scanf("%f", &d3); divid_cheque(d1,d2,d3); }// ending for loop printf("\n\t Do you want to continue........\n"); printf("\n\t Type 1 = continue or 0 = to terminate \n"); scanf("\n\t%d", &ans); } printf("\n Total number of wrong answers : %d out of %d \n\n", wrg, cnt); return 0; } //////////////////////Division(/) Function defined////////////////////////////////////////////////////////////////// //unsigned int division( unsigned int y, unsigned int z, unsigned int x ) // unsigned int divid_cheque( unsigned int j, unsigned int k, float l) // int divid_cheque( float j, float k, float l ) int divid_cheque( float j, float k, float l ) { float as; as=j/k; if ( l== as ) printf("\n \t Right answer..........\n"); if ( l != as ) { printf(" \n \t << WRONG ANSWER >> \n"); printf("\n \t the right answer is = %f",as); wrg++ ; // incrementing counter for wrong answers } else return (wrg); } ////////////////////////////////////////////////////////////////////////////////////////////////

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          The number of digits in a float after the decimal point cannot be truncated. You can only limit the number of digits that you want displayed. For example printf("%6.4f", 4.0/3.0); will display 1.3333.

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          1 Reply Last reply
          0
          • E EmbdedCguru

            HI , this is a question purely related to C programming. I am calculating a division between two float numbers e.g 4.00 / 3.00. Now if i answer 1.3333 , the functions returns that it is wrong. i just want to know how to truncate in Float values in C ??? here is the copy of the code: float d1,d2,d3; main( ) { srand(time(0)); // Initialize random number generator. ans = 1; while ( ans != 0 ) { /// starting of For loop //////////////// wrg =0; for ( cnt=0;cnt<3;cnt++) { d1 = float(rand()%x+2 ); d2 = float(rand()%x+2) ; printf("\n\t Solve this %f / %f :.........\n", d1, d2 ); scanf("%f", &d3); divid_cheque(d1,d2,d3); }// ending for loop printf("\n\t Do you want to continue........\n"); printf("\n\t Type 1 = continue or 0 = to terminate \n"); scanf("\n\t%d", &ans); } printf("\n Total number of wrong answers : %d out of %d \n\n", wrg, cnt); return 0; } //////////////////////Division(/) Function defined////////////////////////////////////////////////////////////////// //unsigned int division( unsigned int y, unsigned int z, unsigned int x ) // unsigned int divid_cheque( unsigned int j, unsigned int k, float l) // int divid_cheque( float j, float k, float l ) int divid_cheque( float j, float k, float l ) { float as; as=j/k; if ( l== as ) printf("\n \t Right answer..........\n"); if ( l != as ) { printf(" \n \t << WRONG ANSWER >> \n"); printf("\n \t the right answer is = %f",as); wrg++ ; // incrementing counter for wrong answers } else return (wrg); } ////////////////////////////////////////////////////////////////////////////////////////////////

            B Offline
            B Offline
            beko
            wrote on last edited by
            #5

            Hello i use the following in order to truncate a double value (found somewhere and modified :) ) However you must be careful in here, because there could be an overflow in double fac = Num/Seed

            #include <math.h>
            #include <stdio.h>

            double truncate(double Num, double Seed)
            {
            // get rational factor
            double fac = Num / Seed;

            // get natural factor
            fac = (fac < 0) ? (ceil(fac)) : (floor(fac));
            // fac = (fac < 0) ? static\_cast<int>(fac-0.5) : static\_cast<int>(fac+0.5);
            
            // return multiple
            return fac \* Seed;
            

            }

            int main()
            {
            double val = 4./3.;

            printf("%6.4f\\n",truncate(val,1e-04));
            
            return 0;
            

            }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups