# How to exclude "inf(inite)" values when checking laser values?

**URL:** <https://get-help.theconstruct.ai/t/how-to-exclude-inf-inite-values-when-checking-laser-values/1762>\
**Category:** Python Basics For Robotics\
**Created:** [December 23, 2019, 5:42am UTC](https://get-help.theconstruct.ai/t/how-to-exclude-inf-inite-values-when-checking-laser-values/1762 "2019-12-23T05:42:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave.m.mckay](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/dave.m.mckay/32/10_2.png) [@dave.m.mckay](https://get-help.theconstruct.ai/u/dave.m.mckay)\
**Post date:** [December 23, 2019, 5:42am UTC](https://get-help.theconstruct.ai/t/how-to-exclude-inf-inite-values-when-checking-laser-values/1762/1 "2019-12-23T05:42:51Z")

</div>

I am an IT admin learning python/ROS and when i hit this script I came up with the following:

```
from robot_control_class import RobotControl

rc = RobotControl()

laserlist = rc.get_laser_full()
biggest = float(0)
spud = laserlist[0]

for laser in laserlist:
    if laser != spud:
        distance = float(laser)
        if biggest < distance:
            biggest = distance
            print("new longer distance is: " + str(biggest))
        else:
            print(str(laser) + "Was less than existing distance: " + str(biggest))
    else:
        print("no distance found")

print("The biggest distance found was: "+ str(biggest))

```

I couldn’t figure out how to test for ‘INF’ without finding a potato or ‘spud’ laser with a bad value and using that as a !=.

When I looked further on an saw the MUCH simpler result I ran the example code feeling very silly… and it came back as “('The higher value in the list is: ', inf)” which, while mechanically true is not useful.  
I reset my environment to default and got"The biggest distance found was: 2.92194676399" on my own test and got an inf from the provided code.

Did i miss something about putting the bot in a box of walls?

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [December 23, 2019, 10:01am UTC](https://get-help.theconstruct.ai/t/how-to-exclude-inf-inite-values-when-checking-laser-values/1762/2 "2019-12-23T10:01:07Z")

</div>

Hi @dave.m.mckay,

You’re right, you need to consider the `inf` value, which represents points where the laser sensor is not seeing anything beyond 30 meters (the range of the sensor). Actually the biggest technically.

However, the biggest “useful” concrete distance is actually the biggest number that is not an `inf`, like you said.

* * *

By the way, welcome to the community!

---

<div class="post-metadata">

**Author:** ![dave.m.mckay](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/dave.m.mckay/32/10_2.png) [@dave.m.mckay](https://get-help.theconstruct.ai/u/dave.m.mckay)\
**Post date:** [December 30, 2019, 10:05am UTC](https://get-help.theconstruct.ai/t/how-to-exclude-inf-inite-values-when-checking-laser-values/1762/3 "2019-12-30T10:05:49Z")

</div>

Thanks Bayodesegun. I am considering subscribing to the Robot Ignite materials and so far so good!

I figured out thats where the value was coming from,'that is why I made the spud value. The problem I had was how do exclude this as a valid value without finding a known version first? float(laser) = ínf didnt seem to do anything.

Even the example code has the same problem of returning the biggest inf result, which as described above is correct, but not the desired information for this part of the course.

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [January 2, 2020, 10:41am UTC](https://get-help.theconstruct.ai/t/how-to-exclude-inf-inite-values-when-checking-laser-values/1762/4 "2020-01-02T10:41:40Z")

</div>

Hi @dave.m.mckay, you can try the following to replace all `inf`s with zero.

```auto
from numpy import array, inf
#...
laserlist = array(laserlist)
laserlist[laserlist == inf] = 0

```

---

<div class="post-metadata">

**Author:** ![ralves](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/ralves/32/7039_2.png) [@ralves](https://get-help.theconstruct.ai/u/ralves)\
**Post date:** [January 27, 2020, 10:17pm UTC](https://get-help.theconstruct.ai/t/how-to-exclude-inf-inite-values-when-checking-laser-values/1762/5 "2020-01-27T22:17:44Z")

</div>

Hi @dave.m.mckay,

A good way of ignoring laser values with infinite is checking whether the laser value is `inf`.  
In your code below:

```
distance = float(laser)
if biggest < distance:
    biggest = distance

```

this could be achieved with (note `and distance != inf`):

```
inf = float('inf')

```

…

```
distance = float(laser)
if biggest < distance and distance != inf:
    biggest = distance

```

This should solve the problem.

Please let us know in case you have more questions.

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [March 30, 2020, 8:12am UTC](https://get-help.theconstruct.ai/t/how-to-exclude-inf-inite-values-when-checking-laser-values/1762/6 "2020-03-30T08:12:07Z")

</div>

A post was split to a new topic: [Python tests are graded as incorrect - please help!](https://get-help.theconstruct.ai/t/python-tests-are-graded-as-incorrect-please-help/2827)

---

<div class="post-metadata">

**Author:** ![david](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/david/32/10_2.png) [@david](https://get-help.theconstruct.ai/u/david)\
**Post date:** [April 19, 2020, 5:50pm UTC](https://get-help.theconstruct.ai/t/how-to-exclude-inf-inite-values-when-checking-laser-values/1762/7 "2020-04-19T17:50:43Z")

</div>

HI, going further on that, I want just to get the distance value which is not infinite…

```
from robot_control_class import RobotControl 

ob1 = RobotControl()

distances = ob1.get_laser_full()

max_value = 0

for ite in distances:

    if (ite > max_value) and (ite != inf) :

        max_value = ite

print (max_value)

I got an error :slight_smile:
Traceback (most recent call last):
  File "test_for.py", line 10, in <module>
    if (ite > max_value) and (ite != inf) :
NameError: name 'inf' is not defined

```

What’s wrong?

---

<div class="post-metadata">

**Author:** ![david](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/david/32/10_2.png) [@david](https://get-help.theconstruct.ai/u/david)\
**Post date:** [April 19, 2020, 6:00pm UTC](https://get-help.theconstruct.ai/t/how-to-exclude-inf-inite-values-when-checking-laser-values/1762/8 "2020-04-19T18:00:39Z")

</div>

HI, I understood that inf is a string, so I just changed by:

```
from robot_control_class import RobotControl 

ob1 = RobotControl()

distances = ob1.get_laser_full()

max_value = 0

for ite in distances:

    

    if (ite > max_value) and (str(ite) != "inf") :

        max_value = ite

print (max_value)
```
