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
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 -
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
--
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...
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
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
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
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