Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

Exception for 32 bit number C++

Why do I get exception

Unhandled exception at 0x00000001 in TestingCOA.exe: 0xC0000005: Access violation (parameters: 0x00000008).

when I try to work with a 4294967295 or higher number. On my machine sizeof double is 8bytes which should be able to handle and work with2^64 -1 number but it is generating exception for a 32 bit number, why is that?

int main()
{
  double n,remainderA;
  int AfterDecimal1[64],RemExponent1;

  cout<< "Enter number
";
  cin>> n;

  remainderA=a-(int)a;

   HandleFractionNumber(remainderA,AfterDecimal1,RemExponent1);
}


int HandleFractionNumber(double remainder,int (&Goku)[64],int &RemExponent)
{
int x=0;
for(int i=0;;i++)
{
    remainder*=2;
    if(remainder>1)
    {
        remainder-=1;
        Goku[x]=1;
        x++;
    }
    else
        if(remainder<1)
        {
            Goku[x]=0;
            x++;
        }
        if(remainder==1)
        {
            Goku[x]=1;
            break;
        }
        RemExponent=x;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Double does not work like that. It is a structure with fields and certain format (known as IEEE double precision). Not all 64 bits are available for the mantissa.

Yet, it should have eaten the number you mentioned (4294967295) at input. Are you sure this is what you put? Is the program you quoted - all there is?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...