How to Get the Sign of an Integer in Python?


Introduction

Python Integers are one of the primary data types, and are used for almost all major mathematical and logical operations. In Python, integers are zero,positive or negative whole numbers without a fractional part and having unlimited precision.They can be expressed in binary, octal and hexadecimal values. In this article, we will learn how to obtain the sign of an integer.

Methods Used

  • Using simple mathematical comparison with zero

  • Using copysign() function of math module

  • Using numpy.sign() function

  • Creating a method with abs() function

Method 1: Using Mathematical Comparison with Zero

We can use the basic mathematical definition of positive and negative numbers to find the sign of a given integer. We use a basic if-else structure to determine the sign of a number.

Syntax

if number>0 return 1
else if number<0 return -1
else return 0

In this method, we use a cascaded if-else-if structure to create a decisionmaking point at the point zero in the number line of integers. If the number input by the user is greater than zero, the program returns +1, if it is less than zero, the program returns -1 and if it’s zero, the program returns zero as the answer.

Algorithm

Step 1 − Take the user input for the integer for which sign is to be determined

Step 2 − Create an ‘if’ with the initial condition that if the input number is greater than zero, the function returns 1

Step 3 − Create a cascading ‘else-if’ condition which returns -1 if the input number is less than zero

Step 4 − Create a final ‘else’ condition where the function returns zero if the input number is zero

Example

def solution(number):
   if (number>0):
      return 1
   elif (number<0):
      return -1
   else:
      return 0

val =-2
sol = solution(val)
print(sol) 

Output

-1

Method 2: Using copysign() Function of Math Module

The copysign() function in the math module returns the value of the first argument with the sign of the second argument. Hence we use 1 in place of the first argument to find the sign of the input integer placed as the second argument. This function can handle values of integers,floating point numbers as well as positive and negative nan.

Syntax

return copysign(1,number)

To perform this operation, we need to call the copysign function. As arguments, we need to pass two numbers, from which the magnitude of the first number and the sign of the second number is returned by the function as a single number.

Algorithm

Step 1 − Import the math module

Step 2 − Take the input integer from the user for which sign is to be determined

Step 3 − Pass the integer as the second argument in the copysign() function, according to the given syntax

Example

import math
def solution(number):
   return int(math.copysign(1,number))
val = -2
sol = solution(val)
print(sol) 

Output

-1

Method 3: Using numpy.sign() Function

The numpy module provides a sign() function which can be used to determine the sign of an integer. This is pretty useful as there isn’t any sign() function by default in the math library of Python. It can be used on an entire array of integers to display the sign element-wise.

Syntax

numpy.sign(array [], out)

The syntax is pretty simple, it calls the sign function and passes two arguments. Only the first argument is compulsory, the second one is optional.The first argument passes the array of elements, the second argument is output array placed with the result.

Algorithm

Step 1 − Import the numpy module

Step 2 − Take input elements in the array for which sign(s) are required to be determined

Step 3 − Call the sign() function and pass the array containing the target elements

Step 4 − Display the output values i.e. the signs of the integers

Example

import numpy as user #importing numpy
array=[25, -25, 0] #input array
print("input array : ", array) #input array with elements
print("Check sign of array : ", user.sign(array)) #signs of elements

Output

input array: [25 -25 0]
Check sign of array: [1 -1 0]

Method 4: Creating a Method with abs() Function

We can use the abs() function to define a sign function which returns the sign of the integer. The abs() function returns the absolute value of a number.

Syntax

return x/abs(x) 

Here we divide the integer with its absolute value to find the sign of the integer. As we divide two integers here, the function returns a float value, however it works for integers.

Algorithm

Step 1 − Define a function

Step 2 − Pass the number to the function whose sign is to be determined as an argument

Step 3 − Create an ‘if-else’ structure to check if input integer is zero

Step 4 − Display the output value by dividing the integer with its absolute value

Example

def int_sign(x):
   if (x==0): return 0
   else: return x/abs(x)
num=-2
print(int_sign(num))    

Output

-1.0

Conclusion

In this article, we have talked about some ways in which we can find the sign of an integer supplied by the user or set in the program. This article is usually written keeping Python in mind, however similar methods are also available in different programming languages.

Updated on: 24-Mar-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements