How to Assign Infinity to a Number in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

How to Assign Infinity to A Number in C++?

What are Infinity and Negative Infinity?

Infinity is a value that comes when a positive number is divided by an extremely small positive value, or it can be a very large value that could not be represented in 64 bits by our system. The positive or unsigned infinity value is defined in many libraries in C++, like cmath and limits, but there is no standard for defining the negative Infinity in C++. But still, there are many ways to represent negative Infinity in C++, which we will discuss further.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Use Negative of numeric_limits::infinity()

The numeric_limits<(data type)>:: Infinity () function in C++ is used to assign the infinity value to a variable in C++. It is present in the limits module of the C++ library. The implementation and explanation are given below.

Using Float data type

C++ Implementation

Output

The above code can be understood in the following way:

  • We have used the float data type variable to assign the value of Infinity.
  • The numeric_limits< float >:: Infinity () function assign the value of Infinity to the Inf variable.
  • After that, we took a variable of float data type named negative_Inf, and then we assigned it the value of Inf*-1, which means the negative of the Inf value, so in this way, we have assigned a negative infinity to a variable.
  • Finally, we have printed the values of both positive and negative Infinity.

Using double data type

C++ Implementation

Output

In above code can be understood by the following steps.

  • We have used the double data type variable to assign the value of Infinity.
  • The numeric_limits< double >:: Infinity () function assign the value of Infinity to the Inf variable.
  • After that, we took a variable of double data type named negative_Inf, and then we assigned it the value of Inf*-1, which means the negative of the Inf value, so in this way, we have assigned a negative infinity to a variable.
  • Finally, we have printed the values of both positive and negative Infinity.

Using Int data type

C++ Implementation

Output

In above code can be understood by the following steps.

  • We have used the variable of int data type to assign the value of Infinity.
  • The numeric_limits< int >:: Infinity () function works differently in the case of int data type. It assigns the value of 0 to the Inf variable in the case of positive Infinity, so we have added 1 to the value to show the sign of the Infinity.
  • After that, we have taken a variable of int data type named negative_Inf, and then we have assigned it the value of Inf*-1, which means the negative of the Inf value, so in this way, we have assigned a negative value to a variable.
  • Finally, we have printed the values of both positive and negative values.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Use Infinity from cmath Library

The INFINITY in C++ can also be used to assign the infinity value to a variable. It can only assign values to the float and double data types but not to the int data type. It is present in the cmath module of the C++ library. The implementation and explanation are given below.

Using Float data type

C++ Implementation

Output

In above code can be understood by the following steps.

  • We have used the variable of float data type to assign the value of Infinity.
  • The INFINITY assigns the value of Infinity to the Inf variable.
  • After that, we took a variable of float data type named negative_Inf, and then we assigned it the value of Inf*-1, which means the negative of the Inf value, so in this way, we have assigned a negative infinity to a variable.
  • Finally, we have printed the values of both positive and negative Infinity.

Using double data type

C++ Implementation

Output

In above code can be understood by the following steps.

  • We have used the variable of double data type to assign the value of Infinity.
  • The INFINITY assign the value of Infinity to the Inf variable.
  • After that, we took a variable of double data type named negative_Inf, and then we assigned it the value of Inf*-1, which means the negative of the Inf value, so in this way, we have assigned a negative infinity to a variable.
  • Finally, we have printed the values of both positive and negative Infinity.

Conclusion

In this quick tutorial, we have discussed how to assign Infinity in C++. We can extract the following conclusions from the article -

  • The syntax and working of C++ infinity for different data types
  • Using functions from limits and cmath libraries to assign the value of Infinity.