0
$\begingroup$

I am trying to generate a list of radii between two given points (0 and 1 in this example). Below is my code in Python to generate the list.

radii = []
num_of_annuli = 100
annuli_width = 1/num_of_annuli
radius_values = 0
for i in range (0, 100):
  radius_values = radius_values + annuli_width
  radii.append(radius_values)
print(radii)

Unfortunately, the above code doesn't work. See the output below.

[0.01, 0.02, 0.03, 0.04, 0.05, 0.060000000000000005, 0.07, 0.08, 0.09, 0.09999999999999999, 0.10999999999999999, 0.11999999999999998, 0.12999999999999998, 0.13999999999999999, 0.15, 0.16, 0.17, 0.18000000000000002, 0.19000000000000003, 0.20000000000000004, 0.21000000000000005, 0.22000000000000006, 0.23000000000000007, 0.24000000000000007, 0.25000000000000006, 0.26000000000000006, 0.2700000000000001, 0.2800000000000001, 0.2900000000000001, 0.3000000000000001, 0.3100000000000001, 0.3200000000000001, 0.3300000000000001, 0.34000000000000014, 0.35000000000000014, 0.36000000000000015, 0.37000000000000016, 0.38000000000000017, 0.3900000000000002, 0.4000000000000002, 0.4100000000000002, 0.4200000000000002, 0.4300000000000002, 0.4400000000000002, 0.45000000000000023, 0.46000000000000024, 0.47000000000000025, 0.48000000000000026, 0.49000000000000027, 0.5000000000000002, 0.5100000000000002, 0.5200000000000002, 0.5300000000000002, 0.5400000000000003, 0.5500000000000003, 0.5600000000000003, 0.5700000000000003, 0.5800000000000003, 0.5900000000000003, 0.6000000000000003, 0.6100000000000003, 0.6200000000000003, 0.6300000000000003, 0.6400000000000003, 0.6500000000000004, 0.6600000000000004, 0.6700000000000004, 0.6800000000000004, 0.6900000000000004, 0.7000000000000004, 0.7100000000000004, 0.7200000000000004, 0.7300000000000004, 0.7400000000000004, 0.7500000000000004, 0.7600000000000005, 0.7700000000000005, 0.7800000000000005, 0.7900000000000005, 0.8000000000000005, 0.8100000000000005, 0.8200000000000005, 0.8300000000000005, 0.8400000000000005, 0.8500000000000005, 0.8600000000000005, 0.8700000000000006, 0.8800000000000006, 0.8900000000000006, 0.9000000000000006, 0.9100000000000006, 0.9200000000000006, 0.9300000000000006, 0.9400000000000006, 0.9500000000000006, 0.9600000000000006, 0.9700000000000006, 0.9800000000000006, 0.9900000000000007, 1.0000000000000007]

The expected output is a list [0.01, 0.02, 0.03, 0.04...1.0] instead.

Anyone has an idea what am I doing wrong? If you have a better, more efficient code to do this job, please share that too.

$\endgroup$
3
  • 1
    $\begingroup$ This seems correct to machine precision. More accuracy could be obtained by summing the from smallest elements to largest $\endgroup$
    – whpowell96
    Commented May 29 at 20:39
  • 1
    $\begingroup$ You could also avoid the sum altogether if the annuli are all the same width as they are here: radii =[i*annuli_width for i in range(1,101)] $\endgroup$
    – Tyberius
    Commented May 30 at 1:12
  • $\begingroup$ You could also use numpy, radii = numpy.linspace(0, 1, num_of_annuli+1) $\endgroup$ Commented Jun 2 at 9:22

0

Browse other questions tagged or ask your own question.