Be wary of using logarithms for this computation, notably for large integers close to boundaries of orders of magnitude. See this, for example: for exponent in range(11, 20): print(exponent, 10 ** exponent - 1, magnitude_order(10 ** exponent - 1)) Output: 11 99999999999 10 12 999999999999 11 โ€ฆ Answer from Quercus on discuss.python.org
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ finding-magnitude-of-a-complex-number-in-python
Finding Magnitude of a Complex Number in Python - GeeksforGeeks
July 23, 2025 - We can manually calculate the magnitude by taking the square root of the sum of the squares of the real and imaginary parts. ... #Manually calculating magnitude def magnitude_manual(z): return (z.real**2 + z.imag**2)**0.5 # Example complex_number ...
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-absolute-value-a-quick-tutorial
Python Absolute Value: A Quick Tutorial | DataCamp
April 11, 2024 - Python offers an easy, built-in function for performing this task, the abs(). The absolute value function allows Python programmers to obtain the magnitude of a number, regardless of its sign, essentially making the number positive.
Top answer
1 of 3
4

Late reply, but the other answer and the linked duplicate question seems to have missed an important aspect of your question, namely that you want the value, not the absolute value, with the largest magnitude. In other words, in a list like [-10, 0, 5], you want the result to be -10, not 10.

To get the value with its original sign, you could (conceptually) follow these steps:

  1. Get a copy of your list where all values were converted to their absolute values
  2. Find the index of the value that has the maximum absolute value
  3. Return the value at that index

This can be done quite easily in numpy by using argmax, which returns the index of the maximum value from an array. Step-by-step, it would look like this:

# Create a numpy array from the list you're searching:
xs = np.array([-10, 0, 5])

# Get an array where each value is converted to its absolute value:
xs_abs = np.abs(xs) 
# This gives [10, 0, 5]

# Get the index of the highest absolute value:
max_index = np.argmax(xs_abs) 
# This gives 0

# Get the number from the original array at the max index:
x = xs[max_index] 
# This gives -10

This can be done in a single line like so:

x = xs[np.argmax(np.abs(xs))]
2 of 3
1

If you mean highest magnitude regardless of negative or positive sign, then you would take the maximum of the list resulting from taking the absolute value of each constituent list value. Does that make sense?

Here is an example:
numbers = [3, 5, 7, -18]

matrix=[]
for x in numbers:
    matrix.append(abs(x))
max(matrix)
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
A function for calculating magnitude order of a number - Page 2 - Python Help - Discussions on Python.org
September 13, 2022 - What about a function for calculating magnitude order, maybe in math module? Something like this: def magnitude_order(num): if num == 0: return 0 absnum = abs(num) order = math.log10(absnum) reโ€ฆ
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.absolute.html
numpy.absolute โ€” NumPy v2.5.dev0 Manual
An ndarray containing the absolute value of each element in x. For complex input, a + ib, the absolute value is \(\sqrt{ a^2 + b^2 }\).
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-absolute-value-python-abs-tutorial
Python Absolute Value โ€“ Python abs Tutorial
June 20, 2022 - You add j to the end of a real number, like so: 1j or 4j.So, an example of a complex number in Python is 2 + 4j or 1 + 2j. Now, when it comes to the return value of the abs() function: For integer numbers, the abs() function returns the absolute ...
๐ŸŒ
LearnDataSci
learndatasci.com โ€บ solutions โ€บ python-absolute-value
Python Absolute Value โ€“ abs() for real and complex numbers โ€“ LearnDataSci
The absolute value of a number refers to that value's magnitude, regardless of whether it is positive or negative. For example, the absolute value of -5 is 5. Below is the syntax for using the abs() function to determine the absolute value of a number in Python:
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_abs.asp
Python abs() Function
Remove List Duplicates Reverse ... Q&A Python Bootcamp Python Certificate Python Training ... The abs() function returns the absolute value of the specified number....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-calculate-absolute-value-in-Python
Python abs() function
May 20, 2025 - Absolute Value of an Integer: 34 Absolute Value of an Float: 154.32 ยท If we pass a complex number as an argument to this function, the return value will be the magnitude of this complex number.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-calculate-an-absolute-value-in-python
How to calculate an absolute value in Python
The abs() function in Python returns the absolute value of the given argument. This argument can be an int, a float, or a complex number. In case of complex numbers, abs() function returns the magnitude part only.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ python-abs-function
abs() in Python | Python abs() Function with Examples - Javatpoint
abs() in Python | Python abs() Function with Examples on Python, Built in, Functions, abs Function, all Function, bin Function, bool Function, python Functions, new sum Function, bytes Function, new callable Function etc.
๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ finding magnitude of a complex number in python
Finding Magnitude of a Complex Number in Python - CodeSpeedy
February 18, 2021 - The abs(number) returns : 1. Modulus of the number if it is an Integer or Floating point. 2. Magnitude if the number is Complex.
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python absolute value: a step-by-step guide
Python Absolute Value: A Step-By-Step Guide | Career Karma
December 1, 2023 - Absolute value is an important ... Absolute values are also often used in data analysis. The Python abs() method calculates the absolute value of a number....
๐ŸŒ
Linux Hint
linuxhint.com โ€บ python_absolute_value
Python Absolute Value
October 22, 2020 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
SciPy
docs.scipy.org โ€บ doc โ€บ numpy-1.13.0 โ€บ reference โ€บ generated โ€บ numpy.absolute.html
numpy.absolute โ€” NumPy v1.13 Manual
Calculate the absolute value element-wise ยท out : ndarray, None, or tuple of ndarray and None, optional
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ numpy โ€บ python numpy magnitude
NumPy Magnitude in Python | Delft Stack
January 30, 2023 - We can also use the numpy.dot() function along with the numpy.sqrt() function to calculate the magnitude of a vector in NumPy. The numpy.dot() function calculates the dot-product between two different vectors, and the numpy.sqrt() function is ...
Top answer
1 of 5
19

Your log10/floor code is perfectly readable, and its performance cost will likely be dwarfed by that of the string formatting you will subsequently be doing on your output.

However, suppose you were to really need the performance...

Note that log10(x) == log2(x) / log2(10) == log2(x) * 1/log2(10)

1/log2(10) is a constant

log2(x) can usually be performed cheaply in the integer pipeline on modern architectures using instructions such as CLZ or a bit twiddling hack, yielding a number between 0 and 63 for a 64-bit integer. That fits in 6 bits, leaving us up to 58 bits after the radix point usable for fixed point arithmetic in a 64-bit type.

So we can then use fixed-point arithmetic to find the log10:

unsigned long long integer_log10( unsigned long long _in )
{
    unsigned long long log10fp6x58 = 0x134413509f79ff0llu; // (unsigned long long) (double(1llu<<58) / log2(10.0))
    return (((integer_log2(_in)) * log10fp6x58)+(1llu<<57)) >> 58;
}

The implementation of integer_log2 is compiler/platform-dependent; e.g. on GCC/PowerPC, it's

unsigned long long integer_log2( unsigned long long _in )
{
    return 63 - __cntlzd(_in);
}

This approach can be generalised for finding the logarithm of any base, simply calculate the appropriate constant as described above.

2 of 5
2

This is the most straightforward and simple method i can think of ... and maybe it will be a bit faster than computing the logarithm:

postfixes = {{1e12, "T"},
             {1e9,  "G"},
             {1e6,  "M"},
             {1e3,  "K"}}

for each postfix in postfixes{
    if(x > postfix.value){
        return (x / postfix.value) + postfix.letter;
    }
}

return x;
๐ŸŒ
Socratic
socratic.org โ€บ questions โ€บ how-do-i-find-the-order-of-magnitude-of-a-number
How do I find the order of magnitude of a number?
October 4, 2015 - Or copy paragraphs, serial numbers, and more from an image, then paste it on your phone or your computer with Chrome. Stuck on a problem? Quickly find explainers, videos, and results from the web for math, history, chemistry, biology, physics, and more. Find out what plant is in your friend's apartment, or what kind of dog you saw in the park.