c# - 'Beautify' number by rounding erroneous digits appropriately -
i want cake , eat it. want beautify (round) numbers largest extent possible without compromising accuracy other calculations. i'm using doubles in c# (with string conversion manipulation too).
here's issue. understand inherent limitations in double number representation (so please don't explain that). however, want round number in way appear aesthetically pleasing end user (i making calculator). problem rounding x significant digits works in 1 case, not in other, whilst rounding decimal place works in other, not first case.
observe:
case a: math.sin(math.pi) = 0.000000000000000122460635382238
case b: 0.000000000000001/3 = 0.000000000000000333333333333333
for first case, want round decimal places. give me nice neat 0 i'm looking for. rounding sig digits mean keep erroneous digits too.
however second case, want round significant digits, lose tons of accuracy if rounded merely decimal places.
is there general way can cater both types of calculation?
i don't thinks it's feasible result , precision has nothing it.
consider input: (1+3)/2^3 . can "beautify" showing result sin(30) or cos(60) or 1/2 , whole lot of other interpretations. choosing wrong "beautification" can mislead user, making them think function has sin(x).
if calculator keeps initial input variables keep operations postponed until need result , make sure simplify result until matches needs. , you'll need consider using rational numbers, e, pi , other irrational numbers may not easy deal with.
Comments
Post a Comment