When to use Math.Abs() .can u please explain me with example. Thank u. | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

When to use Math.Abs() .can u please explain me with example. Thank u.

10th Jan 2017, 2:46 PM
read mo
3 ответов
+ 2
Just return the absolute value of a numeric value ( only positive values acepted) : Sample from official doc // The example displays the following output: // Abs(79228162514264337593543950335) = 79228162514264337593543950335 // Abs(12.45) = 12.45 // Abs(0) = 0 // Abs(-19.69) = 19.69 // Abs(-79228162514264337593543950335) = 79228162514264337593543950335
10th Jan 2017, 2:54 PM
nextco
nextco - avatar
+ 1
// The following loop will work right, no matter if num > 0, or num < 0 int num = -5; for (int i = 0; i < Math.Abs(num); i++) { Console.WriteLine(i); }
10th Jan 2017, 3:15 PM
Boris Batinkov
Boris Batinkov - avatar
+ 1
Math.Abs() will generate the absolute value of a given term. In mathematics, absolute value is the non-negative value of a number without regard to its sign. In simple terms, how far the number is from zero, regardless of the directions. Thus, -5 and 5 and 5 away from 0. Therefore, their absolute value is 5.
11th Jan 2017, 4:30 AM
Imran Din
Imran Din - avatar