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. Rounding

Rounding

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
14 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.
  • K Offline
    K Offline
    knapak
    wrote on last edited by
    #1

    Hiya I have a variable that shoud return a value of say... 50. Instead it's returning 49.9999999956 which may sound pretty good but I need the 50. I found this thing in the Std library which is an enum called float_round_style that includes something called round_toward_infinity... but I can't find how to actually use the durn code, an example or anything other than the description :mad:. Can anyone tell me how to use it? Is there any other easier way to acomplish what I want? Thanks!!! :cool:

    C S PJ ArendsP C 4 Replies Last reply
    0
    • K knapak

      Hiya I have a variable that shoud return a value of say... 50. Instead it's returning 49.9999999956 which may sound pretty good but I need the 50. I found this thing in the Std library which is an enum called float_round_style that includes something called round_toward_infinity... but I can't find how to actually use the durn code, an example or anything other than the description :mad:. Can anyone tell me how to use it? Is there any other easier way to acomplish what I want? Thanks!!! :cool:

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      double d = 49.9999999956; int i = (int)d; Christian Graus - Microsoft MVP - C++

      K B 2 Replies Last reply
      0
      • C Christian Graus

        double d = 49.9999999956; int i = (int)d; Christian Graus - Microsoft MVP - C++

        K Offline
        K Offline
        knapak
        wrote on last edited by
        #3

        Nope, that leaves me with 49, not 50.

        C 1 Reply Last reply
        0
        • K knapak

          Hiya I have a variable that shoud return a value of say... 50. Instead it's returning 49.9999999956 which may sound pretty good but I need the 50. I found this thing in the Std library which is an enum called float_round_style that includes something called round_toward_infinity... but I can't find how to actually use the durn code, an example or anything other than the description :mad:. Can anyone tell me how to use it? Is there any other easier way to acomplish what I want? Thanks!!! :cool:

          S Offline
          S Offline
          Shog9 0
          wrote on last edited by
          #4

          ceil()

          You must be careful in the forest Broken glass and rusty nails If you're to bring back something for us I have bullets for sale...

          K 1 Reply Last reply
          0
          • K knapak

            Nope, that leaves me with 49, not 50.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            I find that very difficult to comprehend. Numbers should round up from .5 upwards, thought. That makes the floor function totally useless. This will work. #include double d = 49.99999956; int i = (int)ceil(d); Christian Graus - Microsoft MVP - C++

            K G 2 Replies Last reply
            0
            • C Christian Graus

              I find that very difficult to comprehend. Numbers should round up from .5 upwards, thought. That makes the floor function totally useless. This will work. #include double d = 49.99999956; int i = (int)ceil(d); Christian Graus - Microsoft MVP - C++

              K Offline
              K Offline
              knapak
              wrote on last edited by
              #6

              Thank you! that sure works!!! :-D

              1 Reply Last reply
              0
              • S Shog9 0

                ceil()

                You must be careful in the forest Broken glass and rusty nails If you're to bring back something for us I have bullets for sale...

                K Offline
                K Offline
                knapak
                wrote on last edited by
                #7

                Thank you, that was easy!!! :-D

                1 Reply Last reply
                0
                • C Christian Graus

                  I find that very difficult to comprehend. Numbers should round up from .5 upwards, thought. That makes the floor function totally useless. This will work. #include double d = 49.99999956; int i = (int)ceil(d); Christian Graus - Microsoft MVP - C++

                  G Offline
                  G Offline
                  G_S
                  wrote on last edited by
                  #8

                  The Function "double ceil(double);" will always round UP. that is even if you have 49.00005 the ceil() function will round UP to 50; if you want to round to the nearest Integer you need somthing else. G_S

                  C S 2 Replies Last reply
                  0
                  • G G_S

                    The Function "double ceil(double);" will always round UP. that is even if you have 49.00005 the ceil() function will round UP to 50; if you want to round to the nearest Integer you need somthing else. G_S

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    G_S wrote: The Function "double ceil(double);" will always round UP. I guess that's why it's called ceil ( as in ceiling ). G_S wrote: if you want to round to the nearest Integer you need somthing else. I was helping someone who wants to round UP. However, casting to int seems to just drop the floating point part, when I assumed it would round to the nearest int, up or down. HEnce, I suggested ceil. Christian Graus - Microsoft MVP - C++

                    1 Reply Last reply
                    0
                    • K knapak

                      Hiya I have a variable that shoud return a value of say... 50. Instead it's returning 49.9999999956 which may sound pretty good but I need the 50. I found this thing in the Std library which is an enum called float_round_style that includes something called round_toward_infinity... but I can't find how to actually use the durn code, an example or anything other than the description :mad:. Can anyone tell me how to use it? Is there any other easier way to acomplish what I want? Thanks!!! :cool:

                      PJ ArendsP Offline
                      PJ ArendsP Offline
                      PJ Arends
                      wrote on last edited by
                      #10

                      double d = 49.99999999; int i = (int)(d + 0.5); i will now be properly rounded.


                      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


                      Honoured as one of The Most Helpful Members of 2004

                      Within you lies the power for good; Use it!

                      G 1 Reply Last reply
                      0
                      • PJ ArendsP PJ Arends

                        double d = 49.99999999; int i = (int)(d + 0.5); i will now be properly rounded.


                        "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


                        Honoured as one of The Most Helpful Members of 2004

                        G Offline
                        G Offline
                        G_S
                        wrote on last edited by
                        #11

                        PJ Arends wrote: double d = 49.99999999; int i = (int)(d + 0.5); Nice very nice but what about the (-) negative ones test it you'll see. int i; if(d<0) i = (int)(d - 0.5); else i = (int)(d + 0.5); G_S

                        1 Reply Last reply
                        0
                        • G G_S

                          The Function "double ceil(double);" will always round UP. that is even if you have 49.00005 the ceil() function will round UP to 50; if you want to round to the nearest Integer you need somthing else. G_S

                          S Offline
                          S Offline
                          Shog9 0
                          wrote on last edited by
                          #12

                          To alter the processor's default rounding behavior, you can use the _controlfp() function to set specific FPU flags (such as _RC_CHOP, _RC_UP, _RC_DOWN, _RC_NEAR).

                          You must be careful in the forest Broken glass and rusty nails If you're to bring back something for us I have bullets for sale...

                          1 Reply Last reply
                          0
                          • K knapak

                            Hiya I have a variable that shoud return a value of say... 50. Instead it's returning 49.9999999956 which may sound pretty good but I need the 50. I found this thing in the Std library which is an enum called float_round_style that includes something called round_toward_infinity... but I can't find how to actually use the durn code, an example or anything other than the description :mad:. Can anyone tell me how to use it? Is there any other easier way to acomplish what I want? Thanks!!! :cool:

                            C Offline
                            C Offline
                            cmk
                            wrote on last edited by
                            #13

                            The method PJ showed, and you expanded for negative numbers, is a standard way. Shog hinted at another way doing it using _controlfp (or _control87). The rounding control flags specify how the assembly instruction frndint will round. From Intel info on frndint: The Control Word 16-bit register is used ... The RC field (bits 11 and 10) or Rounding Control determines how the FPU will round results in one of four ways: 00 = Round to nearest, or to even if equidistant (this is the initialized state) 01 = Round down (toward -infinity) 10 = Round up (toward +infinity) 11 = Truncate (toward 0) So, by default, it rounds the way you want. The problem is i don't know of any C function that calls this, hence the workaround of adding 0.5 and then truncating by cast to int. A more general (non-portable) solution would be to write your own function that calls frndint. e.g.

                            __forceinline double FkRound( double N )
                            {
                            __asm {
                            fld N
                            frndint
                            }
                            }

                            Before calling this you could call _controlfp() to set the way you want rounding to work, or just to make sure it hasn't been changed by some other code. This works for both positive and negative numbers. ...cmk Save the whales - collect the whole set

                            1 Reply Last reply
                            0
                            • C Christian Graus

                              double d = 49.9999999956; int i = (int)d; Christian Graus - Microsoft MVP - C++

                              B Offline
                              B Offline
                              Bob Stanneveld
                              wrote on last edited by
                              #14

                              Hello, The standard implies that when a floating point value is converted to a intergral value, the fractional part is discarded... I also got the blogging virus..[^]

                              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