MySQL does not support IEEE 754 infinity values (positive or negative infinity) for FLOAT or DOUBLE data types. This limitation affects arithmetic, insertion, and selection of special numeric values.
Arithmetic Behavior
Division operations that would produce infinity in IEEE 754 return NULL instead of Inf or NaN:
SELECT 1/0, 0/0;
-- Result: NULL, NULL
Insertion Behavior
Attempting to insert infinity string literals (e.g., 'Inf', '+Inf', '-Inf', or 'Infinity') typically results in 0.0 being stored. Depending on your SQL mode, this may trigger Error Code 1265: "Data truncated for column" or cause the INSERT to fail entirely in strict mode.
Selection Behavior
When selecting extremely large values (e.g., 1e+52), behavior is platform-dependent. Some platforms return inf/-inf, while others return 0/-0.
Recommended Workarounds
Since MySQL lacks native infinity support, use one of the following strategies:
- Large numbers: Use arbitrarily large values like
1e308 for positive infinity and -1e308 for negative infinity. - Language constants: Use application-level constants like
Double.MAX_VALUE or Double.MIN_VALUE. - NULL with flags: Represent infinity as
NULL and use an additional flag column to indicate if the NULL represents infinity. - Separate VARCHAR column: Store the numeric value in a numeric column (using
NULL for infinity) and store the text representation (e.g., 'Infinity') in a separate VARCHAR column. - Application layer conversion: Convert infinity values to/from alternative representations in your application code before storage and after retrieval.
Best Practices
- Use the
DECIMAL type if you require exact numeric values (though DECIMAL does not support special values like infinity). - Handle infinity cases at the application layer before sending data to MySQL.
- Clearly document how infinity is represented within your database schema.