Browsing the blog archives for January, 2009.

Java – Not A Number Gotcha

Examples

java

There are always subtle nuances to every computer language. Recently, I found the following NaN (not a number) nuance in Java:

Bad:

if (value == Double.NaN)
    // Do something...

Good:

if (Double.isNaN(value))

    // Do something....
2 Comments

MySQL and Negative Infinity

Examples

mysql

Storing and retrieving negative infinity in a MySQL database is accomplished by inserting an arbitrarily large negative number.  Negative infinity is handy when storing default values in a database.

The following SQL statement is an example of inserting negative infinity:

INSERT INTO test(negative_infinity) VALUES('-1e500');

Here is the test table:

CREATE TABLE test (
     negative_infinity DOUBLE NOT NULL
) ENGINE=INNODB character set utf8;

The value in the database is stored as the maximum negative value for the data type, double (-1.79769313486232e+308).

mysql> select * from test;
+------------------------+
| negative_infinity      |
+------------------------+
| -1.79769313486232e+308 |
+------------------------+
1 row in set (0.00 sec)

In Java, the Double contsant for max infinity, directly correlates to the value stored by MySQL.

No Comments