[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

float / rounding question

helen.m.flynn

2/25/2008 10:45:00 AM

Hi I'm very much a beginner with Python.
I want to write a function to convert celcius to fahrenheit like this
one:

def celciusToFahrenheit(tc):
tf = (9/5)*tc+32
return tf

I want the answer correct to one decimal place, so
celciusToFahrenheit(12) would return 53.6.

Of course the function above returns 53.600000000000001.

How do I format it correctly?
16 Answers

helen.m.flynn

2/25/2008 10:59:00 AM

0

On Feb 25, 10:44 am, helen.m.fl...@gmail.com wrote:
> Hi I'm very much a beginner with Python.
> I want to write a function to convert celcius to fahrenheit like this
> one:
>
> def celciusToFahrenheit(tc):
> tf = (9/5)*tc+32
> return tf
>
> I want the answer correct to one decimal place, so
> celciusToFahrenheit(12) would return 53.6.
>
> Of course the function above returns 53.600000000000001.
>
> How do I format it correctly?

By the way, I tried this:

return '%2.1f' % tf but that returns a string instead of a number.

Any other suggestions?

Jorge Godoy

2/25/2008 11:05:00 AM

0

sabatier wrote:

> On Feb 25, 10:44 am, helen.m.fl...@gmail.com wrote:
>> Hi I'm very much a beginner with Python.
>> I want to write a function to convert celcius to fahrenheit like this
>> one:
>>
>> def celciusToFahrenheit(tc):
>> tf = (9/5)*tc+32
>> return tf
>>
>> I want the answer correct to one decimal place, so
>> celciusToFahrenheit(12) would return 53.6.
>>
>> Of course the function above returns 53.600000000000001.
>>
>> How do I format it correctly?
>
> By the way, I tried this:
>
> return '%2.1f' % tf but that returns a string instead of a number.
>
> Any other suggestions?

But you are asking for a string on your format string above.

And also formatting make no sense in other context since a number is a
number and 53.600000 and 53.6 are the same number (besides precision).

You are concerned with how numbers are represented in binary. When
displaying the value use the format string you shown above and all will
work.

Necmettin Begiter

2/25/2008 12:05:00 PM

0

25 February 2008 Monday 12:44:46 tarihinde helen.m.flynn@gmail.com sunlari yazmisti:
> Hi I'm very much a beginner with Python.
> I want to write a function to convert celcius to fahrenheit like this
> one:
>
> def celciusToFahrenheit(tc):
> tf = (9/5)*tc+32
> return tf
>
> I want the answer correct to one decimal place, so
> celciusToFahrenheit(12) would return 53.6.
>
> Of course the function above returns 53.600000000000001.
>
> How do I format it correctly?

Use the round(number,digits) function:

tf = round((9/5)*tc+32,1)

casevh

2/25/2008 1:26:00 PM

0

On Feb 25, 2:44 am, helen.m.fl...@gmail.com wrote:
> Hi I'm very much a beginner with Python.
> I want to write a function to convert celcius to fahrenheit like this
> one:
>
> def celciusToFahrenheit(tc):
> tf = (9/5)*tc+32
> return tf
>
> I want the answer correct to one decimal place, so
> celciusToFahrenheit(12) would return 53.6.
>
> Of course the function above returns 53.600000000000001.
>
> How do I format it correctly?

That is the normal behavior for binary (radix-2) numbers. Just like it
is impossible write 1/3 exactly as a decimal (radix-10) number, 536/10
cannot be written exactly as a binary number. If you really need
decimal numbers, use the Decimal class.

See http://docs.python.org/tut/n....

casevh

Sion Arrowsmith

2/25/2008 2:32:00 PM

0

Necmettin Begiter <necmettin.begiter@gmail.com> wrote:
>25 February 2008 Monday 12:44:46 tarihinde helen.m.flynn@gmail.com =C5=9Fun=
>lar=C4=B1 yazm=C4=B1=C5=9Ft=C4=B1:
>> Of course the function above returns 53.600000000000001.
>>=20
>> How do I format it correctly?
>
>Use the round(number,digits) function:
>
>tf =3D round((9/5)*tc+32,1)

>>> 53.6
53.600000000000001
>>> round(53.6, 1)
53.600000000000001

--
\S -- siona@chiark.greenend.org.uk -- http://www.chaos.org...
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

Mel

2/25/2008 3:21:00 PM

0

helen.m.flynn@gmail.com wrote:
> Hi I'm very much a beginner with Python.
> I want to write a function to convert celcius to fahrenheit like this
> one:
>
> def celciusToFahrenheit(tc):
> tf = (9/5)*tc+32
> return tf
>
> I want the answer correct to one decimal place, so
> celciusToFahrenheit(12) would return 53.6.
>
> Of course the function above returns 53.600000000000001.
>
> How do I format it correctly?

print celcisuToFahrenheit (12)

will do fine.


Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def celciusToFahrenheit(tc):
.... tf = (float(9)/5)*tc+32
.... return tf
....
>>> celciusToFahrenheit (12)
53.600000000000001
>>> print celciusToFahrenheit (12)
53.6


The straight value display from the interpreter pursues precision to
the bitter end, doing its formatting with the repr function. print
uses str formatting for a more expected result.

Mel.

Terry Reedy

2/25/2008 8:15:00 PM

0


<helen.m.flynn@gmail.com> wrote in message
news:d4d9e9d6-b0e0-4063-a5b2-456bcea5a6ce@z17g2000hsg.googlegroups.com...
| Hi I'm very much a beginner with Python.
| I want to write a function to convert celcius to fahrenheit like this
| one:
|
| def celciusToFahrenheit(tc):
| tf = (9/5)*tc+32
| return tf

Unless you are importing 'integer division' or using 3.0, that should be
9.0/5.0.

| I want the answer correct to one decimal place, so
| celciusToFahrenheit(12) would return 53.6.

As written, running above on 2.x returns 44.

tjr



John Machin

2/25/2008 10:15:00 PM

0

On Feb 26, 7:14 am, "Terry Reedy" <tjre...@udel.edu> wrote:
> <helen.m.fl...@gmail.com> wrote in message
>
> news:d4d9e9d6-b0e0-4063-a5b2-456bcea5a6ce@z17g2000hsg.googlegroups.com...
> | Hi I'm very much a beginner with Python.
> | I want to write a function to convert celcius to fahrenheit like this
> | one:
> |
> | def celciusToFahrenheit(tc):
> | tf = (9/5)*tc+32
> | return tf
>
> Unless you are importing 'integer division' or using 3.0, that should be
> 9.0/5.0.

Has the syntax changed? I thought it was:
from __future__ import division

The OP may wish to avoid the confusion and the pointless division by
using:
tf = 1.8 * tc + 32


>
> | I want the answer correct to one decimal place, so
> | celciusToFahrenheit(12) would return 53.6.
>
> As written, running above on 2.x returns 44.
>
> tjr

Piet van Oostrum

3/7/2008 10:12:00 PM

0

>>>>> casevh <casevh@gmail.com> (C) wrote:

>C> On Feb 25, 2:44 am, helen.m.fl...@gmail.com wrote:
>>> Hi I'm very much a beginner with Python.
>>> I want to write a function to convert celcius to fahrenheit like this
>>> one:
>>>
>>> def celciusToFahrenheit(tc):
>>> tf = (9/5)*tc+32
>>> return tf
>>>
>>> I want the answer correct to one decimal place, so
>>> celciusToFahrenheit(12) would return 53.6.
>>>
>>> Of course the function above returns 53.600000000000001.
>>>
>>> How do I format it correctly?

>C> That is the normal behavior for binary (radix-2) numbers. Just like it
>C> is impossible write 1/3 exactly as a decimal (radix-10) number, 536/10
>C> cannot be written exactly as a binary number. If you really need
>C> decimal numbers, use the Decimal class.

Sorry to come in so late in this discussion. Although it is correct to say
that many real numbers that have an exact decimal representation cannot be
exactly represented in binary, that is no excuse to print 53.6 as
53.600000000000001. This is just lousy printing and the fact that this kind
of question comes up every week shows that it is confusing to many people.

Python just uses the C library for printing, I presume, and the conversion
routines in the C library are rather simplistic. It is, however, possible
to do better, so that 53.6 -- although internally represented as something
that could be described as 53.600000000000001 -- will actually be printed
as 53.6. Indeed, when reading back the printed value you get the exact
representation as the internal number that was printed, and IMHO, that is
what matters. Apparently there is more than one representation that has
this property. I would guess (but didn't check) that
53.60000000000000100000001 also gives the same number. From all these
representations it would be best to choose the simplest one, i.e. 53.6.
This problem and a solution has been described in one of the classical
computer science publications:

http://portal.acm.org/citation.cf...
--
Piet van Oostrum <piet@cs.uu.nl>
URL: http://pietvano... [PGP 8DAE142BE17999C4]
Private email: piet@vanoostrum.org

Mark Dickinson

3/8/2008 12:13:00 AM

0

On Mar 7, 5:12 pm, Piet van Oostrum <p...@cs.uu.nl> wrote:
> Python just uses the C library for printing, I presume, and the conversion
> routines in the C library are rather simplistic. It is, however, possible
> to do better, so that 53.6 -- although internally represented as something
> that could be described as 53.600000000000001 -- will actually be printed
> as 53.6.

There are issues with doing this portably and reliably. See

http://bugs.python.org...

for a recent discussion.

Mark