The reason for the special ordering of division and multiplication in C ++

I am in the middle of porting some C ++ code to java, and I continue to work in cases where no one writes, he continued to do the following:

double c = (1.0/(a+1.0)*pow(b, a+1.0));
double d = (1./(integral(gamma, dmax)-integral(gamma, dmin)))*(integral(gamma+1, dmax)-integral(gamma+1, dmin));

Instead:

double c = pow(b, a+1.0)/(a+1.0);
double d = (integral(gamma+1, dmax)-integral(gamma+1, dmin))/(integral(gamma, dmax)-integral(gamma, dmin));

The second one looks much clearer, and if I'm not mistaken about the order of operations in C ++, they should do the same. Is there any reason to do the first, not the second? The only thing I could think of would be a strange case with precision.

+3
source share
3 answers

Yes, they are the same. The only reason I can think of is mathematical clarity: sometimes, when you normalize the quantity, you often write:

answer = (1/total) * (some of it)

For example, the Cauchy integral theorem is often written

f(a) = (1/(2*pi*i)) * integral(f(z)/(z-a), dz)
+6

a b (, , a , , const), , (, 1/... ).

, , , .

, , , -, 1/.... , : ( ).

+2

Yes, they must do the same. Perhaps the original author replicated the equations found, where

+1
source

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


All Articles