Re: Best way to calculate fraction part of x?

2015-03-26 Thread Russell Owen
On 3/24/15 6:39 PM, Jason Swails wrote: On Mon, Mar 23, 2015 at 8:38 PM, Emile van Sebille em...@fenx.com mailto:em...@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

Re: Best way to calculate fraction part of x?

2015-03-26 Thread Oscar Benjamin
On 23 March 2015 at 12:52, Steven D'Aprano steve+comp.lang.pyt...@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 -

Re: Best way to calculate fraction part of x?

2015-03-26 Thread Emile van Sebille
On 3/24/2015 6:39 PM, Jason Swails wrote: On Mon, Mar 23, 2015 at 8:38 PM, Emile van Sebille em...@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 --

Re: Best way to calculate fraction part of x?

2015-03-24 Thread Jason Swails
On Mon, Mar 23, 2015 at 8:38 PM, Emile van Sebille em...@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...​

Best way to calculate fraction part of x?

2015-03-23 Thread Steven D'Aprano
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

Re: Best way to calculate fraction part of x?

2015-03-23 Thread Skip Montanaro
On Mon, Mar 23, 2015 at 7:52 AM, Steven D'Aprano steve+comp.lang.pyt...@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

Re: Best way to calculate fraction part of x?

2015-03-23 Thread Mark Lawrence
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

Re: Best way to calculate fraction part of x?

2015-03-23 Thread Emile van Sebille
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 -- https://mail.python.org/mailman/listinfo/python-list