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. Algorithms
  4. Just a simple question about rounding negative numbers, I hope.

Just a simple question about rounding negative numbers, I hope.

Scheduled Pinned Locked Moved Algorithms
questioncssbeta-testingcode-review
5 Posts 2 Posters 45 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.
  • J Offline
    J Offline
    Jeremy Falcon
    wrote on last edited by
    #1

    I think I know the answer to this, but would love some feedback from math peeps much better than me at this. After some Googling, I still don't know of anything definitive. So.... When it comes to rounding off positive numbers, fortunately everyone is one the same page:

    Round Off
    7.5 => 8.0
    7.4 => 7.0

    We get the idea. And when it comes to rounding up, we all know to move away from zero and rounding down we move towards zero. Now, that's all good and well... for positive numbers. But, what about negative numbers?

    Round Off
    -7.5 => 8.0 or 7.0?
    -7.4 => 8.0 or 7.0?

    I suppose it depends on the definition of `up` and `down`. When less than zero would `up` be moving towards zero and `down` be moving away from zero? If so, then is this correct?

    Round Off
    -7.5 => 7.0
    -7.4 => 8.0

    Or am I off my rocker? :laugh: Much appreciated. Edit: I just found out that the terms "up" and "down" are just colloquialisms and don't have really any bearing on math. So, when negative "up" would be down and down would be "up". So, something like this would be true... -1.4 rounds to -1 -1.5 rounds to -2 Anywho, if anyone can confirm I'm not off my rocker with this, would totally appreciate it.

    Jeremy Falcon

    Mircea NeacsuM 1 Reply Last reply
    0
    • J Jeremy Falcon

      I think I know the answer to this, but would love some feedback from math peeps much better than me at this. After some Googling, I still don't know of anything definitive. So.... When it comes to rounding off positive numbers, fortunately everyone is one the same page:

      Round Off
      7.5 => 8.0
      7.4 => 7.0

      We get the idea. And when it comes to rounding up, we all know to move away from zero and rounding down we move towards zero. Now, that's all good and well... for positive numbers. But, what about negative numbers?

      Round Off
      -7.5 => 8.0 or 7.0?
      -7.4 => 8.0 or 7.0?

      I suppose it depends on the definition of `up` and `down`. When less than zero would `up` be moving towards zero and `down` be moving away from zero? If so, then is this correct?

      Round Off
      -7.5 => 7.0
      -7.4 => 8.0

      Or am I off my rocker? :laugh: Much appreciated. Edit: I just found out that the terms "up" and "down" are just colloquialisms and don't have really any bearing on math. So, when negative "up" would be down and down would be "up". So, something like this would be true... -1.4 rounds to -1 -1.5 rounds to -2 Anywho, if anyone can confirm I'm not off my rocker with this, would totally appreciate it.

      Jeremy Falcon

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      For the most part you are right. However:

      Jeremy Falcon wrote:

      I just found out that the terms "up" and "down" are just colloquialisms

      The term "rounding" is also a colloquialism. Depends on what function you use for rounding.

      round (-1.4) = -1
      round (-1.5) = -2
      round (-2.5) = -3
      rint (-2.5) = -2

      rint rounds using current rounding mode, while round rounds always away from 0. For MSVC default rounding mode is checked and set using fegetround and fesetround[[^](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fegetround-fesetround2? (by default FEview=msvc-170 "New Window")] (by default FE_TONEAREST).

      Mircea

      J 1 Reply Last reply
      0
      • Mircea NeacsuM Mircea Neacsu

        For the most part you are right. However:

        Jeremy Falcon wrote:

        I just found out that the terms "up" and "down" are just colloquialisms

        The term "rounding" is also a colloquialism. Depends on what function you use for rounding.

        round (-1.4) = -1
        round (-1.5) = -2
        round (-2.5) = -3
        rint (-2.5) = -2

        rint rounds using current rounding mode, while round rounds always away from 0. For MSVC default rounding mode is checked and set using fegetround and fesetround[[^](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fegetround-fesetround2? (by default FEview=msvc-170 "New Window")] (by default FE_TONEAREST).

        Mircea

        J Offline
        J Offline
        Jeremy Falcon
        wrote on last edited by
        #3

        Mircea Neacsu wrote:

        rint rounds using current rounding mode, while round rounds always away from 0.

        Holy crap, all these years and I never knew of `rint`. Good to know though. And thanks for the bit about `round` moving away from zero. That's a lot easier to understand. :laugh:

        Jeremy Falcon

        Mircea NeacsuM 1 Reply Last reply
        0
        • J Jeremy Falcon

          Mircea Neacsu wrote:

          rint rounds using current rounding mode, while round rounds always away from 0.

          Holy crap, all these years and I never knew of `rint`. Good to know though. And thanks for the bit about `round` moving away from zero. That's a lot easier to understand. :laugh:

          Jeremy Falcon

          Mircea NeacsuM Offline
          Mircea NeacsuM Offline
          Mircea Neacsu
          wrote on last edited by
          #4

          My mnemonic trick: think of a fat, round zero pushing everything away :laugh:

          Mircea

          J 1 Reply Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            My mnemonic trick: think of a fat, round zero pushing everything away :laugh:

            Mircea

            J Offline
            J Offline
            Jeremy Falcon
            wrote on last edited by
            #5

            Noice :laugh:

            Jeremy Falcon

            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