C++ | Abseil Numeric Library














































C++ | Abseil Numeric Library



The Numeric Library

The numeric library of abseil contains only 128-bit integers (both signed and unsigned types), and hence contains only one header file currently, i.e. int128.h.

absl::uint128

uint128 type represents unsigned 128-bit  integer.

A few things to keep in mind while using uint128
  • It can't be implicitly converted to other integral types, including other 128-bit integral types, i.e. conversion must be explicit.
        For example : 
    absl::uint128 x;
    unint64_t i = x; // this conversion isn't possible
    uint64_t i = static_cast<uint64_t>(x); // this conversion works fine

  • Although, implicit conversion from integral type is possible.
        i.e. 
unint64_t a;
absl::uint128 b = a;
// this conversion works fine

  • The standard type traits  std::is_integral<uint128>::value and std::is_arithmetic<uint128>::value are false.

  • Since 128-bit integers literals are not yet part of C++ language, and hence to construct such integer, you should use the factory function absl::MakeUint128().


absl::int128

int128 type defines signed 128-bit  integer.

A few things to keep in mind while using int128

  • It can't be implicitly converted to other integral types, including other 128-bit integral types.
         For example : 
    absl::int128 x;
    int64_t i = x; // this conversion isn't possible
    int64_t i = static_cast<int64_t>(x); // this conversion works fine
  • Although, implicit conversion from integral type is possible.
        i.e. 
int64_t a;
absl::int128 b = a;
// this conversion works fine

  • The standard type traits  std::is_integral<int128>::value and std::is_arithmetic<int128>::value are false.

  • Since 128-bit integers literals are not yet part of C++ language, and hence to construct such integer, you should use the factory function absl::MakeInt128().
     
        Note : absl::MakeInt128(x, y) means 264 * x + y

Example : 
#include <iostream>

#include "absl/numeric/int128.h"

int main() {

absl::int128 a = absl::MakeInt128(
1, 0);

std::cout << "integer 2^64 is : " << std::endl;
std::cout << a << std::endl;

return 0;
}


Output : 
               



More Articles of Anuj Maurya:

Name Views Likes
C++ | Find smallest interval 222 0
C++ | Lexicographic rank of string 95 0
C++ | Counting Rooms (CSES) 92 0
C++ | Building Roads (CSES) 78 0
C++ | High Score (CSES) 199 0
C++ | Collect maximum coin in grid 75 0
C++ | Is this k-palindrome ? 75 0
C++ | Shuffle the array elements 64 0
C++ | Fizz Buzz 81 0
C++ | Check if given points form square 59 0
C++ | Klee Algorithm to find union of line segment 87 0
C++ | Check if points are collinear 75 0
C++ | Check if given point lies inside triangle 82 0
C++ | Check if two line segment intersect 78 0
C++ | Number of ways to decode message 74 0
C++ || Minimise the subset difference 60 0
C++ | Count of subsets with given sum 67 24
C++ | Currency Arbitrage 97 22
C++ | Estimating the value of π (pi) using Monte-Carlo Simulation 57 18
C++ | Find the first missing number 75 16
C++ | Finding word in grid using DFS 66 11
C++ | Implementing pow(x, y) in logarithmic time 52 9
C++ | Sum of all factors of number 81 10
C++ | Building Teams (CSES) 69 21
C++ | Positions of anagram 47 12
C++ | Integer factorization 55 13
C++ | Primality test 63 14
C++ | Subset Sum Problem 64 12
C++ | Civil Time in Abseil 66 19
C++ | Abseil Time Duration 119 15
C++ | absl::Time 92 12
C++ | Introduction to Abseil Time library 81 9
C++ | absl::StripPrefix() and absl::StripSuffix() 79 8
C++ | absl::ConsumePrefix() and absl::ConsumeSuffix() 92 19
C++ | absl::string_view 71 13
C++ | absl::Substitute() | Formatting string in abseil 84 15
C++ | absl::StrJoin() | Yet another way of joining strings 116 7
C++ | String concatenation in Abseil - 2 | absl::StrCat() 103 10
C++ | absl::StrAppend() | String concatenation in abseil - 1 78 16
C++ | absl::StrSplit() 149 15
C++ | Appending to string and Writing to stream in Abseil 86 11
C++ | absl::PrintF(), absl::FPrintF() and absl::SNPrintF() 96 0
C++ | Abseil StrFormat() 70 27
C++ | Abseil flags Library 123 348
C++ | Abseil Numeric Library 87 65
C++ | Abseil Hash library 81 0
C++ | Abseil Containers 139 46
C++ Introduction to Abseil 88 13
CSES | Shortest Route II 251 47
CSES | Shortest Routes I 187 1

Comments