Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87827 > unrolled thread
| Started by | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| First post | 2015-03-23 23:52 +1100 |
| Last post | 2015-03-26 15:02 -0700 |
| Articles | 9 — 8 participants |
Back to article view | Back to comp.lang.python
Best way to calculate fraction part of x? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-23 23:52 +1100
Re: Best way to calculate fraction part of x? Skip Montanaro <skip.montanaro@gmail.com> - 2015-03-23 08:02 -0500
Re: Best way to calculate fraction part of x? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-23 13:11 +0000
Re: Best way to calculate fraction part of x? wxjmfauth@gmail.com - 2015-03-23 06:17 -0700
Re: Best way to calculate fraction part of x? Emile van Sebille <emile@fenx.com> - 2015-03-23 17:38 -0700
Re: Best way to calculate fraction part of x? Jason Swails <jason.swails@gmail.com> - 2015-03-24 21:39 -0400
Re: Best way to calculate fraction part of x? Emile van Sebille <emile@fenx.com> - 2015-03-26 13:48 -0700
Re: Best way to calculate fraction part of x? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-03-26 21:59 +0000
Re: Best way to calculate fraction part of x? Russell Owen <rowen@uw.edu> - 2015-03-26 15:02 -0700
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2015-03-23 23:52 +1100 |
| Subject | Best way to calculate fraction part of x? |
| Message-ID | <55100c7a$0$13004$c3e8da3$5496439d@news.astraweb.com> |
I have a numeric value, possibly a float, Decimal or (improper) Fraction, and I want the fractional part. E.g. fract(2.5) should give 0.5. Here are two ways to do it: py> x = 2.5 py> x % 1 0.5 py> x - int(x) 0.5 x % 1 is significantly faster, but has the disadvantage of giving the complement of the fraction if x is negative: py> x = -2.75 py> x % 1 0.25 Are there any other, possibly better, ways to calculate the fractional part of a number? -- Steven
[toc] | [next] | [standalone]
| From | Skip Montanaro <skip.montanaro@gmail.com> |
|---|---|
| Date | 2015-03-23 08:02 -0500 |
| Message-ID | <mailman.65.1427115770.10327.python-list@python.org> |
| In reply to | #87827 |
On Mon, Mar 23, 2015 at 7:52 AM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > x % 1 is significantly faster, but has the disadvantage of giving the > complement of the fraction if x is negative I suppose abs(x) % 1 would be just as slow as x - int(x) ? (haven't checked) Skip
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-03-23 13:11 +0000 |
| Message-ID | <mailman.66.1427116295.10327.python-list@python.org> |
| In reply to | #87827 |
On 23/03/2015 12:52, Steven D'Aprano wrote: > I have a numeric value, possibly a float, Decimal or (improper) Fraction, > and I want the fractional part. E.g. fract(2.5) should give 0.5. > > Here are two ways to do it: > > py> x = 2.5 > py> x % 1 > 0.5 > py> x - int(x) > 0.5 > > x % 1 is significantly faster, but has the disadvantage of giving the > complement of the fraction if x is negative: > > py> x = -2.75 > py> x % 1 > 0.25 > > Are there any other, possibly better, ways to calculate the fractional part > of a number? > Any sparks here http://stackoverflow.com/questions/875238/fractional-part-of-the-number-question ? I'm assuming that the reference to "pkaeding's algorithm" refers to https://github.com/pkaeding but haven't confirmed that to be fact. -- 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 | wxjmfauth@gmail.com |
|---|---|
| Date | 2015-03-23 06:17 -0700 |
| Message-ID | <ddda796e-6f42-401c-bbfd-069480dfe543@googlegroups.com> |
| In reply to | #87827 |
Le lundi 23 mars 2015 13:52:20 UTC+1, Steven D'Aprano a écrit : >>> math.modf(2.5) (0.5, 2.0) >>>
[toc] | [prev] | [next] | [standalone]
| From | Emile van Sebille <emile@fenx.com> |
|---|---|
| Date | 2015-03-23 17:38 -0700 |
| Message-ID | <mailman.88.1427157542.10327.python-list@python.org> |
| In reply to | #87827 |
On 3/23/2015 5:52 AM, Steven D'Aprano wrote:
> Are there any other, possibly better, ways to calculate the fractional part
> of a number?
float (("%6.3f" % x)[-4:])
Emile
[toc] | [prev] | [next] | [standalone]
| From | Jason Swails <jason.swails@gmail.com> |
|---|---|
| Date | 2015-03-24 21:39 -0400 |
| Message-ID | <mailman.125.1427247586.10327.python-list@python.org> |
| In reply to | #87827 |
[Multipart message — attachments visible in raw view] — view raw
On Mon, Mar 23, 2015 at 8:38 PM, Emile van Sebille <emile@fenx.com> wrote:
> On 3/23/2015 5:52 AM, Steven D'Aprano wrote:
>
> Are there any other, possibly better, ways to calculate the fractional
>> part
>> of a number?
>>
>
> float (("%6.3f" % x)[-4:])
In general you lose a lot of precision this way...
[toc] | [prev] | [next] | [standalone]
| From | Emile van Sebille <emile@fenx.com> |
|---|---|
| Date | 2015-03-26 13:48 -0700 |
| Message-ID | <mailman.214.1427402951.10327.python-list@python.org> |
| In reply to | #87827 |
On 3/24/2015 6:39 PM, Jason Swails wrote:
>
>
> On Mon, Mar 23, 2015 at 8:38 PM, Emile van Sebille <emile@fenx.com
<snip>
> float (("%6.3f" % x)[-4:])
>
>
> In general you lose a lot of precision this way...
>
Even more if you use %6.1 -- but feel free to flavor to taste. :)
Emile
[toc] | [prev] | [next] | [standalone]
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2015-03-26 21:59 +0000 |
| Message-ID | <mailman.219.1427407165.10327.python-list@python.org> |
| In reply to | #87827 |
On 23 March 2015 at 12:52, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > I have a numeric value, possibly a float, Decimal or (improper) Fraction, > and I want the fractional part. E.g. fract(2.5) should give 0.5. > > Here are two ways to do it: > > py> x = 2.5 > py> x % 1 > 0.5 > py> x - int(x) > 0.5 > > x % 1 is significantly faster, but has the disadvantage of giving the > complement of the fraction if x is negative: > > py> x = -2.75 > py> x % 1 > 0.25 The other version gives -0.75 in this case so I guess that's what you want. > Are there any other, possibly better, ways to calculate the fractional part > of a number? What do you mean by better? Is it just faster? To modify the % version so that it's equivalent you can do: >>> x = -2.75 >>> (x % 1) - (x < 0) -0.75 I'm not sure if that's faster than x - int(x) though. Obviously it depends which numeric type you're primarily interested in. Oscar
[toc] | [prev] | [next] | [standalone]
| From | Russell Owen <rowen@uw.edu> |
|---|---|
| Date | 2015-03-26 15:02 -0700 |
| Message-ID | <mailman.220.1427407398.10327.python-list@python.org> |
| In reply to | #87827 |
On 3/24/15 6:39 PM, Jason Swails wrote:
>
>
> On Mon, Mar 23, 2015 at 8:38 PM, Emile van Sebille <emile@fenx.com
> <mailto:emile@fenx.com>> wrote:
>
> On 3/23/2015 5:52 AM, Steven D'Aprano wrote:
>
> Are there any other, possibly better, ways to calculate the
> fractional part
> of a number?
>
>
> float (("%6.3f" % x)[-4:])
>
>
> In general you lose a lot of precision this way...
I suggest modf in the math library:
math.modf(x)
Return the fractional and integer parts of x. Both results carry the
sign of x and are floats.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web