A
Artem Voroztsov
Is it intentional that (-1/2) gives -1 ?
Please, give me links if there is any rationale.
(i have ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32] )
# Ruby
puts -1/2; # => -1
// C (
#include <stdio.h>
int main() {
printf("%d\n", -1/2); // => 0 (in most platforms. )
return 0;
}
It looks like standard C9x states not only equation b*(a/b) + a%b == a
but also "truncation towards zero".
In http://www.open-std.org/JTC1/SC22/WG14/www/docs/C99RationaleV5.10.pdf
one could read (6.5.5, page 74):
.. In Fortran, however, the result will always
truncate toward zero, and the overhead seems to be acceptable to the
numeric
programming community. Therefore, C99 now requires similar behavior, which
should facilitate porting of code from Fortran to C...
Please, give me links if there is any rationale.
(i have ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32] )
# Ruby
puts -1/2; # => -1
// C (
#include <stdio.h>
int main() {
printf("%d\n", -1/2); // => 0 (in most platforms. )
return 0;
}
It looks like standard C9x states not only equation b*(a/b) + a%b == a
but also "truncation towards zero".
In http://www.open-std.org/JTC1/SC22/WG14/www/docs/C99RationaleV5.10.pdf
one could read (6.5.5, page 74):
.. In Fortran, however, the result will always
truncate toward zero, and the overhead seems to be acceptable to the
numeric
programming community. Therefore, C99 now requires similar behavior, which
should facilitate porting of code from Fortran to C...