When might abs( ) be useful?


When might abs( ) be useful?


There are times when we need to know the magnitude of a number, regardless of whether it’s positive or negative. This magnitude is simply how far it is from zero, as you know, and that’s what this built-in function helps us get.

Some examples where a magnitude may come in handy are:

  1. Real-life problems involving distance from a point, because axes have positive and negative values and your direction can affect the sign, but your distance from the starting point should be positive.
  2. Checking how fast a car is going relative to the speed limit, since going faster would be a positive amount over the limit, and going slower would be a negative amount. Too much in either direction can be dangerous!