Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using '/' with long double?

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); 
like image 433
Shane Avatar asked Feb 06 '12 14:02

Shane


1 Answers

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.

like image 103
Ernest Friedman-Hill Avatar answered Sep 29 '22 09:09

Ernest Friedman-Hill