Using '/' with long double?

I am trying to understand why using '/' with a long double as follows leads to a value of 0.000000, whereas the same code with double is not

double d = (double)total_results / (double)total_points; 

Gives a value of 0.785403, but

 long double d = (long double)total_results / (long double)total_points; 

Gives the value of 0.000000. I am trying to get the most accurate value for "total_results / total_points"

EDIT: In the end, the error was simply that I output it using "% f" instead of "% Lf"

Before

 printf("Running on %d thread(s), results is %f.\n", NUM_THREADS, d); 

After

 printf("Running on %d thread(s), results is %Lf.\n", NUM_THREADS, d); 
+6
source share
1 answer

This is obviously just an assumption, but how do you derive the results? If you use printf with the wrong field specifier, printing an erroneous zero is definitely a possible result. Using g ++, I tried "% lf" and got "-2.0000" when I was supposed to get "0.75". The correct qualifier is "% Lf", with capital L.

+15
source

Source: https://habr.com/ru/post/907771/


All Articles