Open In App

__builtin_inf() functions of GCC compiler

Improve
Improve
Like Article
Like
Save
Share
Report

These functions do not need to include any header file to use them. So it provides faster usage, as these are Built-in functions of GCC compiler which is most used compiler, even in competitive programming.

Note: If the compiler shows warning of overflow, use casting before __builtin_inf() function as 

(data_type)__builtin_inf()

1. __builtin_inf(void): This function returns positive infinity which will then be converted to DBL_MAX of C language limits.h header file Macro. Its return data type is double. 

Example: 

if __builtin_inf() is used
it returns infinite

Output: inf

Here is a C++ program that shows the use of this function:

C++




#include <iostream>
using namespace std;
 
int main()
{
    // infinite value (<b>inf</b>)
    double double_inf = __builtin_inf();
 
    cout << double_inf << endl;
    return 0;
}


Output: 

inf

 

2. __builtin_infd32(void): This function returns maximum value of 4-byte integer and returns data type is 32-bit integer.

Example: 

if __builtin_infd32() is used
it returns the maximum value of
the 32-bit integer

Output: 2147483647

Here is a C++ program which shows the use of this function: 

C++




#include <iostream>
using namespace std;
 
int main()
{
    // function returns highest signed integer value
    int int_inf = (int)__builtin_infd32();
 
    // you can use __builtin_inf() function
    // as well and then cast to integer
    int int_inf2 = (int)__builtin_inf();
 
    cout << int_inf << endl;
    cout << int_inf2 << endl;
 
    // function returns highest unsigned integer value
    unsigned int unsinged_int_inf
        = (unsigned int)__builtin_infd32();
 
    // you can use __builtin_inf() function as well
    // and then cast to unsigned integer
    unsigned int unsinged_int_inf2
        = (unsigned int)__builtin_inf();
 
    cout << unsinged_int_inf << endl;
    cout << unsinged_int_inf2 << endl;
 
    return 0;
}


Output: 

2147483647
2147483647
4294967295
4294967295

 

3. __builtin_infd64(void): This function returns Highest value that can be represented by 8 byte integer which is highest value of integer we can use in C++.

Example: 

if __builtin_infd64() is used
it returns the maximum value of
the 64-bit integer

Output: 9223372036854775807

Here is a C++ program which shows the use of this function: 

C++




#include <iostream>
using namespace std;
 
int main()
{
    // highest signed 8 byte value
    long long int long_inf = (long long int)__builtin_infd64();
 
    // you can use __builtin_inf() as well
    // and then cast to long long int
    long long int long_inf2
        = (long long int)__builtin_inf();
 
    cout << long_inf << endl;
    cout << long_inf2 << endl;
 
    // highest unsigned 8 byte value
    unsigned long long int unsigned_longlong_inf
        = (unsigned long long int)__builtin_infd64();
 
    // you can use __builtin_inf() as well
    // and then cast to unsigned long long int
    unsigned long long int unsigned_longlong_inf2
        = (unsigned long long int)__builtin_inf();
 
    cout << unsigned_longlong_inf << endl;
    cout << unsigned_longlong_inf2 << endl;
    return 0;
}


Output: 

9223372036854775807
9223372036854775807
18446744073709551615
18446744073709551615

 

4. __builtin_infd128(void): As the maximum size that can be used for integer is 64-bit in C/C++, this function give the same result as __builtin_infd64().

Example: 

if __builtin_infd128() is used
it returns the maximum value of
the 64-bit integer

Output: 9223372036854775807

5. __builtin_inff(void): This function returns float maximum value means 4-byte maximum decimal point value.

Example: 

if __builtin_inff() is used
it returns the maximum value of
the 32-bit float

Output: inf

Here is a C++ program which shows the use of this function: 

C++




#include <iostream>
using namespace std;
 
int main()
{
    float float_inf = __builtin_inff();
 
    // you can use __builtin_inf() as well
    // and then cast it to float data type
    float float_inf2 = (float)__builtin_inf();
 
    cout << float_inf << endl;
    cout << float_inf2 << endl;
    return 0;
}


Output: 

inf
inf

 

6. __builtin_infl(void): This function returns maximum long double data type value.

Example: 

if __builtin_infl() is used
it returns the maximum value of
the long double type

Output: inf

Here is a C++ program which shows the use of this function: 

C++




#include <iostream>
using namespace std;
 
int main()
{
    long double long_double_inf = __builtin_infl();
 
    // you can use __builtin_inf() as well
    // and then cast it to float data type
    long double long_double_inf2
        = (long double)__builtin_inf();
 
    cout << long_double_inf << endl;
    cout << long_double_inf2 << endl;
    return 0;
}


Output: 

inf
inf

 

7. In the recent versions of GCC compiler, these are two new functions: __builtin_infn(void) & __builtin_infnx(void). One can replace n with 32 or 64 and it will return inf value respectively of 32-bit, 64-bit. 

Similar article: Builtin functions of GCC compiler
 



Last Updated : 02 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads