python - Why is numpy array's .tolist() creating long doubles? -
i have math operations produce numpy
array of results 8 significant figures. when use tolist()
on array y_axis
, creates assume 32-bit numbers.
however, wonder if garbage. assume is garbage, seems intelligent enough change last number rounding makes sense.
print "y_axis:",y_axis y_axis = y_axis.tolist() print "y_axis:",y_axis y_axis: [-0.99636686 0.08357361 -0.01638707] y_axis: [-0.9963668578012771, 0.08357361233570479, -0.01638706796138937]
so question is: if not garbage, using tolist
in accuracy calculations, or python using entire number, not displaying it?
when call print y_axis
on numpy array, getting truncated version of numbers numpy storing internally. way in truncated depends on how numpy's printing options set.
>>> arr = np.array([22/7, 1/13]) # init array >>> arr # np.array default printing array([ 3.14285714, 0.07692308]) >>> arr[0] # int default printing 3.1428571428571428 >>> np.set_printoptions(precision=24) # increase np.array print "precision" >>> arr # np.array high-"precision" print array([ 3.142857142857142793701541, 0.076923076923076927347012]) >>> float.hex(arr[0]) # actual underlying representation '0x1.9249249249249p+1'
the reason looks you're "gaining accuracy" when print out .tolist()
ed form of y_axis
default, more digits printed when call print
on list when call print
on numpy array.
in actuality, numbers stored internally either list or numpy array should identical (and should correspond last line above, generated float.hex(arr[0])
), since numpy uses numpy.float64
default, , python float
objects 64 bits default.
Comments
Post a Comment