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. How to make truncation in C++

How to make truncation in C++

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++question
12 Posts 7 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.
  • R Rajesh R Subramanian

    What function? Just cast it to an integer. Or if you want a function:

    __inline int trunc(float f)
    {
    return (int)f;
    }

    It is a crappy thing, but it's life -^ Carlo Pallini

    A Offline
    A Offline
    alikalik
    wrote on last edited by
    #3

    If I do it in this way, the results are:

    trunc(7.1) = 7;
    trunc(7.6) = 7; // fractional part is just dropped

    R 1 Reply Last reply
    0
    • A alikalik

      How can I make a trunc() function in C++, Example:

      trunc(7.1) = 7;
      trunc(7.6) = 8;

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #4

      Presuming you want similar behaviour for numbers < 0

      trunc(-7.1) = -7;
      trunc(-7.6) = -8;

      then this should work

      int trunc(double d) { return floor(d+0.5); }

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      1 Reply Last reply
      0
      • A alikalik

        If I do it in this way, the results are:

        trunc(7.1) = 7;
        trunc(7.6) = 7; // fractional part is just dropped

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #5

        Oh sorry, I overlooked your previous post. You can use one of those flooring or ceiling functions. [Added] OK, Stuart just replied with a floor function at the same time. [/added]

        It is a crappy thing, but it's life -^ Carlo Pallini

        1 Reply Last reply
        0
        • A alikalik

          How can I make a trunc() function in C++, Example:

          trunc(7.1) = 7;
          trunc(7.6) = 8;

          M Offline
          M Offline
          Michael Schubert
          wrote on last edited by
          #6

          rounded_num = (int)(num + (num < 0 ? -0.5 : 0.5));

          1 Reply Last reply
          0
          • A alikalik

            How can I make a trunc() function in C++, Example:

            trunc(7.1) = 7;
            trunc(7.6) = 8;

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #7

            You call it trunc(), but that is the expected behaviour of round() function. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            R 1 Reply Last reply
            0
            • C CPallini

              You call it trunc(), but that is the expected behaviour of round() function. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #8

              I was guessing that the OP is trying to implement the functionality himself.

              It is a crappy thing, but it's life -^ Carlo Pallini

              C 1 Reply Last reply
              0
              • R Rajesh R Subramanian

                I was guessing that the OP is trying to implement the functionality himself.

                It is a crappy thing, but it's life -^ Carlo Pallini

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #9

                Your guess is right, I suppose. However, OP must not give, even to his functions, deceiving names, unless, of course, he is a Klingon programmer. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                1 Reply Last reply
                0
                • A alikalik

                  How can I make a trunc() function in C++, Example:

                  trunc(7.1) = 7;
                  trunc(7.6) = 8;

                  K Offline
                  K Offline
                  KarstenK
                  wrote on last edited by
                  #10

                  you are looking for a rounding function int round(double d) {    return (int) (d + 0.5); }

                  Press F1 for help or google it. Greetings from Germany

                  1 Reply Last reply
                  0
                  • A alikalik

                    How can I make a trunc() function in C++, Example:

                    trunc(7.1) = 7;
                    trunc(7.6) = 8;

                    D Offline
                    D Offline
                    Dan 0
                    wrote on last edited by
                    #11

                    The FPU already does this for you.

                       //! Round double to integer
                    inline int iround(double v){
                    	int retval;
                    	\_\_asm fld qword ptr \[v\]
                    	\_\_asm fistp dword ptr \[retval\]
                    	return retval;
                    
                    }
                    
                    //! Round double to unsigned integer
                    inline unsigned int uround(double v){
                    	unsigned retval;
                    	\_\_asm fld qword ptr \[v\]
                    	\_\_asm fistp dword ptr \[retval\]
                    	return retval;
                    }
                    
                    M 1 Reply Last reply
                    0
                    • D Dan 0

                      The FPU already does this for you.

                         //! Round double to integer
                      inline int iround(double v){
                      	int retval;
                      	\_\_asm fld qword ptr \[v\]
                      	\_\_asm fistp dword ptr \[retval\]
                      	return retval;
                      
                      }
                      
                      //! Round double to unsigned integer
                      inline unsigned int uround(double v){
                      	unsigned retval;
                      	\_\_asm fld qword ptr \[v\]
                      	\_\_asm fistp dword ptr \[retval\]
                      	return retval;
                      }
                      
                      M Offline
                      M Offline
                      Michael Schubert
                      wrote on last edited by
                      #12

                      Nice.

                      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