Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > floating point woes

Reply
Thread Tools

floating point woes

 
 
Hans-Peter Jansen
Guest
Posts: n/a
 
      02-15-2011
Hi,

while I usually cope with the woes of floating point issues, this is
one, that I didn't expect:

>>> round(2.385, 2)

2.3799999999999999

Doesn't the docs say, it's rounded up for this case?

<quote>
Values are rounded to the closest multiple of 10 to the power minus n;
if two multiples are equally close, rounding is done away from 0
</quote>

Well, that one is clearly rounding down.

What's up, eh, down here?

Pete

Python 2.6 (r26:66714, Feb 8 2011, 08:50:11)
[GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2

 
Reply With Quote
 
 
 
 
Mel
Guest
Posts: n/a
 
      02-16-2011
Hans-Peter Jansen wrote:

> Hi,
>
> while I usually cope with the woes of floating point issues, this is
> one, that I didn't expect:
>
>>>> round(2.385, 2)

> 2.3799999999999999
>
> Doesn't the docs say, it's rounded up for this case?
>
> <quote>
> Values are rounded to the closest multiple of 10 to the power minus n;
> if two multiples are equally close, rounding is done away from 0
> </quote>
>
> Well, that one is clearly rounding down.
>
> What's up, eh, down here?


2.385 isn't really 2.385:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> repr (2.385)

'2.3849999999999998'
>>>


so it correctly rounded down. You need to use Decimal numbers if you want
numbers that behave the way they look.

Mel.

 
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
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
floating point problem... floating indeed :( teeshift Ruby 2 12-01-2006 01:16 AM
converting floating point to fixed point H aka N VHDL 15 03-02-2006 02:26 PM
floating point to fixed point conversion riya1012@gmail.com C Programming 4 02-22-2006 05:56 PM
Fixed-point format for floating-point numbers Motaz Saad Java 7 11-05-2005 05:33 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