# Best way to calculate fraction part of x? Classic List Threaded 8 messages Open this post in threaded view
|

## Best way to calculate fraction part of x?

 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
Open this post in threaded view
|

## Best way to calculate fraction part of x?

 On Mon, Mar 23, 2015 at 7:52 AM, Steven D'Aprano 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
Open this post in threaded view
|

## Best way to calculate fraction part of x?

 In reply to this post by Steven D'Aprano-11 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
Open this post in threaded view
|

## Best way to calculate fraction part of x?

 In reply to this post by Steven D'Aprano-11 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
Open this post in threaded view
|

## Best way to calculate fraction part of x?

 On Mon, Mar 23, 2015 at 8:38 PM, Emile van Sebille 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...? -------------- next part -------------- An HTML attachment was scrubbed... URL:
Open this post in threaded view
|

## Best way to calculate fraction part of x?

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