error: unsupported operand type(s) for *: 'Decimal' and 'float'

11,019 views
Skip to first unread message

Waleria

unread,
Jun 10, 2010, 3:22:20 PM6/10/10
to Django users
Hello..

I again.....now my problem is:

I have the function following: http://paste.pocoo.org/show/224041/

These values i get of a form...however returns me an error....fallows
below the error

Exception Type: TypeError
Exception Value: unsupported operand type(s) for *: 'Decimal' and
'float'

Exception Location: C:\simuladores\..\simuladores\detector\views.py in
graph, line 32

Dan Harris

unread,
Jun 10, 2010, 3:33:35 PM6/10/10
to Django users
The error looks like you are attempting to multiply a decimal by a
float which isn't allowed. Django coerces form fields like
DecimalField into a decimal object. See: http://docs.python.org/library/decimal.html
for more information about the decimal object.

An error occurs like the one you have if you do something like:

>>> from decimal import Decimal
>>> import math
>>> d = Decimal("2")
>>> d*math.pi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'Decimal' and 'float'

You can convert the decimal returned by the form into a float, or
convert the floats into decimals and do your math. Converting to
decimal is probably more precise.

Hope this helps,

Dan Harris
dih...@gmail.com

Dan Harris

unread,
Jun 10, 2010, 3:50:18 PM6/10/10
to Django users
To be more specific it looks like this:

((pow(2*math.pi*v,2)))*((pow(2,2))

piece of your very large single line of calculation is the culprit.
You should be able to convert to a decimal like so:

{previous_stuff_here} * Decimal(str(((pow(2*math.pi*v,
2)))*((pow(2,2)))) * {more_stuff_here }

As a readability thing, you might want to split that giant line of
code into a few separate calculations, but that's just an idea!

Cheers,

Dan Harris
dih...@gmail.com

On Jun 10, 3:33 pm, Dan Harris <dih0...@gmail.com> wrote:
> The error looks like you are attempting to multiply a decimal by a
> float which isn't allowed. Django coerces form fields like
> DecimalField into a decimal object. See:http://docs.python.org/library/decimal.html
> for more information about the decimal object.
>
> An error occurs like the one you have if you do something like:
>
> >>> from decimal import Decimal
> >>> import math
> >>> d = Decimal("2")
> >>> d*math.pi
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: unsupported operand type(s) for *: 'Decimal' and 'float'
>
> You can convert the decimal returned by the form into a float, or
> convert the floats into decimals and do your math. Converting to
> decimal is probably more precise.
>
> Hope this helps,
>
> Dan Harris
> dih0...@gmail.com

felix

unread,
Jun 10, 2010, 3:51:31 PM6/10/10
to Django users

use

float( form.cleaned_data['a'] )

etc

also, when retreiving the field from the db it will be a Decimal
object and needs to be converted to do math with it

on_sale = float(myObj.price) * 0.9

Waleria

unread,
Jun 11, 2010, 6:58:43 AM6/11/10
to Django users
Dan,

Do you speak for me to do this? for the example...

http://paste.pocoo.org/show/224233

Dan Harris

unread,
Jun 11, 2010, 8:13:14 AM6/11/10
to Django users
That looks ok from a code readability point of view, however you will
still need to cast the form values to float or cast the float values
(i.e. pow(2,2) and pow(2*math.pi*v,2) to Decimals otherwise will you
will get an UnsupportedOperation exception.

Dan Harris
dih...@gmail.com

Waleria

unread,
Jun 11, 2010, 8:25:42 AM6/11/10
to Django users
Ok....i converted this (i.e. pow(2,2) and pow(2*math.pi*v,2) to
Decimals....my graph was plotted correctly.....however...i haven't
split that giant line of code a few separte calculations......do you
think best to do this?



On 11 jun, 09:13, Dan Harris <dih0...@gmail.com> wrote:
> That looks ok from a code readability point of view, however you will
> still need to cast the form values to float or cast the float values
> (i.e. pow(2,2) and pow(2*math.pi*v,2) to Decimals otherwise will you
> will get an UnsupportedOperation exception.
>
> Dan Harris
> dih0...@gmail.com
Reply all
Reply to author
Forward
0 new messages