Author |
Message |
boomusters
|
Posted: 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?
Python: |
final = [0.0]
term = 0.0
for i in range (len(poly)):
term = [(poly[i]*(i)),2]
if i >=1:
final.append(term)
return final
|
Mod Edit:
Pleas wrap you code in either of the following in order to preserve whitespace and to highlight the syntax.
code: |
[syntax="python"] ... code ... [/syntax]
[code] ... code ... [/code ]
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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). |
|
|
|
|
![](images/spacer.gif) |
boomusters
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Ultrahex
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
QuantumPhysics
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
boomusters
|
Posted: 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 [] |
|
|
|
|
![](images/spacer.gif) |
QuantumPhysics
|
Posted: 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% |
|
|
|
|
![](images/spacer.gif) |
boomusters
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
QuantumPhysics
|
Posted: Sun Oct 21, 2012 6:35 pm Post subject: RE:rounding error |
|
|
So you want it to round final to decimal notation? |
|
|
|
|
![](images/spacer.gif) |
boomusters
|
Posted: 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! |
|
|
|
|
![](images/spacer.gif) |
QuantumPhysics
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
boomusters
|
Posted: 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! |
|
|
|
|
![](images/spacer.gif) |
QuantumPhysics
|
Posted: Sun Oct 21, 2012 9:33 pm Post subject: RE:rounding error |
|
|
Your welcome, really sorry I couldn't help ya anymore. |
|
|
|
|
![](images/spacer.gif) |
|