首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言中整除有关问题

2013-02-15 
C语言中整除问题C语言中,当除数、被除数和商都定义成int型时,如不能整除,商会自动转换成int类型。那么,C语言

C语言中整除问题
C语言中,当除数、被除数和商都定义成int型时,如不能整除,商会自动转换成int类型。
那么,C语言是不是默认舍去小数部分,不论小数部分是否足够大?
如,10/3=3,舍去了小数部分;-10/3=-3,舍去了小数部分。
我试了试编译这两个例子,结果是这样。
对不对?我不太肯定 c
[解决办法]

引用:
对于负数,不同的编译器处理方式似乎是不一样的


When integers are divided and the division is inexact, if both operands are positive the result of the / operator is the largest integer less than the algebraic quotient and the result of the % operator is positive. If either operand is negative, whether the result of the / operator is the largest integer less than the algebraic quotient or the smallest integer greater than the algebraic quotient is implementation-defined, as is the sign of the result of the % operator. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.

[解决办法]
上面的文字来自
C89
3.3.5 Multiplicative operators
http://www.bsb.me.uk/ansi-c/ansi-c-one-file.html#sec_3_3_5

热点排行
Bad Request.