#1,098 – Floating Point Overflow

When you perform an arithmetic operation on a floating point value and the result has a magnitude that is too large to be represented by the floating point data type, an overflow condition occurs.  This is equivalent to trying to store a value whose exponent is larger than the maximum allowed exponent value.

If the result of the calculation is greater than the maximum representable positive floating point value, the result of the calculation is +Infinity.  If the result of the calculation is less than the largest representable negative floating point value, the result of the calculation is -Infinity.

            // Create a big floating point number
            float bigNumber = float.MaxValue;
            Console.WriteLine(bigNumber);  // 3.402823E+38

            // Adding small number doesn't change large number
            float newBig = bigNumber + 1.0f;
            Console.WriteLine(newBig);     // 3.402823E+38

            // But doubling the original number leads to overflow
            float reallyBig = bigNumber * 2.0f;
            Console.WriteLine(reallyBig);

1098-001

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #1,098 – Floating Point Overflow

  1. Pingback: Dew Drop – May 16, 2014 (#1778) | Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: