473,473 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

working of round()

Does round() always perfectly return the output expected or are there
some artifacts which don't allow perfect functionality

Using python 2.5:
>>round(12.234, 2)
12.23
>>round(12.234, 3)
12.234
>>round(12.234, 1)
12.199999999999999
>>>
but was expecting 12.2

Also, for round(x,n), can't 'x' be an expression

round(5.25/2, 2)

was expecting 2.62 , but
>>round(5.25/2, 2)
2.6299999999999999

Apr 16 '07 #1
3 1812
Kr************@gmail.com wrote:
Does round() always perfectly return the output expected or are there
some artifacts which don't allow perfect functionality

Using python 2.5:
>>>round(12.234, 2)
12.23
>>>round(12.234, 3)
12.234
>>>round(12.234, 1)
12.199999999999999

but was expecting 12.2
http://docs.python.org/tut/node16.html
Also, for round(x,n), can't 'x' be an expression

round(5.25/2, 2)

was expecting 2.62 , but
>>>round(5.25/2, 2)
2.6299999999999999
round(x, n) essentially does the following:

math.floor(x * 10**n + 0.5) / 10**n

Since (5.25/2)*100 == 262.5, adding 0.5 gives 263.0 and ultimately 2.63 as the
rounded number. round() does not do anything more complicated like round-to-even.

Use the decimal module if you want decimal arithmetic rather than binary
floating point.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Apr 16 '07 #2
Kr************@gmail.com wrote:
Does round() always perfectly return the output expected or are there
some artifacts which don't allow perfect functionality

Using python 2.5:
>>>round(12.234, 2)
12.23
>>>round(12.234, 3)
12.234
>>>round(12.234, 1)
12.199999999999999

but was expecting 12.2

Also, for round(x,n), can't 'x' be an expression

round(5.25/2, 2)

was expecting 2.62 , but
>>>round(5.25/2, 2)
2.6299999999999999
You're running into floating-point issues (e.g. it's impossible to
represent 2.63 perfectly in binary). What are you really trying to do?
If you just want to format these with only two decimal places, use
string formatting::
>>'%.2f' % 12.234
'12.23'
>>'%.2f' % (5.25 / 2)
'2.63'

I'm not sure why you would have expected 2.62 for the latter when::
>>5.25 / 2
2.625

STeVe
Apr 16 '07 #3
On Apr 15, 8:06 pm, Krishna.K.1...@gmail.com wrote:
Does round() always perfectly return the output expected or are there
some artifacts which don't allow perfect functionality

Using python 2.5:
>round(12.234, 2)
12.23
>round(12.234, 3)
12.234
>round(12.234, 1)
12.199999999999999

but was expecting 12.2

Also, for round(x,n), can't 'x' be an expression

round(5.25/2, 2)

was expecting 2.62 , but
>round(5.25/2, 2)

2.6299999999999999
The problem is that floats are encoded as fractions where the
denominator is an exponent of 2.
2.63 is not representable as such a fraction.
2.629999999999999999999999999999999999... is the closest fraction.
Rounding this number will only give you the same thing.
If you want decimals to act as expected, use the Decimal class in
module decimal. It works as expected, but is much slower.

Apr 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: David Veeneman | last post by:
I'm working on a project that uses floating-point values (doubles), and I'm being driven crazy by something pretty basic. I understand that it's in the nature of floating-point calculations to...
4
by: Chris Davoli | last post by:
The folllowing will round to 526, but it should round to 527. It works correctly for all other numbers, except for this one. Does anybody know of a bug in Math.Round? Dim ldecWater As Decimal =...
5
by: Marc | last post by:
Hi, I cannot get the round function to work on vb.net. I get the message that round is not declared? Has round function changed or something? MsgBox(round(3, 3))
7
by: kkmigas | last post by:
Can some one explain if this can be fixed using php.ini settings ? echo "round 20.545 -".round(20.545,2)."<br>"; echo "round 20.555 -".round(20.555,2)."<br>"; echo "number_format 20.545...
10
by: johnlutz | last post by:
I found code close to this somewhere yesterday for a vertical scrollbar and I modified it for a horizontal scroll bar. It seems to work in IE 7 and Netscape 7, but it isn't working for the FF I have...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
1
by: yappy70 | last post by:
Hello. I am a college student in a Java class. I am working on an assignment, that I believe my code is correct, but after its compiled and ran I only get values back for the last statement. Any...
18
by: Philluminati | last post by:
I am writing a poker game which needs to work with cash. I am aware that there are problems with floats that make them unsuitable for storing money values. On top of this I may be expected to do...
3
by: dirknrw | last post by:
Hi, I have a tricky question!? :-) I'm using the perl -e command in order to match lines between two search patterns. The problem I have is, for the first example it is working, for the second...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.