3

Sometimes I encounter in arithmetic operations expression like this: n*(1/k).

Such expression can be presented in simpler manner: n/k.

I could imagine that in certain situations the former could be more descriptive if (1/k) represents well known ingredient but it is not always the case. What about performance gains/losses? What about precision?

Is there any hidden reason that some developers use n*(1/k) form?

Ryszard Dżegan
  • 21,718
  • 6
  • 31
  • 53
  • There can certainly be performance gains if multiple calculations are being performed but `1/k` is common across all of them - multiplies tend to have better perf than divides. – Damien_The_Unbeliever Jun 17 '14 at 08:50
  • 1
    if n=3 and k=3, n/k gives the correct answer although n*(1/k) gives an aproximate answer. – Parag Gangil Jun 17 '14 at 08:51
  • I think it's more common in mathematics to write it like this. – George Nechifor Jun 17 '14 at 09:03
  • @ParagGangil Differences in results for both cases may indicate that the form of expression is preserved by compiler thus it has matter how we write that. Your argument is strong enough for me to avoid n*(1/k) form in favor of n/k when precison is important. – Ryszard Dżegan Jun 17 '14 at 09:38
  • @Damien_The_Unbeliever I found performance [tests in C# by Steve Wortham](http://swortham.blogspot.com/2011/10/how-much-faster-is-multiplication-than.html) in which multiplication is 41x faster than division in certain configuration but there can be also no difference in speeds. – Ryszard Dżegan Jun 17 '14 at 09:44
  • Related: http://stackoverflow.com/questions/22621241/what-does-the-constant-0-0039215689-represent – Mysticial Jun 17 '14 at 17:56
  • With the right compiler options, there's no difference since the compiler will convert `n/k` into `n*(1/k)`. But if it doesn't, you'll have to pay the cost of division being many times slower than multiplication. – Mysticial Jun 17 '14 at 17:57
  • @Mysticial n/k to n*(1/k)? Not conversely? Both contain division and second contains in addition a multiplication. They are not constants. Those are runtime evaluated variables. – Ryszard Dżegan Jun 17 '14 at 20:06
  • 1
    `k` doesn't need to be constant. It only has to be used as a divisor more than once. If you have just `a/k`, the compiler will keep it that way. If you have `a/k` and `b/k`, then it will be faster to compute `r = 1/k`. Then `a*r` and `b*r`. This optimization can be done either manually or by the compiler (if you allow it to do unsafe FP optimizations). Even if you write it as `a*(1/k)` and `b*(1/k)`, the compiler will be smart enough to do the `1/k` only once. – Mysticial Jun 17 '14 at 20:40
  • If people are writing `n*(1/k)` indiscriminately in all cases even when it is not beneficial, then it's probably just out of habit. – Mysticial Jun 17 '14 at 20:42
  • http://stackoverflow.com/questions/1528727/why-is-sse-scalar-sqrtx-slower-than-rsqrtx-x – phuclv Apr 14 '15 at 10:35
  • for integers then multiplying by its multiplative inverse (with some adjustments if needed) will return the correct division result so all modern compilers will optimize that out – phuclv Apr 14 '15 at 10:39
  • Read also: [Is multiplication faster than float division?](http://stackoverflow.com/questions/17883240/is-multiplication-faster-than-float-division) [Should I use multiplication or division?](http://stackoverflow.com/questions/226465/should-i-use-multiplication-or-division) – phuclv Apr 14 '15 at 11:03

1 Answers1

0

To me i find that using n*(1/k) will be having a lesser accurate answer because of the reason that when the control solves an produces the result of (1/k) the will be situations that may cause rounding off or trimming of the result that may lead to the loss of accuracy.And during the multiplication process the magnitude of the loss increases.Hence as far as i am concerned i will say n/k is better

subas chandran
  • 13
  • 1
  • 1
  • 5
  • Sometimes speed is preferred over precision. Read the links on the comments. `x*rsqrt(x)` will be much faster than `sqrt(x)` – phuclv Apr 14 '15 at 10:32
  • I told my views about the precision .I know that it answers only the half of the question.I am dont know about the performance issues. Its better to leave simply than giving wrong directions. – subas chandran Apr 14 '15 at 10:52
  • It still doesn't answer the question. The OP asked a reason for using it, not why you shouldn't use it – phuclv Apr 14 '15 at 11:12
  • read the question dude.....he asks something like **What about precision?** i answered just that.I hope someone will soon give the complete answer. – subas chandran Apr 14 '15 at 11:35
  • read the question dude. He asked *"Is there a reason to use arithmetic expression n*(1/k) over n/k?"* The other questions are for the reason to use that expression – phuclv Apr 14 '15 at 11:40