Skip to main content

Timeline for Gravity model for a simulator

Current License: CC BY-SA 3.0

16 events
when toggle format what by license comment
May 1, 2016 at 7:25 comment added Emily L. Addendum, don't take my word for it: Always benchmark! :)
May 1, 2016 at 7:25 comment added Emily L. For old cpus multiplying by the reciprocal was faster but modern cpus have fast division and are super scalar, they can execute many instructions in one cycle. So this old adage is not necessarily true anymore. Doing: dx/=r3; dy/=r3 is two ops that can be done in parallel by the cpu as soon as the result of r3 is available. If you OTOH use the reciprocal, the cpu has to wait until the that completes and then do two multiplications (probably in parallel). Note that if the two naive divs were in parallel the naive way would be done when you started the mul op due to the data dependency.
May 2, 2015 at 1:11 comment added anon @JS1 Ah, that makes sense
May 2, 2015 at 0:36 comment added JS1 @QPaysTaxes I was just copying OP's style. Personally, I use // almost everywhere.
May 2, 2015 at 0:29 comment added Kyle I'd have to disagree that Math.pow is clearer. I actually share this pet peeve with JS1. I prefer repeated multiplication for small powers (I'd say less than about 4). Combined with the appropriate variable name, I'd say the repeated multiplication is perfectly clear in intent. I dislike premature optimization as much as the next guy, but the repeated multiplication is almost surely faster and is used somewhere that will likely be a performance bottleneck. Since I'd argue there's no loss in clarity (or developer time), I'd go for multiplication.
May 1, 2015 at 22:23 comment added anon A pet peeve of mine is when people use /* */ when they're making a single-line comment. Out of curiosity, is there a particular reason you do that, or is it just purely subjective?
May 1, 2015 at 21:00 history edited JS1 CC BY-SA 3.0
Added better comment
May 1, 2015 at 20:36 comment added JS1 I wonder my aversion to Math.pow() is related to the fact that I use C as my main language. In C, I would never call pow() to take something to the 2nd or 3rd power. Reasons: 1) It's slower. 2) You need to include math.h and link libm.a. But if you guys insist that Math.pow() is better in Java, I believe you. But I still wouldn't use it myself :). Does the java runtime optimize Math.pow(2) and (3) to do it the easy way?
May 1, 2015 at 20:09 comment added user34073 I would definitely use Math.pow(r, 3) instead of r * r * r.
May 1, 2015 at 19:47 comment added Caridorc Matt.pow makes intent clearer.
May 1, 2015 at 19:19 history edited JS1 CC BY-SA 3.0
added 152 characters in body
May 1, 2015 at 19:14 history edited JS1 CC BY-SA 3.0
added 152 characters in body
May 1, 2015 at 19:11 comment added JS1 @RK1 I think it's r^3 because you divide by r^2 to get the force, and then you multiply by (dx/r) to get the direction, thus introducing another r to the denominator. Also look at my edit. I now compute 1.0/(r*r*r) to reduce the number of divisions.
May 1, 2015 at 18:56 comment added RK1 The amount of things I learned and noticed thanks to your answer is incredible. Just a quick fix, its r^2, not r^3. Thank you!
May 1, 2015 at 18:53 vote accept RK1
May 1, 2015 at 18:26 history answered JS1 CC BY-SA 3.0