Going CRAZY from division
I have two integers "clicks" and "views" and am trying to find what the ratio of clicks to views. This should be (and is on my calculator) clicks / views * 100 (to get the percent) 3 / 356 * 100 = .842 % <dtml-var expr="clicks/views"> Returns = 0 <dtml-var expr="clicks/views" fmt="%.6f"> Returns 0.00000 I don't know what is wrong. Anyone? BZ
On Mon, Nov 18, 2002 at 05:17:18PM -0500, BZ wrote:
I have two integers "clicks" and "views" and am trying to find what the ratio of clicks to views. This should be (and is on my calculator)
clicks / views * 100 (to get the percent) 3 / 356 * 100 = .842 %
<dtml-var expr="clicks/views"> Returns = 0
<dtml-var expr="clicks/views" fmt="%.6f"> Returns 0.00000
I don't know what is wrong. Anyone?
Welcome to Python (and generally, most programming languages) integer calculations where 1 / 2 is 0. Integer division will yield integer results. If one of the operands is float however, it'll give a float result. In other words, 1 / 2.0 is 0.5 You'll have to convert at least one of the two operands to a float for the above to yield a float: <dtml-var expr="clicks/float(views)"> -- Martijn Pieters | Software Engineer mailto:mj@zope.com | Zope Corporation http://www.zope.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
Integer division in Python is "floor division" with respect to the operands both being ints (dividing two ints results in an int). Python does "true division" when either operand is a float. You should change one or the other or both of "clicks" or "views" to a float to get a float back. HTH, - C On Mon, 2002-11-18 at 17:17, BZ wrote:
I have two integers "clicks" and "views" and am trying to find what the ratio of clicks to views. This should be (and is on my calculator)
clicks / views * 100 (to get the percent) 3 / 356 * 100 = .842 %
<dtml-var expr="clicks/views"> Returns = 0
<dtml-var expr="clicks/views" fmt="%.6f"> Returns 0.00000
I don't know what is wrong. Anyone?
BZ
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Probably python's pathological division paradox. Try: "float(clicks)/float(views)" Might also work and get the 100 in there: "(100.0*clicks)/views"
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of BZ Sent: Monday, November 18, 2002 2:17 PM To: zope@zope.org Subject: [Zope] Going CRAZY from division
I have two integers "clicks" and "views" and am trying to find what the ratio of clicks to views. This should be (and is on my calculator)
clicks / views * 100 (to get the percent) 3 / 356 * 100 = .842 %
<dtml-var expr="clicks/views"> Returns = 0
<dtml-var expr="clicks/views" fmt="%.6f"> Returns 0.00000
I don't know what is wrong. Anyone?
BZ
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
<dtml-var expr="3.0/356 * 100"> results in 0.842696629213 you should have at least one _not_ integer <hth> fritz (-:fs) ====================================================== Am Montag, 18. November 2002 23:17 erhalten:
I have two integers "clicks" and "views" and am trying to find what the ratio of clicks to views. This should be (and is on my calculator)
clicks / views * 100 (to get the percent) 3 / 356 * 100 = .842 %
<dtml-var expr="clicks/views"> Returns = 0
<dtml-var expr="clicks/views" fmt="%.6f"> Returns 0.00000
I don't know what is wrong. Anyone?
BZ
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
You guys ROCK. That was it. Thanks to everyone! BZ On Monday, November 18, 2002, at 05:48 PM, fritz wrote:
<dtml-var expr="3.0/356 * 100"> results in 0.842696629213
you should have at least one _not_ integer
<hth>
fritz (-:fs)
======================================================
Am Montag, 18. November 2002 23:17 erhalten:
I have two integers "clicks" and "views" and am trying to find what the ratio of clicks to views. This should be (and is on my calculator)
clicks / views * 100 (to get the percent) 3 / 356 * 100 = .842 %
<dtml-var expr="clicks/views"> Returns = 0
<dtml-var expr="clicks/views" fmt="%.6f"> Returns 0.00000
I don't know what is wrong. Anyone?
BZ
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Hi, --On Montag, 18. November 2002 17:17 -0500 BZ <bz@bwanazulia.com> wrote:
I have two integers "clicks" and "views" and am trying to find what the ratio of clicks to views. This should be (and is on my calculator)
clicks / views * 100 (to get the percent) 3 / 356 * 100 = .842 % This one is basic numeric. Not that python (like many other languages) uses integer operation if only integers are used. In this case you want to change your term into
3*100/356 First multiplication and then division. This increases exactness. (This is true for every fixed point arithmetik) Regards Tino
Try your code at a Python interactive prompt. You might be surprised what you get. Compare:
3*100/356 0
3*100.0/356 0.84269662921348309
The use of all integers vs. integers and a float really does make a difference. HTH, Dylan At 04:52 PM 11/19/2002, you wrote:
Hi,
--On Montag, 18. November 2002 17:17 -0500 BZ <bz@bwanazulia.com> wrote:
I have two integers "clicks" and "views" and am trying to find what the ratio of clicks to views. This should be (and is on my calculator)
clicks / views * 100 (to get the percent) 3 / 356 * 100 = .842 % This one is basic numeric. Not that python (like many other languages) uses integer operation if only integers are used. In this case you want to change your term into
3*100/356
First multiplication and then division. This increases exactness. (This is true for every fixed point arithmetik)
Regards Tino
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Hi, --On Dienstag, 19. November 2002 17:26 -0800 Dylan Reinhardt <zope@dylanreinhardt.com> wrote:
Try your code at a Python interactive prompt. You might be surprised what you get.
No. I'm not :-)
Compare:
3*100/356 0
3*100.0/356 0.84269662921348309
Sure, but its a very exact 0 :-) Regards Tino
And it seems that the order of division over multiplication doesn't really matter...
3*100.0/356 == 3/356.0*100 1
Tino Wildenhain wrote:
Hi,
--On Montag, 18. November 2002 17:17 -0500 BZ <bz@bwanazulia.com> wrote:
I have two integers "clicks" and "views" and am trying to find what
the
ratio of clicks to views. This should be (and is on my calculator)
clicks / views * 100 (to get the percent) 3 / 356 * 100 = .842 %
This one is basic numeric. Not that python (like many other languages) uses integer operation if only integers are used. In this case you want to change your term into
3*100/356
First multiplication and then division. This increases exactness. (This is true for every fixed point arithmetik)
Regards Tino
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
participants (8)
-
BZ -
Charlie Reiman -
Chris Beaven -
Chris McDonough -
Dylan Reinhardt -
fritz -
Martijn Pieters -
Tino Wildenhain