Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > float / rounding question

Reply
Thread Tools

float / rounding question

 
 
helen.m.flynn@gmail.com
Guest
Posts: n/a
 
      02-25-2008
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?
 
Reply With Quote
 
 
 
 
sabatier
Guest
Posts: n/a
 
      02-25-2008
On Feb 25, 10:44 am, (E-Mail Removed) 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?

 
Reply With Quote
 
 
 
 
Jorge Godoy
Guest
Posts: n/a
 
      02-25-2008
sabatier wrote:

> On Feb 25, 10:44 am, (E-Mail Removed) 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.

 
Reply With Quote
 
Necmettin Begiter
Guest
Posts: n/a
 
      02-25-2008
25 February 2008 Monday 12:44:46 tarihinde http://www.velocityreviews.com/forums/(E-Mail Removed) şunları yazmıştı:
> 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)
 
Reply With Quote
 
casevh
Guest
Posts: n/a
 
      02-25-2008
On Feb 25, 2:44 am, (E-Mail Removed) 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/node16.html.

casevh
 
Reply With Quote
 
Sion Arrowsmith
Guest
Posts: n/a
 
      02-25-2008
Necmettin Begiter <(E-Mail Removed)> wrote:
>25 February 2008 Monday 12:44:46 tarihinde (E-Mail Removed) =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 -- (E-Mail Removed) -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu become se bera eadward ofdun hlddre heafdes bce bump bump bump
 
Reply With Quote
 
Mel
Guest
Posts: n/a
 
      02-25-2008
(E-Mail Removed) 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.

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-25-2008

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| 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



 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      02-25-2008
On Feb 26, 7:14 am, "Terry Reedy" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
> | 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


 
Reply With Quote
 
Piet van Oostrum
Guest
Posts: n/a
 
      03-07-2008
>>>>> casevh <(E-Mail Removed)> (C) wrote:

>C> On Feb 25, 2:44 am, (E-Mail Removed) 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.cfm?id=93559
--
Piet van Oostrum <(E-Mail Removed)>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: (E-Mail Removed)
 
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
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
double to float rounding error in 8th digit Shirsoft C++ 13 02-10-2007 04:41 PM
Rounding Float in C and Remove those Zeros kennethlou@yahoo.com.hk C Programming 13 05-15-2006 10:27 PM
rounding a float/double to nearest 1/10th Shea Martin C++ 7 10-30-2003 04:51 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments