# Example NumPy Floats

**URL:** <https://discuss.python.org/t/example-numpy-floats/23952>\
**Category:** Python Help\
**Tags:** help\
**Created:** [February 16, 2023, 5:00pm UTC](https://discuss.python.org/t/example-numpy-floats/23952 "2023-02-16T17:00:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paris2024](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@paris2024](https://discuss.python.org/u/paris2024)\
**Post date:** [February 16, 2023, 5:00pm UTC](https://discuss.python.org/t/example-numpy-floats/23952/1 "2023-02-16T17:00:23Z")

</div>

Can you help with with this please: Create a NumPy programme to generate all floats with a step of 0.5 between 0 and 15.

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [February 16, 2023, 5:06pm UTC](https://discuss.python.org/t/example-numpy-floats/23952/2 "2023-02-16T17:06:35Z")

</div>

Please post this in #users. This category is for discussing how to distribute Python projects.

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [February 16, 2023, 6:31pm UTC](https://discuss.python.org/t/example-numpy-floats/23952/3 "2023-02-16T18:31:03Z")

</div>

You need to write code before asking for help.

---

<div class="post-metadata">

**Author:** ![franklinvp](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/franklinvp/32/11142_2.png) [@franklinvp](https://discuss.python.org/u/franklinvp)\
**Post date:** [February 16, 2023, 9:03pm UTC](https://discuss.python.org/t/example-numpy-floats/23952/4 "2023-02-16T21:03:45Z")

</div>

You can add `0.5` to `0.0` while the result has not become larger than `15`.  
A first attempt can be

```auto
result = []
first = 0.0
last = 15.0
increment = 0.5
while current_value <= last:
  result.append(current_value)
  current_value += increment

```

Since we can predict how many values we will get, namely `int((15 - 0.0) / 0.5) + 1` we could also do

```auto
first = 0.0
last = 15.0
increment = 0.5
result = [first + n * increment for n in range(int((last - first)/increment) + 1)]

```

Note that `0.5` =2^{-1} and `0.0` are sums of powers of 2. So, all the values that we need are values that float can take. If they had given other value instead of `0.5` perhaps the values that we need are not values that `float` can take. So, we have to compute them approximatively. The first approach of doing `current_value += increment` can magnify the error in that case. So, it might be better to use the `first + n * increment` approach.

---

<div class="post-metadata">

**Author:** ![facelessuser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/facelessuser/32/1971_2.png) [@facelessuser](https://discuss.python.org/u/facelessuser)\
**Post date:** [February 16, 2023, 9:25pm UTC](https://discuss.python.org/t/example-numpy-floats/23952/5 "2023-02-16T21:25:20Z")

</div>

To be honest, numpy already as something built-in that can do this:

```auto
>>> list(np.arange(0.0, 15.5, 0.5))
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0]

```

But that may be cheating if it’s an assignment 🙃.

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [February 17, 2023, 6:30am UTC](https://discuss.python.org/t/example-numpy-floats/23952/6 "2023-02-17T06:30:00Z")

</div>

It is a numpy program, and clearly the most sensible one. A more sensible exercise would be to do the program in python without numpy.

Trying to answer questions about class assignments is often frustrating, especially when no code is given, because context is lacking.
