Skip to content

How to Resolve Error "CS0594: Floating-point constant is outside the range of type 'type'" in C#

Fix the C# compiler error CS0594 when a floating-point literal exceeds the maximum range of float (3.4×10³⁸) or double (1.7×10³⁰⁸), with solutions like upgrading to double or using PositiveInfinity.

The Compiler Error CS0594 is a numeric range error. The message reads: "Floating-point constant is outside the range of type 'float' (or 'double')".

In C#, floating-point types (float and double) adhere to the IEEE 754 standard, which dictates strict upper and lower limits on the magnitude of numbers they can store.

  • float (System.Single): Max value is approximately 3.4 × 10^38.
  • double (System.Double): Max value is approximately 1.7 × 10^308.

This error occurs when you write a numeric literal (a constant) that exceeds these boundaries. The compiler detects that the number is too large to be represented and refuses to compile it to avoid immediate overflow into "Infinity".

Understanding Floating-Point Limits

Unlike integers, which wrap around (overflow) in unchecked contexts, floating-point literals are checked strictly by the compiler.

Type Suffix Min Value (Approx) Max Value (Approx)
float f or F ±1.5 × 10^-45 ±3.4 × 10^38
double d or D (or none) ±5.0 × 10^-324 ±1.7 × 10^308

If you type a number like 4.0e38f (4 followed by 38 zeros), it exceeds the float maximum of 3.4e38f, triggering CS0594.

Scenario 1: Float Overflow (The 'f' Suffix)

The most common cause is defining a large number that fits inside a double (default for decimals in C#), but explicitly adding the f suffix to force it into a float.

Example of error:

public class PhysicsCalc
{
    // ⛔️ Error CS0594: Floating-point constant is outside the range of type 'float'.
    // 3.5 x 10^39 is larger than the max float (3.4 x 10^38).
    public const float MassiveEnergy = 3.5e39f; 
}

Scenario 2: Double Overflow

double is incredibly large, but it is not infinite. If you attempt to define a number larger than 1.79 x 10^308, even the double type cannot hold it.

Example of error:

public class MathConstants
{
    // ⛔️ Error CS0594: Constant is too large even for a double.
    // 10^310 exceeds the limit of 10^308.
    public const double WayTooBig = 1.0e310; 
}

Solution: Change Data Types or Use Infinity

Depending on your needs, you can either upgrade the container (from float to double) or, if you really intended to represent "Infinity," use the specific constant for it.

Fix 1: Upgrade to Double

If the number fits in a double, remove the f suffix and change the variable type.

public class PhysicsCalc
{
    // ✅ Correct: 'double' can easily hold 3.5e39.
    public const double MassiveEnergy = 3.5e39; 
}

Fix 2: Use PositiveInfinity

If the number is logically "infinite" or effectively out of bounds for the system, use the built-in constant.

public class MathConstants
{
    // ✅ Correct: Explicitly representing Infinity
    public const double WayTooBig = double.PositiveInfinity; 
}

Note on decimal

You might consider switching to decimal (m suffix). However, while decimal has higher precision (more decimal places), it has a smaller range than double.

  • decimal Max: approx 7.9 × 10^28.
  • If a number fails CS0594 for float (at 10^38), it will definitely not fit in decimal.

Conclusion

CS0594 prevents you from hardcoding numbers that the computer physically cannot store.

  1. Check the Suffix: Are you forcing a massive number into a float using f?
  2. Check the Magnitude:
    • If it is greater than 10^38, use double.
    • If it is greater than 10^308, you must use double.PositiveInfinity or a specialized BigFloat library.
Navigation