Numeric Limits in C++

Understanding these limitations is crucial for any programmer worth their salt (or caffeine). And trust me, it’s not as boring as you might think there are some pretty hilarious consequences when we push our numbers too far!

Before anything else: what exactly do I mean by “numeric limits”? Well, in C++, every data type has a maximum and minimum value that can be stored within its memory allocation. For example, the smallest possible number for an `int` is -2,147,483,648 (or 2^31), while the largest is 2,147,483,647 (or 2^31-1).

Now, you might be thinking: “Hey, that’s not so bad! I can handle those numbers just fine.” And to that, I say… good for you! But what happens when we start working with larger or smaller values? Let me show you.

Let’s take the `float` data type as an example. A `float` has a maximum value of 3.40282e+38 and a minimum value of -3.40282e+38 (or roughly 1.79E+308 for both). Sounds pretty reasonable, right?

Well… not exactly. Let’s say we want to calculate the square root of 2,147,483,647 using a `float`. Here’s what happens:

“`c++
// This script calculates the square root of 2,147,483,647 using a `float` data type, which has a maximum value of 3.40282e+38 and a minimum value of -3.40282e+38.

#include
#include // for sqrt() function
using namespace std;

int main() {
float x = 2147483647; // Declaring and initializing a variable x with the value 2147483647, which is the maximum value for a float data type
float y = sqrt(x); // Using the sqrt() function from the cmath library to calculate the square root of x and assigning it to the variable y
cout << "The square root of " << x << " is: " << y << endl; // Outputting the result of the calculation to the console return 0; // Returning 0 to indicate successful execution of the program }



When we run this program, the output looks like this:


code
// This script calculates the square root of 2147483647 and prints the result in scientific notation.

// Declare a variable to store the number whose square root will be calculated
let num = 2147483647;

// Calculate the square root using the Math.sqrt() method and store the result in a variable
let squareRoot = Math.sqrt(num);

// Print the result in scientific notation using the toExponential() method
console.log(“The square root of ” + num + ” is: ” + squareRoot.toExponential());
“`

Hmm… that’s not exactly what we were expecting! The actual answer should be around 46,339. But why did our `float` data type mess things up? Well, it turns out that the maximum value for a `float` is actually much smaller than you might think only about 7 digits of precision are available beyond the decimal point!

So what can we do to avoid these kinds of issues? Here are some tips:

1. Use appropriate data types for your needs (e.g., use `double` instead of `float` if you need more precision).
2. Avoid using floating-point numbers for calculations that require exact results (such as financial or scientific applications). Instead, consider using fixed-point arithmetic or integer math.
3. Be aware of the limitations and quirks of your chosen data types this will help you avoid unexpected errors and improve the performance of your code!

SICORPS