Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 11-01-2019, 04:14 AM   #1
kylecarroll
Junior Member
kylecarroll began at the beginning.
 
Posts: 9
Karma: 10
Join Date: Oct 2019
Device: Kindle
Division fails when using an int variable

I'm trying to get an average rating in my plugin and I keep getting a 'divide by zero' error when I do this:

Code:
 top = int(sum(rateList))
            bottom = int(len(rateList))
            averageRating = top/bottom
However, if I change the "/" to any other mathematical symbol, it works as expected. Additionally, if I remove the variables and use plain int values, it works fine. Is this a bug, or am I doing something wrong?
kylecarroll is offline   Reply With Quote
Old 11-01-2019, 09:32 AM   #2
davidfor
Grand Sorcerer
davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.
 
Posts: 24,907
Karma: 47303748
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
What is "ratelist"? I assume it is supposed to be a list of numbers. But, with that result, I would assume it is an empty list. Have you checked that it has the values in it you expect?
davidfor is offline   Reply With Quote
Advert
Old 11-01-2019, 10:30 AM   #3
theducks
Well trained by Cats
theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.
 
theducks's Avatar
 
Posts: 29,809
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
When you make an Int from a small fraction, you could end up with a zero .
You really need to test for that before attempting division
theducks is offline   Reply With Quote
Old 11-01-2019, 04:40 PM   #4
DNSB
Bibliophagist
DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.
 
DNSB's Avatar
 
Posts: 35,461
Karma: 145525534
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Forma, Clara HD, Lenovo M8 FHD, Paperwhite 4, Tolino epos
Quote:
Originally Posted by theducks View Post
When you make an Int from a small fraction, you could end up with a zero .
You really need to test for that before attempting division
In this case, it would seem ratelist would have to have no members for the len to be set to 0. I am also wondering why the conversion to int is being done -- the len() function should be returning an int.

Perhaps a look at how ratelist is being generated and what is being returned? A few print statements to see what is actually being generated?

Last edited by DNSB; 11-01-2019 at 04:44 PM. Reason: Added comment about print statements
DNSB is offline   Reply With Quote
Old 11-01-2019, 04:54 PM   #5
kylecarroll
Junior Member
kylecarroll began at the beginning.
 
Posts: 9
Karma: 10
Join Date: Oct 2019
Device: Kindle
I've tested by printing each statement len(rateList) and sum(rateList) and both give the expected values.

Additionally, I have them casted to int in that example just because that is what I had currently tried doing to ensure it was indeed an int. I've done it without the casting as well to no avail.
kylecarroll is offline   Reply With Quote
Advert
Old 11-01-2019, 05:02 PM   #6
kylecarroll
Junior Member
kylecarroll began at the beginning.
 
Posts: 9
Karma: 10
Join Date: Oct 2019
Device: Kindle
I figured it out! The division was nested inside a for loop which was generating the rate list. So, on the first iteration, that list WAS empty. I moved it outside of the loop and worked as expected. Thanks for getting my brain turning.
kylecarroll is offline   Reply With Quote
Old 11-01-2019, 09:14 PM   #7
theducks
Well trained by Cats
theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.
 
theducks's Avatar
 
Posts: 29,809
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
Quote:
Originally Posted by kylecarroll View Post
I figured it out! The division was nested inside a for loop which was generating the rate list. So, on the first iteration, that list WAS empty. I moved it outside of the loop and worked as expected. Thanks for getting my brain turning.
Hopefully our discussion helped.
Come to think of it, I chased one many decades ago, where I forgot to initialize the (self typing) variable. First pass, it would bomb with a 'wrong type' error
theducks is offline   Reply With Quote
Old 11-01-2019, 09:36 PM   #8
kylecarroll
Junior Member
kylecarroll began at the beginning.
 
Posts: 9
Karma: 10
Join Date: Oct 2019
Device: Kindle
The discussion definitely helped. Sometimes all it takes is bouncing your thoughts off of others to find your goofs!
kylecarroll is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to name institutions as authors (i.e. NO name division') Pachuqismo Library Management 9 11-28-2017 04:26 AM
Error - division by zero fanton44 Calibre 2 10-27-2015 10:59 AM
Custom Column with division Metaxil Library Management 5 12-15-2014 06:31 PM
ePub division in chapters TheWatt ePub 3 04-04-2011 04:02 PM
Sony Reader division not going anywhere. boylec News 18 07-26-2009 09:44 AM


All times are GMT -4. The time now is 01:01 AM.


MobileRead.com is a privately owned, operated and funded community.