c - Converting single-precision floating point numbers to double-precision for division -


working high-performance-computing guy, tend default single-precision floating point numbers (float or real) whenever possible. because can perform more operations per second if each operation individually faster perform.

one of more senior people work with, however, insists (when accuracy required) should temporarily convert single-precision data double-precision in order perform division. is:

float a, b; float ans = ((double)a)/((double)b); 

or

real :: a, b, ans ans = real(dble(a)/dble(b)) 

depending on language you're working in. in opinion, looks ugly, , honest don't know if answer held in ans more accurate if had written ans = a/b in single-point precision.

can tell me whether converting numbers prior arithmetic, specifically performing division, result in more accurate answer? language/compiler specific question, or ieee? number values accuracy improvement noticeable?

any enlightening comments/answers appreciated.

float ans = ((double)a)/((double)b);

this article demonstrates ans same computed single-precision division ieee 754 arithmetics , flt_eval_method=0.

when flt_eval_method=1, same property trivially true.

when flt_eval_method=2, not sure. possible 1 might interpret rules meaning long double computation of a/b must first rounded double, float. in case, can less accurate directly rounding long double float (the latter produces correctly rounded results, whereas former fail in extremely rare cases, unless theorem figueroa's applies , shows never happens).

long story short, modern, reasonable floating-point computing platform (*), superstition float ans = ((double)a)/((double)b); has benefits. should ask senior people refer in question exhibit 1 pair a, b of values result different, not mention more accurate. surely if insist better should no trouble them provide 1 single pair of values makes difference.

(*) remember use -fexcess-precision=standard gcc preserve sanity


Comments