Advertisement
AyrA

How float works in C

May 2nd, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. const char* Labels[]={"Inf","NaN","12 "};
  4.  
  5. #define SIZE   6
  6. #define NEG12  5
  7. #define POS12  4
  8. #define NEGNAN 3
  9. #define POSNAN 2
  10. #define NEGINF 1
  11. #define POSINF 0
  12.  
  13.  
  14. void print(const char*,const float*,const int*);
  15.  
  16. /*Testing what invalid numbers look in float*/
  17. int main(int argc,char* argv[])
  18. {
  19.     /*we want to store 6 float values (infinity, -Infinity, NaN, -NaN, 12, -12)*/
  20.     float f[SIZE];
  21.     /*
  22.     This is a dirty trick, we create an integer array,
  23.     that is in the same location as the float array.
  24.     This allows us to look at the floats as integers.
  25.     Floats and integers are both 32bit, so they occupy
  26.     the same number of bytes in memory (4)
  27.     */
  28.     int* i=(int*)&f;
  29.  
  30.     /*Positive Infinity*/
  31.     f[POSINF]=1.0/0.0;
  32.     /*Negative Infinity*/
  33.     f[NEGINF]=-1.0/0.0;
  34.     /*NaN from positive Infinity*/
  35.     f[POSNAN]=f[POSINF]-f[POSINF];
  36.     /*NaN from negative Infinity*/
  37.     f[NEGNAN]=f[NEGINF]-f[NEGINF];
  38.     /*Regular positive number*/
  39.     f[POS12]=12.0;
  40.     /*Regular negative number*/
  41.     f[NEG12]=-12.0;
  42.    
  43.     print("Values 'as-is'",f,i);
  44.  
  45.     /*test what happens, if we copy NaN over the positive infinity, but use integers*/
  46.     i[POSINF]=i[POSNAN];
  47.     print("Copy +NaN (int) to +Inf (int)",f,i);
  48.  
  49.     /*Assigning the NaN integer to a float*/
  50.     f[NEGINF]=(float)i[POSNAN];
  51.     print("Assigning +NaN (int) value to -Inf (float), but cast it correctly",f,i);
  52.  
  53.     /*Assigning -12 directly to a float via integer*/
  54.     i[NEG12]=-12;
  55.     print("Assigning -12 (int) to -12 (float) by setting the int value",f,i);
  56.  
  57.     /*END*/
  58.     printf("\r\nPress return to exit\r\n");
  59.     getchar();
  60.     return 0;
  61.     /*
  62.     Conclusion:
  63.     - Inifinity and NaN are just specific numbers.
  64.     - Infinity exists as positive and negative
  65.     - NaN exists only as a negative number
  66.     - Floats have a protection against accidental assignment of said invalid value (crash proof)
  67.     - Floats store numbers completely differently from integers
  68.     */
  69. }
  70.  
  71. /*prints table of numbers*/
  72. void print(const char* description,const float* f,const int* i)
  73. {
  74.     int q=0;
  75.     printf("\r\n%s\r\n\r\nFLOAT---------------- INT------------------\r\n",description);
  76.     for(q=0;q<SIZE;q++)
  77.     {
  78.         char c=q%2==0?'+':'-';
  79.         printf("%c%s: %15f %c%s: %15i\r\n",
  80.         c,Labels[q/2],
  81.         f[q],
  82.         c,Labels[q/2],
  83.         i[q]);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement