views:

282

answers:

2

Newbie here. I have the following code:

myADC = 128
maxVoltage = 5.0
maxADC = 255.0
VoltsPerADC = maxVoltage/maxADC
myVolts = myADC * VoltsPerADC
print "myADC = {0: >3}".format(myADC)
print "VoltsPerADC =  {0: >7}".format(VoltsPerADC)
print VoltsPerADC
print "myVolts = {0: >7}".format(myVolts)
print myVolts

This outputs the following:

myADC = 128
VoltsPerADC =  0.0196078
0.0196078431373
myVolts =  2.5098
2.50980392157

I have been searching for an explanation of how the number of significant digits is determined by default, but have had trouble locating an explanation that makes sense to me. This link link text suggests that by default the "print" statement prints numbers to 10 significant figures, but that does not seem to be the case in my results.

How are the number of significant digits/precision determined?

Can someone shed some light on this for me.

Thanks in advance for your time and patience.

+3  A: 

The precision is determined by the hardware. Python uses hardware floats (actually doubles) for its floats. The implications are discussed in the tutorial: http://docs.python.org/tutorial/floatingpoint.html

If you want more control over precision and rounding, you should consider using the decimal module.

Thomas Wouters
A: 

In general, if the documentation isn't at python.org, it is not authoritative. If you look at print it will tell you that floating point numbers are first converted to strings and float will tell you (as Thomas noted) that the machine native double type is how floats are stored and that the language makes no guarantees about significant digits.

msw
Ahhh...so since print converts a float to a string and, according to the tutorial link Thomas provided, str() "Python’s built-in str() function produces only 12 significant digits...," this is wny the lines print VoltsPerADC print myVoltsoutput numbers with 12 digits. Am I understanding that part of this correctly?Why do the ",format" statements print different amounts of digits?Thanks, again. This is very helpful.
russ
I think you may be confusing the mathematical/scientific concept of "significant digits" with "floating point representation" (you aren't helped by the confusion of the terms in the tutorial). I expect that the '12 digits of str()' is chosen to strike a balance between truncating rounding errors and readability. If you are managing "mathematical" significant digits, control them with format. str(0.1) is probably more of a practical convenience than a meaningful length. See also http://docs.sun.com/source/806-3568/ncg_goldberg.html for way more detail.
msw