Computer Science Canada rounding error |
Author: | boomusters [ Sat Oct 20, 2012 12:57 pm ] | ||||
Post subject: | rounding error | ||||
hey i have a rounding error somewhere in there, im supposed to get a list of numbers and output the derivitive, can someone tell me my error?
Mod Edit: Pleas wrap you code in either of the following in order to preserve whitespace and to highlight the syntax.
|
Author: | Insectoid [ Sat Oct 20, 2012 5:50 pm ] |
Post subject: | RE:rounding error |
There will always be risk of rounding errors whenever you use real numbers. I suggest you use integers for everything and only convert to real after all calculations have been done (ideally during/just prior to output). |
Author: | boomusters [ Sun Oct 21, 2012 12:54 pm ] |
Post subject: | Re: RE:rounding error |
Insectoid @ Sat Oct 20, 2012 5:50 pm wrote: There will always be risk of rounding errors whenever you use real numbers. I suggest you use integers for everything and only convert to real after all calculations have been done (ideally during/just prior to output).
i would prefer to but i need exact answers for the calculations, for example if it rounded to 14.002 and the answer was 14.000099 my answer would be wrong |
Author: | Ultrahex [ Sun Oct 21, 2012 1:25 pm ] |
Post subject: | Re: rounding error |
you either need to use a numerically stable solution; the easiest solution is to not use floating point numbers. If the given input is a list of integers, then you can get away without ever using floating point in this solution. |
Author: | QuantumPhysics [ Sun Oct 21, 2012 3:33 pm ] |
Post subject: | RE:rounding error |
Yea, I can tell you your error. First of all, where is poly declared? -> possible source of error Second of all, you must make final an open ended list if you plan to add multiple values into it. Just like an array leave the [ ] space blank so you can add an in definitive+ amount of values into it. Not 0.0 |
Author: | boomusters [ Sun Oct 21, 2012 6:26 pm ] |
Post subject: | Re: RE:rounding error |
QuantumPhysics @ Sun Oct 21, 2012 3:33 pm wrote: Yea, I can tell you your error. First of all, where is poly declared? -> possible source of error
Second of all, you must make final an open ended list if you plan to add multiple values into it. Just like an array leave the [ ] space blank so you can add an in definitive+ amount of values into it. Not 0.0 poly is the temperary variable in a function def derivities(poly) it works but if only 1 number is inputed like "[7]" i need it to output 0.0 and it outputs [] |
Author: | QuantumPhysics [ Sun Oct 21, 2012 6:30 pm ] |
Post subject: | RE:rounding error |
Please post your whole code and I'll make sure to help you out 101% |
Author: | boomusters [ Sun Oct 21, 2012 6:33 pm ] |
Post subject: | Re: RE:rounding error |
QuantumPhysics @ Sun Oct 21, 2012 6:30 pm wrote: Please post your whole code and I'll make sure to help you out 101%
def computeDeriv(poly): final = [] term = 0.0 for i in range (len(poly)): term = (poly[i]*(i)) if i >=1: final.append(term) return final |
Author: | QuantumPhysics [ Sun Oct 21, 2012 6:35 pm ] |
Post subject: | RE:rounding error |
So you want it to round final to decimal notation? |
Author: | boomusters [ Sun Oct 21, 2012 6:37 pm ] |
Post subject: | Re: RE:rounding error |
QuantumPhysics @ Sun Oct 21, 2012 6:35 pm wrote: So you want it to round final to decimal notation?
correct! and thanks for helping! |
Author: | QuantumPhysics [ Sun Oct 21, 2012 7:14 pm ] |
Post subject: | Re: rounding error |
To convert a value to decimal places you can type return '%.3f' % x % is the iterator .3f = 3 decimal places change to any value for number of decimal places % = value sign x = value you want to convert to 3 decimal places you can also do: # external library from decimal import * # 2 = number of decimal places getcontext(variable).prec = 2 return variable Either than that I hardly know of any ways to convert to decimal places with python. I cannot test this for you, sorry. I am not in my python environment atm |
Author: | boomusters [ Sun Oct 21, 2012 7:39 pm ] |
Post subject: | Re: rounding error |
QuantumPhysics @ Sun Oct 21, 2012 7:14 pm wrote: To convert a value to decimal places you can type return '%.3f' % x
% is the iterator .3f = 3 decimal places change to any value for number of decimal places % = value sign x = value you want to convert to 3 decimal places you can also do: # external library from decimal import * # 2 = number of decimal places getcontext(variable).prec = 2 return variable Either than that I hardly know of any ways to convert to decimal places with python. I cannot test this for you, sorry. I am not in my python environment atm ok well thank you anyway! |
Author: | QuantumPhysics [ Sun Oct 21, 2012 9:33 pm ] |
Post subject: | RE:rounding error |
Your welcome, really sorry I couldn't help ya anymore. |