-5

Floating point is weird in C language. I know it is generally used as (float)a/b. However, I am curious what is the main cause and reason for the code phenomenon below.

#include <stdio.h>

int main(void)
{
  int a=30, b=16;
  double divresult;
  divresult = a/b;  
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("divresult : %f \n",divresult);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  return 0;
}

output here

result1 : 0.000000 
result1 : 0.000000 
result1 : 0.000000 
result1 : 0.000000 
result1 : 0.000000 
result1 : 0.000000 
result1 : 0.000000 
result1 : 0.000000 
result1 : 0.000000 
divresult : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 

Why is this happening? I can't really understand. In general, we know that computers cannot accurately represent floating point numbers. But in this case, there seems to be some pattern. How does the change of values before and after "divresult printf" appear?

amayoon
  • 3
  • 2

1 Answers1

1

Printf is supposed to print double but you pass integer. It is undefined behaviour. In this case it expresses itself by printing "remains" from the previous parameter passed. In this modified example it is clearly visible. But of course it is an UB and the behaviour of the program can be completely different. Also remember that the result of the integer division is also an integer. 30/16 == 1 in C and C++

(double)(a/b) is 1.0 as it converts the result of integer division to double

(double)a/b converts a to double and then does the double division.

#include <stdio.h>

int main(void)
{
  int a=30, b=16;
  double divresult;
  divresult = a/b;  
  printf("result1 : %f \n", (double)(a/b));
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", (double)a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("result1 : %f \n", a/b);
  printf("divresult : %f \n",divresult);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", 0.0);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", (double)a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", (double)(a/b));
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", 0.0);
  printf("result2 : %f \n", a/b);
  printf("result2 : %f \n", a/b);
  return 0;
}

Result

x86-64 gcc 10.2

Program returned: 0
Program stdout

result1 : 1.000000 
result1 : 1.000000 
result1 : 1.000000 
result1 : 1.875000 
result1 : 1.875000 
result1 : 1.875000 
result1 : 1.875000 
result1 : 1.875000 
result1 : 1.875000 
divresult : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 0.000000 
result2 : 0.000000 
result2 : 0.000000 
result2 : 1.875000 
result2 : 1.875000 
result2 : 1.875000 
result2 : 1.875000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 1.000000 
result2 : 0.000000 
result2 : 0.000000 
result2 : 0.000000 
0___________
  • 60,014
  • 4
  • 34
  • 74