473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Floating point -> string conversions

Can someone explain these differences?
from math import pi
pi 3.1415926535897931 type(pi) <type 'float'> repr(pi) '3.1415926535897931' str(pi) '3.14159265359' '%f' % pi '3.141593' '%.16f' % pi

'3.1415926535897931'

Why do the string and default floating point representations get rounded?

(and is this documented anywhere?)

--
John.
Jul 18 '05 #1
5 1726
John Fouhy <jf****@paradise.net.nz> wrote:
...
Why do the string and default floating point representations get rounded?

(and is this documented anywhere?)


Sure, right in the "Tutorial (start here)" for example.
http://www.python.org/dev/doc/devel/tut/node16.html
Alex
Jul 18 '05 #2
al*****@yahoo.com (Alex Martelli) wrote in message news:<1gmxb89.r6ndtk17sltbgN%al*****@yahoo.com>...
John Fouhy <jf****@paradise.net.nz> wrote:
...
Why do the string and default floating point representations get rounded?

(and is this documented anywhere?)

Sure, right in the "Tutorial (start here)" for example.
http://www.python.org/dev/doc/devel/tut/node16.html
Alex


I knew I should have looked in the tutorial, rather than just the
"numeric types" section of the library reference :-/

Actually, that page doesn't mention that '%f' will give you a
different rounding again...

--
John.
Jul 18 '05 #3
I have another related question...
pow(2, 31) 2147483648L '%d' % 2147483647.0 # python will convert to int '2147483647' '%d' % 2147483648.0 # too big for an int, so error. Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: int argument required '%d' % long(2147483648.0) # but yet, no trouble accepting a long. '2147483648' '%d' % int(2147483648.0) # and int() converts to long anyway

'2147483648'

Is this a bug?

(python 2.3.4)

--
John.
Jul 18 '05 #4
John Fouhy wrote:
I have another related question...

pow(2, 31)
2147483648L
'%d' % 2147483647.0 # python will convert to int
'2147483647'
'%d' % 2147483648.0 # too big for an int, so error.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: int argument required
'%d' % long(2147483648.0) # but yet, no trouble accepting a long.
'2147483648'
'%d' % int(2147483648.0) # and int() converts to long anyway


'2147483648'

Is this a bug?

(python 2.3.4)

I don't think so. The fact of the matter is that a %d format token
explicitly expects an integral value.

The fact that %s coerces things makes us expect more of other format
tokens, but what would you expect %d to do with 2147483648.5?

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
Jul 18 '05 #5
Steve Holden <st***@holdenweb.com> wrote in message news:<t9Skd.57$nj.56@lakeread01>...
The fact that %s coerces things makes us expect more of other format
tokens, but what would you expect %d to do with 2147483648.5?


I'd expect it to give me "2147483648", since that would be consistent
with its other behaviour :-)

(eg: eval('"%d" % 1.5') == '1')

I guess what annoys me is that python encourages you to not worry
about the difference between ints and longs (which is great) (unless
you're doing high performance computing), but then something like this
can cause code to stop working just because you've passed a border
which is almost invisible.

(of course, this means that one solution which I would be happy with
is for python to reject any non-integer/long arguments to "%d" :-) )

--
John.
Jul 18 '05 #6

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

Similar topics

31
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
24
by: j0mbolar | last post by:
C supports single precision floating point and double precision floating point but does it support fixed floating point? i've read that fixed floating point is more accurate than single precision...
7
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.