calculating magnitude of a number in java
13,338
Solution 1
I believe you are looking for Math.abs(). Using this function:
5 = Math.abs(5) = Math.abs(-5)
Solution 2
Try Math.abs.
abs(-4) = 4;
abs(5) = 5;
Solution 3
Math.abs() is the function you are looking for.
Solution 4
The function's in the Math library and is called abs()
(short for "absolute value").
Related videos on Youtube
Author by
Ahmed
Updated on June 25, 2022Comments
-
Ahmed almost 2 years
I forgot the function in Java to calculate the magnitude of an integer. help please..
|5| = +5 = 5
|-5| = +5 = 5
so if a - b is - x
mag (a - b) is x a posstive number..
-
Dan W about 12 yearsAre you looking for Math.abs()?
-
Marc B about 12 yearsYou mean the abs() function, aka absolute value?
-
Alnitak about 12 yearsdamn, what's the appropriate close reason to use for "couldn't be bothered to check the API docs" ?
-
Benjamin R over 9 years@Alnitak For some people, because of their mathematical experience the regular term for "absolute value" is "magnitude", or sometimes you might be able to remember the term "magnitude" but not "absolute value". So, whilst some version of Math.abs() is present in just about every programming language, and "absolute value" might be easily found in the API docs, the term "magnitude" is not quite so. Therefore it's a valid question, it caused no pain or damage for anyone to answer it, and now this Q appears at the top of a Google search for 'java magnitude function'. Therefore net positive.
-
-
Benjamin R over 9 years+1 because you provided a code example to match the question. (and because you weren't snide to the Asker...)