Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 rounding error
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
boomusters




PostPosted: 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 ]
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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).
boomusters




PostPosted: 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
Ultrahex




PostPosted: 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.
QuantumPhysics




PostPosted: 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
boomusters




PostPosted: 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 []
QuantumPhysics




PostPosted: 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%
boomusters




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
QuantumPhysics




PostPosted: Sun Oct 21, 2012 6:35 pm   Post subject: RE:rounding error

So you want it to round final to decimal notation?
boomusters




PostPosted: 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!
QuantumPhysics




PostPosted: 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
boomusters




PostPosted: 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!
QuantumPhysics




PostPosted: Sun Oct 21, 2012 9:33 pm   Post subject: RE:rounding error

Your welcome, really sorry I couldn't help ya anymore.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: