General Algorithm for Primitive Calculation?

RE: I had the idea of rounding twice...
#8
(2016-08-12, 19:48)TravisÂ Cobbs Wrote: I'm not positive, but the "correct" solution might be your original round4f, with one minor change. Instead of constructing a BigDecimal with just the double value (for arbitrary precision), construct it with the double value and a MathContext.DECIMAL64 as a second argument. Based on the documentation, this should make BigDecimal use IEEE 754 rounding rules, which I would expect to match the C# rounding.

Side note: I'm pretty sure your solution can trigger wrong rounding in other cases. Say, for example, that your calculated value is 0.5036499999999911 instead of 0.5036499999999999. You're going to round up to 0.5037 in that case, while 0.5036 would be the correct result. Also, it seems that you could avoid the whole string thing by rounding the BigDecimal to 8 places, and then rounding that rounded BigDecimal to 4 places.

Thanks!
I tried a few things and finally released the PrimGen2 clone with LDPartEditor 0.8.22.
At the beginning, I thought it would be quite simple to generate some LDraw primitives...
...it turns out to be another lecture in numerics instead :)
« Next Oldest | Next Newest »

 Messages In This Thread General Algorithm for Primitive Calculation? - by Nils Schmidt - 2016-08-10, 22:53 RE: General Algorithm for Primitive Calculation? - by Travis Cobbs - 2016-08-11, 4:15 RE: General Algorithm for Primitive Calculation? - by Philippe Hurbain - 2016-08-11, 6:58 FYI: "Case study" of some numerical challenges (and the solution [not]) - by Nils Schmidt - 2016-08-11, 23:36 RE: FYI: "Case study" of some numerical challenges (and the solution [not]) - by Philippe Hurbain - 2016-08-12, 5:46 I had the idea of rounding twice... - by Nils Schmidt - 2016-08-12, 12:07 RE: I had the idea of rounding twice... - by Travis Cobbs - 2016-08-12, 19:48 RE: I had the idea of rounding twice... - by Nils Schmidt - 2016-08-20, 10:05

Forum Jump:

Users browsing this thread: 1 Guest(s)