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

c语言的整除0

2012-02-11 
请教高手c语言的整除0#include conio.h#include stdio.h/*intmain(){inta1,b0printf( Divisionbyz

请教高手c语言的整除0
#include <conio.h>
#include <stdio.h>
/*int   main()
{
        int   a=1,b=0;
        printf( "Division   by   zero:%d\n ",a/b);//除数为0与cpu的异常处理有关????  
        getchar();
        return   0;
}*/
/*以下是汇编代码
004012D7     idiv                 eax,dword   ptr   [ecx]  
004012D9     mov                   dword   ptr   [ebp-0Ch],eax  
004012DC     mov                   eax,dword   ptr   [ebp-0Ch]  
004012DF     mov                   dword   ptr   [esp+4],eax  
004012E3     mov                   dword   ptr   [esp],403000h  
004012EA     call                 00401830  
004012EF     call                 00401900  
004012F4     mov                   eax,0  
*/
int   main()
{
        double   x=1.0,y=-1.0,z=0.0;
printf( "division   by   zero:%f   %f\n ",x/z,y/z);
getchar();
return   0;
}
问题一:为什么整除int型会产生错误?是什么错误?
二:用double型的时候结果为1.#INF00   -1.#INF00作何解释???


[解决办法]
1. 产生硬件错误中断
2. #INF 表示无穷大
[解决办法]
正无穷

[解决办法]
整形有取值范围的
[解决办法]
可能整形无法表示正无穷,不太清楚。
[解决办法]
如浮点异常有时就是除0引起.
double的0.0实际上并不是所有位都是0,而是一个非常小的小数因此结果是无限小.
[解决办法]
#INF严格的讲不能叫无穷,而应该是浮点数的溢出.
[解决办法]
阶码全一,尾数全零严格讲就是无穷, 不是溢出, 遵循IEEE754标准的实现都是酱紫...
double float 的 0.0 有两种表示, +0和-0 , +0 就是全0, -0 最高位(符号位)为1, 其余全0 , 根本就不是一个非常小的数 ...
[解决办法]
会不会跟编译器有关系?
[解决办法]
浮点数据溢出,
这个和数据格式标准有关。
[解决办法]
在标准中就规定了这个 除0 是禁止的
[解决办法]
我想问一下, 除0的意义是什么?

不考虑程序,
就从数学理论讲,
这个 除0 就没有意义的吧?
[解决办法]
深奥啊
此处的0在机器中表示的ASC码二进制数吧,也就是说不是机器0了,那么是可以除的吧,但是好象记得在汇编中除0是不被允许的,这应该是规定吧
[解决办法]
除0会导致CPU产生一个异常(异常号为:0x300),如果用户没有对这种异常进行处理,系统就按照默认的处理方式进行处理。
[解决办法]
发生除0中断
[解决办法]
除0异常是Pentium system 就定义了的:


A Pentium system can have up to 256 different exception types. Numbers in the range 0 to 31 correspond to exceptions that are defined by the Pentium architecture, and thus are identical for any Pentium-class system. Numbers in the range 32 to 255 correspond to interrupts and traps that are defined by the operating system.

**********Figure: Examples of exceptions in Pentium systems**********

Exception Number Description Exception Class
0 divide error fault
13 general protection fault fault
14 page fault fault
18 machine check abort
32–127 OS-defined exceptions interrupt or trap
128 (0x80) system call trap
129–255 OS-defined exceptions interrupt or trap


除0错误是一个fault类型的异常,fault类型的异常说明如下:
Faults result from error conditions that a handler might be able to correct. When a fault occurs, the processor transfers control to the fault handler. If the handler is able to correct the error condition, it returns control to the faulting instruction, thereby reexecuting it. Otherwise, the handler returns to an abort routine in the kernel that terminates the application program that caused the fault

也就是说,fault是潜在可修复的异常(Potentially recoverable error),如果exception hander (内核代码)可以修复这个错误,就会返回出错指令继续执行,否则就会调用abort例程终止程序


除0发生时Unix的处理情况:
A divide error (exception 0) occurs when an application attempts to divide by zero, or when the result of a divide instruction is too big for the destination operand. Unix does not attempt to recover from divide errors,opting instead to abort the program. Unix shells typically report divide errors as “Floating exceptions”.

也就是Unix系统不试图修复除0错误,并终止程序。
而从Windows系统的反应来看,也同样选择了出错终止。


至于为什么整形除0报错而浮点不报,说一点我的理解:
从通用概念来说,除0本来就是不允许的。即使允许,它的值也应该是无穷大,而int值采用的补码通用表示法,使得它表示的整数范围有限,无法表示无穷大这一概念;而根据IEEE浮点表示法,有正无穷和负无穷的表示,即指数全1和尾数全0,所以可以不报错,而从得到的结果正负无穷来指示结果出错。

PS:英文文献摘自: Computer Systems: From a programmer 's perspective


[解决办法]
浮点数据溢出

热点排行