Register Guidelines E-Books Search Today's Posts Mark Forums Read

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

Notices

Reply
 
Thread Tools Search this Thread
Old 11-01-2019, 05: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, 10: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: 18,400
Karma: 30695818
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, 11: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: 24,606
Karma: 34133341
Join Date: Aug 2009
Location: The Central Coast of California
Device: K4NT(RIP), Galaxy Tab A, Kobo Aura2
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, 05: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: 6,793
Karma: 32159627
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Aura One, Aura H2O, Aura HD, Nexus 7 HD, iPad Air, 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 05:44 PM. Reason: Added comment about print statements
DNSB is offline   Reply With Quote
Old 11-01-2019, 05: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, 06: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, 10: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: 24,606
Karma: 34133341
Join Date: Aug 2009
Location: The Central Coast of California
Device: K4NT(RIP), Galaxy Tab A, Kobo Aura2
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, 10: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

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:26 AM
Error - division by zero fanton44 Calibre 2 10-27-2015 11:59 AM
Custom Column with division Metaxil Library Management 5 12-15-2014 07:31 PM
ePub division in chapters TheWatt ePub 3 04-04-2011 05:02 PM
Sony Reader division not going anywhere. boylec News 18 07-26-2009 10:44 AM


All times are GMT -4. The time now is 06:18 PM.


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