Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89539 > unrolled thread
| Started by | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| First post | 2015-04-29 13:24 +0200 |
| Last post | 2015-04-30 14:29 +1000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
str more precise in 3 as 2.7 Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 13:24 +0200
Re: str more precise in 3 as 2.7 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-29 12:50 +0100
Re: str more precise in 3 as 2.7 Chris Angelico <rosuav@gmail.com> - 2015-04-29 21:55 +1000
Re: str more precise in 3 as 2.7 Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 14:09 +0200
Re: str more precise in 3 as 2.7 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-30 14:29 +1000
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-04-29 13:24 +0200 |
| Subject | str more precise in 3 as 2.7 |
| Message-ID | <87twvzch2u.fsf@Equus.decebal.nl> |
I am trying to make my modules also work under Python 3. I found that
str is more precise in Python 3. The expression:
str(134 / 6.0)
gives in 2.7.8:
'22.3333333333'
and in 3.4.1:
'22.333333333333332'
Was not very hard to solve:
if python_version == 3:
output06[5] = '22.333333333333332'
output10[5] = '23.333333333333332'
output10[8] = '24.77777777777778'
Earlier I did:
import sys
# For when the difference between 2 and 3 is important
python_version = sys.version_info[0]
At the moment I use it only once, but it is never bad to be prepared.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-04-29 12:50 +0100 |
| Message-ID | <mailman.82.1430308275.3680.python-list@python.org> |
| In reply to | #89539 |
On 29/04/2015 12:24, Cecil Westerhof wrote: > I am trying to make my modules also work under Python 3. I found that > str is more precise in Python 3. The expression: > str(134 / 6.0) > gives in 2.7.8: > '22.3333333333' > and in 3.4.1: > '22.333333333333332' > > Was not very hard to solve: > if python_version == 3: > output06[5] = '22.333333333333332' > output10[5] = '23.333333333333332' > output10[8] = '24.77777777777778' > > Earlier I did: > import sys > > # For when the difference between 2 and 3 is important > python_version = sys.version_info[0] > > At the moment I use it only once, but it is never bad to be prepared. > They simply output different numbers of digits by default. Just control the number of digits that you want to see and in this case you needn't worry again about Python 2 vs 3 differences. See either https://docs.python.org/3/library/string.html#formatspec or https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-04-29 21:55 +1000 |
| Message-ID | <mailman.83.1430308536.3680.python-list@python.org> |
| In reply to | #89539 |
On Wed, Apr 29, 2015 at 9:24 PM, Cecil Westerhof <Cecil@decebal.nl> wrote: > I am trying to make my modules also work under Python 3. I found that > str is more precise in Python 3. The expression: > str(134 / 6.0) > gives in 2.7.8: > '22.3333333333' > and in 3.4.1: > '22.333333333333332' > > Was not very hard to solve: > if python_version == 3: > output06[5] = '22.333333333333332' > output10[5] = '23.333333333333332' > output10[8] = '24.77777777777778' Try using repr() instead of str() - it might be more useful to you. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-04-29 14:09 +0200 |
| Message-ID | <87lhhbcf1d.fsf@Equus.decebal.nl> |
| In reply to | #89543 |
Op Wednesday 29 Apr 2015 13:55 CEST schreef Chris Angelico: > On Wed, Apr 29, 2015 at 9:24 PM, Cecil Westerhof <Cecil@decebal.nl> wrote: >> I am trying to make my modules also work under Python 3. I found >> that str is more precise in Python 3. The expression: str(134 / >> 6.0) gives in 2.7.8: '22.3333333333' and in 3.4.1: >> '22.333333333333332' >> >> Was not very hard to solve: >> if python_version == 3: >> output06[5] = '22.333333333333332' >> output10[5] = '23.333333333333332' >> output10[8] = '24.77777777777778' > > Try using repr() instead of str() - it might be more useful to you. That looks very interesting. I am going to use it. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2015-04-30 14:29 +1000 |
| Message-ID | <5541afad$0$11119$c3e8da3@news.astraweb.com> |
| In reply to | #89539 |
On Wednesday 29 April 2015 21:24, Cecil Westerhof wrote:
> I am trying to make my modules also work under Python 3. I found that
> str is more precise in Python 3. The expression:
> str(134 / 6.0)
> gives in 2.7.8:
> '22.3333333333'
> and in 3.4.1:
> '22.333333333333332'
The precise details of how floats are displayed may vary. In general, if you
care about the exact details, you should control them by hand using either
C-style % string interpolation, or the format method:
py> x = 134 / 6.0
py> "%.16f" % x
'22.3333333333333321'
py> "{:.16f}".format(x)
'22.3333333333333321'
--
Steve
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web