
-----------------------------------
boomusters
Sat Oct 20, 2012 12:57 pm

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?


    final = 


Mod Edit:

Pleas wrap you code in either of the following in order to preserve whitespace and to highlight the syntax.
 ... code ... 

[code] ... code ... [/code ]
[/code]

-----------------------------------
Insectoid
Sat Oct 20, 2012 5:50 pm

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).

-----------------------------------
boomusters
Sun Oct 21, 2012 12:54 pm

Re: 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).

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

-----------------------------------
Ultrahex
Sun Oct 21, 2012 1:25 pm

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.

-----------------------------------
QuantumPhysics
Sun Oct 21, 2012 3:33 pm

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

-----------------------------------
boomusters
Sun Oct 21, 2012 6:26 pm

Re: 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 

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 []

-----------------------------------
QuantumPhysics
Sun Oct 21, 2012 6:30 pm

RE:rounding error
-----------------------------------
Please post your whole code and I'll make sure to help you out 101%

-----------------------------------
boomusters
Sun Oct 21, 2012 6:33 pm

Re: RE:rounding error
-----------------------------------
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

-----------------------------------
QuantumPhysics
Sun Oct 21, 2012 6:35 pm

RE:rounding error
-----------------------------------
So you want it to round final to decimal notation?

-----------------------------------
boomusters
Sun Oct 21, 2012 6:37 pm

Re: RE:rounding error
-----------------------------------
So you want it to round final to decimal notation?

correct! and thanks for helping!

-----------------------------------
QuantumPhysics
Sun Oct 21, 2012 7:14 pm

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

-----------------------------------
boomusters
Sun Oct 21, 2012 7:39 pm

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

ok well thank you anyway!

-----------------------------------
QuantumPhysics
Sun Oct 21, 2012 9:33 pm

RE:rounding error
-----------------------------------
Your welcome, really sorry I couldn't help ya anymore.
