Just a simple question about rounding negative numbers, I hope.
-
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.0We 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.0Or 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
-
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.0We 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.0Or 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
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) = -2rint
rounds using current rounding mode, whileround
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 defaultFE_TONEAREST
).Mircea
-
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) = -2rint
rounds using current rounding mode, whileround
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 defaultFE_TONEAREST
).Mircea
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 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
My mnemonic trick: think of a fat, round zero pushing everything away :laugh:
Mircea
-
My mnemonic trick: think of a fat, round zero pushing everything away :laugh:
Mircea
Noice :laugh:
Jeremy Falcon