no muntoo, don't be blase and narrow-minded, your formula is wrong for negative values!
see it at last!
And if you're not sure: test it (or simply believe it to me.)
this is valid:
Stroustrup, C.6.2.6 wrote:The fractional part is discarded. In other words, conversion from a floating-point type to an integer type truncates.
Also, "right/wrong" really depends on your definition of it, what you're trying to do, and what you actually need. But, yeah, your formula should be the "correct" one for what you believe is correct.
Commit to LEGO Mindstorms Robotics Stack Exchange: bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
what's mathematically right or wrong is not decided by me or by you but by the mathematic laws and axioms or definitions.
but you may believe what you want, you are not forbidden to believe wrong facts.
Indeed "my" round() function returns for -0.1 => 0 and e.g. for -0.6 => -1
you might have seen that if you had tried.
Hi Doc,
Although your definition may be mathematically correct your interpretation is not the same as that used my many floating point libraries. So for instance Java (and lots of other languages) defines round as:
(long)Math.floor(a + 0.5d) http://download.oracle.com/javase/6/doc ... nd(double)
Which means that round(-0.5) is 0 not -1 (it would be -1 with your definition)...
I thought I would just give you another reason to hate Java...
no, you didn't :)
the -0.5 rounding is indeed interpreted differently, but that's not the point.
If -0.49999999999... is rounded to 0 and -0.5 to -1
or
-0.5 is rounded to 0 and -0.500000000000....1 to -1
that's not a mathematic problem but just a convention and the result depends maybe only on the binary floating point notation of -0.5 and it's precision .
(is 0.99999999999999999... as a fp value ==1 ? mathematically, it is, of course, but you won't get this equation with computers probably...)
Some define a rounding from every exact i+ 0.5 (for all integer i either positive or negative) as the next bigger integer number (i+1).
the symmetric associative definition
round(-x)=-round(x)
is nevertheless more logical, but who said that Java programmers do think logically?^^
(Not even C programmers do it always and continuously.)