Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python 3.0 new integer division

Reply
Thread Tools

Python 3.0 new integer division

 
 
Hutch
Guest
Posts: n/a
 
      04-08-2008
We now have a float result when two integers are divided in the same mannor
as 2.4 or 2.5.
I can handle that and use the Floor division but a simple question.

Why in the world would you round down the last presented digit to a 6
instead of just leaving it along as an 8.
For some reason rounding down for a float in Python does not seem correct.

IDLE 3.0a4

>>> 12345678901234567890123456789012345678901234567890/345

3.5784576525317586e+46


>>> 12345678901234567890123456789012345678901234567890//345

35784576525317588087314367504383610663481839327
^
^|
35784576525317586000000000000000000000000000000 == 3.5784576525317586e+46


 
Reply With Quote
 
 
 
 
Jonathan Gardner
Guest
Posts: n/a
 
      04-08-2008
On Apr 8, 9:13*am, "Hutch" <A...@MCHSI.COM> wrote:
> We now have a float result when two integers are divided in the same mannor
> as 2.4 or 2.5.
> I can handle that and use the Floor division but a simple question.
>
> Why in the world would you round down the last presented digit to a 6
> instead of just leaving it along as an 8.
> For some reason rounding down for a float in Python does not seem correct.
>
> IDLE 3.0a4
>
> >>> 12345678901234567890123456789012345678901234567890/345

>
> 3.5784576525317586e+46
>
> >>> 12345678901234567890123456789012345678901234567890//345

>
> 35784576525317588087314367504383610663481839327
> * * * * * * * * * * * * * * * * ^
> * * * * * * * * * * * * * * * * ^|
> 35784576525317586000000000000000000000000000000 *== 3.5784576525317586e+46


Floats are weird that way. Part of the problem is you are representing
a decimal number (base 10) in binary (base 2). The other part is that
you can't increment floats by a very tiny amount. In other words, a +
b = a for floats when b is sufficiently small but not zero.

Blame floats, not Python.

If you want precision with fractions, you should be using the Decimal
type, which uses a rational. A rational, if you recall from your math
classes, is one integer divided by another.
 
Reply With Quote
 
 
 
 
Matimus
Guest
Posts: n/a
 
      04-08-2008
On Apr 8, 9:13 am, "Hutch" <A...@MCHSI.COM> wrote:
> We now have a float result when two integers are divided in the same mannor
> as 2.4 or 2.5.
> I can handle that and use the Floor division but a simple question.
>
> Why in the world would you round down the last presented digit to a 6
> instead of just leaving it along as an 8.
> For some reason rounding down for a float in Python does not seem correct.
>
> IDLE 3.0a4
>
> >>> 12345678901234567890123456789012345678901234567890/345

>
> 3.5784576525317586e+46
>
> >>> 12345678901234567890123456789012345678901234567890//345

>
> 35784576525317588087314367504383610663481839327
> ^
> ^|
> 35784576525317586000000000000000000000000000000 == 3.5784576525317586e+46


This just has to do with the way floating point numbers are
represented in memory. More information:
http://docs.python.org/tut/node16.html

Matt
 
Reply With Quote
 
Hutch
Guest
Posts: n/a
 
      04-08-2008

"Matimus" <> wrote in message
news:b6eafdd4-dbf8-4269-962f-...
> On Apr 8, 9:13 am, "Hutch" <A...@MCHSI.COM> wrote:
>> We now have a float result when two integers are divided in the same
>> mannor
>> as 2.4 or 2.5.
>> I can handle that and use the Floor division but a simple question.
>>
>> Why in the world would you round down the last presented digit to a 6
>> instead of just leaving it along as an 8.
>> For some reason rounding down for a float in Python does not seem
>> correct.
>>
>> IDLE 3.0a4
>>
>> >>> 12345678901234567890123456789012345678901234567890/345

>>
>> 3.5784576525317586e+46
>>
>> >>> 12345678901234567890123456789012345678901234567890//345

>>
>> 35784576525317588087314367504383610663481839327
>> ^
>> ^|
>> 35784576525317586000000000000000000000000000000 ==
>> 3.5784576525317586e+46

>
> This just has to do with the way floating point numbers are
> represented in memory. More information:
> http://docs.python.org/tut/node16.html
>
> Matt


Was thinking IBM decimal when I asked the question --should have remembered
detail of floats.
Thanks
Hutch


 
Reply With Quote
 
Grzegorz Słodkowicz
Guest
Posts: n/a
 
      04-08-2008

> If you want precision with fractions, you should be using the Decimal
> type, which uses a rational. A rational, if you recall from your math
> classes, is one integer divided by another.
>


Isn't Decimal a BCD implementation?
 
Reply With Quote
 
Jonathan Gardner
Guest
Posts: n/a
 
      04-08-2008
On Apr 8, 2:25*pm, Grzegorz Słodkowicz <jerg...@wp.pl> wrote:
>
> Isn't Decimal a BCD implementation?


Yep, you are right and I am wrong. http://www.python.org/dev/peps/pep-0...y-not-rational


 
Reply With Quote
 
Mark Dickinson
Guest
Posts: n/a
 
      04-09-2008
On Apr 8, 6:01 pm, Jonathan Gardner <jgard...@jonathangardner.net>
wrote:
> On Apr 8, 2:25 pm, Grzegorz Słodkowicz <jerg...@wp.pl> wrote:
>
>
>
> > Isn't Decimal a BCD implementation?

>
> Yep, you are right and I am wrong.http://www.python.org/dev/peps/pep-0...y-not-rational


Strictly speaking, BCD doesn't come into it: the coefficient of a
Decimal instance is stored simply as a string of digits. This is
pretty wasteful in terms of space: 1 byte per decimal digit
instead of the 4 bits per digit that BCD gives, but it's
convenient and fairly efficient.

An alternative representation that's gained popularity recently is
DPD (densely packed decimal), which packs 3 decimal digits into 10
bits in a clever way that allows reasonably efficient extraction
of any one of the 3 digits. Decimal doesn't use this either.

Mark
 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      04-09-2008
On Apr 9, 8:35*pm, Mark Dickinson <dicki...@gmail.com> wrote:
> Strictly speaking, BCD doesn't come into it: *the coefficient of a
> Decimal instance is stored simply as a string of digits. *This is
> pretty wasteful in terms of space: *1 byte per decimal digit
> instead of the 4 bits per digit that BCD gives, but it's
> convenient and fairly efficient.
>
> An alternative representation that's gained popularity recently is
> DPD (densely packed decimal), which packs 3 decimal digits into 10
> bits in a clever way that allows reasonably efficient extraction
> of any one of the 3 digits. *Decimal doesn't use this either.
>
> Mark


Naive question: why not just use a long + an exponent?

e.g. 132560 -> (13256, 1)
0.534 -> (534, -3)
5.23e10 -> (523,

--
Arnaud

 
Reply With Quote
 
Mark Dickinson
Guest
Posts: n/a
 
      04-09-2008
On Apr 9, 3:57*pm, Arnaud Delobelle <arno...@googlemail.com> wrote:
> Naive question: why not just use a long + an exponent?
>
> e.g. 132560 *-> (13256, 1)
> * * *0.534 * -> (534, -3)
> * * *5.23e10 -> (523,
>


It's a good question. The standard answer is that if the
coefficient is a long then it's awkward to get at individual
digits; looking up a digit becomes an O(n^2) operation
(involving a division and a remainder) instead of the O(1)
that it should be. And you need access to the digits for
rounding operations, which are pretty darn common (one
round at the end of each arithmetic operation, as a rule).

But I could easily be convinced that storing the coefficient
as a long speeds things up for the usual use cases, even if
it gives horrible asymptotics for those trying to do really
high-precision calculations. And it would certainly make
the code slightly simpler in places.

It would be great if someone could try converting Decimal
so that the coefficient is stored as a long, to see if there's
any noticeable impact on speed one way or the other. It
wouldn't be such a hard change: a few hours of work at most.
It's on my todo list to try this, but so far down that it's
not looking like it'll end up at the top of the list before
Christmas 20??.

Mark
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Division of an integer by a real number using VHDL genlock VHDL 22 03-17-2010 03:27 PM
division by 7 without using division operator krypto.wizard@gmail.com C Programming 94 02-09-2007 06:57 AM
integer division Darius Fatakia C++ 2 01-28-2004 03:47 PM
will Synposys Design Compiler support division by two's power and integer rounding? walala VHDL 12 09-14-2003 03:49 PM
integer division towards -infinity Sidney Cadot C Programming 3 07-11-2003 02:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57