I am trying to understand why using '/' with long double in the following way leads to a 0.000000 value while the same code with double does not
double d = (double)total_results / (double)total_points;
Gives the value 0.785403 but
long double d = (long double)total_results / (long double)total_points;
Gives the value 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 was outputting 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);
This is obviously just a guess, but how are you outputting the results? If you're using 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 should have gotten "0.75". The right specifier is "%Lf", with a capital L.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With