Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » error: unsupported operand type(s) for *: 'Decimal' and 'float' [RSS Feed]

#1 June 11, 2010 00:06:38

Dan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


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.htmlfor 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


On Jun 10, 3:22 pm, Waleria <waleriantu...@gmail.com> wrote:
> 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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 11, 2010 00:12:59

f.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


use

float( form.cleaned_data )

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




On Jun 10, 9:22 pm, Waleria <waleriantu...@gmail.com> wrote:
> 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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 11, 2010 00:24:17

Dan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


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
dih0...@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
>
> On Jun 10, 3:22 pm, Waleria <waleriantu...@gmail.com> wrote:
>
>
>
> > 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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 11, 2010 11:58:50

W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


Dan,

Do you speak for me to do this? for the example...http://paste.pocoo.org/show/224233On 10 jun, 16:51, felix <crucialfe...@gmail.com> wrote:
> use
>
> float( form.cleaned_data )
>
> 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
>
> On Jun 10, 9:22 pm, Waleria <waleriantu...@gmail.com> wrote:
>
> > 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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 June 11, 2010 13:13:21

Dan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


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

On Jun 11, 6:58 am, Waleria <waleriantu...@gmail.com> wrote:
> Dan,
>
> Do you speak for me to do this? for the example...
>
>http://paste.pocoo.org/show/224233>
> On 10 jun, 16:51, felix <crucialfe...@gmail.com> wrote:
>
>
>
> > use
>
> > float( form.cleaned_data )
>
> > 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
>
> > On Jun 10, 9:22 pm, Waleria <waleriantu...@gmail.com> wrote:
>
> > > 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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 June 11, 2010 13:26:02

W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


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
>
> On Jun 11, 6:58 am, Waleria <waleriantu...@gmail.com> wrote:
>
> > Dan,
>
> > Do you speak for me to do this? for the example...
>
> >http://paste.pocoo.org/show/224233>
> > On 10 jun, 16:51, felix <crucialfe...@gmail.com> wrote:
>
> > > use
>
> > > float( form.cleaned_data )
>
> > > 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
>
> > > On Jun 10, 9:22 pm, Waleria <waleriantu...@gmail.com> wrote:
>
> > > > 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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » error: unsupported operand type(s) for *: 'Decimal' and 'float' [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support