top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Setting longer default decimal precision in python?

+5 votes
1,597 views

Using 1/3 as an example,

 >>> 1./3
0.3333333333333333
 >>> print "%.50f" % (1./3)
0.33333333333333331482961625624739099293947219848633
 >>> print "%.50f" % (10./3)
3.33333333333333348136306995002087205648422241210938
 >>> print "%.50f" % (100./3)
33.33333333333333570180911920033395290374755859375000

which seems to mean real (at least default) decimal precision is limited to "double", 16 digit precision (with rounding error). Is there a way to increase the real precision, preferably as the default?
For instance, UBasic uses a "Words for fractionals", f, "Point(f)" system, where Point(f) sets the decimal display precision, .1^int(ln(65536^73)/ln(10)), with the last few digits usually garbage.
Using "90*(pi/180)*180/pi" as an example to highlight the rounding error (4 = UBasic's f default value):

 Point(2)=.1^09: 89.999999306
 Point(3)=.1^14: 89.9999999999944
 Point(4)=.1^19: 89.9999999999999998772
 Point(5)=.1^24: 89.999999999999999999999217
 Point(7)=.1^33: 89.999999999999999999999999999999823
 Point(10)=.1^48: 89.999999999999999999999999999999999999999999997686
 Point(11)=.1^52: 89.9999999999999999999999999999999999999999999999999632

If not in the core program, is there a higher decimal precision module that can be added?

posted Nov 18, 2013 by Garima Jain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Python floats actually are implemented as C doubles. And they're not configurable.

However, Python also has a Decimal class, which (unlike floats) are actually decimal rather than binary, and include configurable precision. There is a performance hit -- prior to Python version 3.3, Decimal was
quite slow, but in 3.3 they got a major speed increase and are now nearly as fast as floats.

An example:

py> import decimal
py> x = decimal.Decimal(1)/3
py> decimal.getcontext().prec = 50
py> y = decimal.Decimal(1)/3
py> print(x, y)
0.3333333333333333333333333333 
0.33333333333333333333333333333333333333333333333333
answer Nov 18, 2013 by Naveena Garg
Similar Questions
+1 vote

Comparing floats to Fractions gives unexpected results:

# Python 3.3
py> from fractions import Fraction
py> 1/3 == Fraction(1, 3)
False

but:

py> 1/3 == float(Fraction(1, 3))
True

I expected that float-to-Fraction comparisons would convert the Fraction to a float, but apparently they do the opposite: they convert the float to a Fraction:

py> Fraction(1/3)
Fraction(6004799503160661, 18014398509481984)

Am I the only one who is surprised by this? Is there a general rule for which way numeric coercions should go when doing such comparisons?

0 votes

I'm writing some scripts with python, and I found sometimes, when I try to use time.sleep(1) to sleep 1 sec, it would actually sleep for 9/10 secs or even longer.

From python document, I saw this:

time.sleep(secs)
....
"Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system."

So, my question: under what kind of condition, time.sleep would suspend longer time than expected?

0 votes

Trivial question, please help.

I need to convert a string literal like "111.25" to floating point number.
atof() fails me as it returns 1111 and discards .25?

+3 votes

See the following code

void main(){
   float a=5.2;
  if(a==5.2)
     printf("Equal");
  else if(a<5.2)
     printf("Less than");
  else
     printf("Greater than"); 
}

Expected output is equal but we does not get same why?

...