Home > Back-end >  Small white help c + +
Small white help c + +

Time:10-06

Why can appear such different

CodePudding user response:

Floating point types of precision problem

CodePudding user response:

reference 1st floor qybao response:
floating point types of precision problem

Can you explain it a little explanation

CodePudding user response:

Nothing to explain
Their print memory to see
 int main (int arg c, const char * argv []) {
Cout<" The first kind of (float) (2.2 1.2) to 1.0="& lt; <(float) (2.2 1.2) to 1.0 & lt; Cout<" The second: (float) (2.2-1.2-1.0)="& lt; <(float), (2.2-1.2-1.0) & lt;
Int sf=sizeof (int) (float).
Int sd=sizeof (int) (double);
Printf (" sizeof (float)=% d \ n ", sf);
Printf (" sizeof (double)=% d \ n ", sd);

Double d1=float (2.2 1.2) to 1.0;
Double d2=2.2-1.2-1.0;
Float=f1 float (2.2 1.2) to 1.0;
Float f2=float (2.2-1.2-1.0).

Char * p=(char *) & amp; D1;
int i;
Printf (" d1=");
for (i=0; iPrintf (" % 02 x, "(* (p + I) & amp; 0 x00ff));
}
printf("\n");

P=(char *) & amp; D2.
Printf (" d2=");
for (i=0; iPrintf (" % 02 x, "(* (p + I) & amp; 0 x00ff));
}
printf("\n");


P=(char *) & amp; F1;
Printf (" f1=");
for (i=0; iPrintf (" % 02 x, "(* (p + I) & amp; 0 x00ff));
}
printf("\n");//f1 truncation d1 low four are all 0

P=(char *) & amp; F2;
Printf (" f2=");
for (i=0; iPrintf (" % 02 x, "(* (p + I) & amp; 0 x00ff));
}
printf("\n");//f2 truncation d2 low four is not zero, according to the floating point format (float format) output//memory format you want to view the floating-point


return 0;
}
Prev:C cycle
Next:Excel c
  • Related