Q:

Spark 1.5.2: NaN while calculating stddev

I am getting NaN while calculating standard deviation (stddev).  This is a very simple use case as described below:
 val df = Seq(("1",19603176695L),("2", 26438904194L),("3",29640527990L),("4",21034972928L),("5", 23975L)).toDF("v","data")

I have the stddev defined as an UDF: 
def stddev(col: Column) = {
        sqrt(mean(col*col) - mean(col)*mean(col))
 }

I'm getting NaN when I call the UDF as shown below:
df.agg(stddev(col("data")).as("stddev")).show() 

It produces the following: 
+------+
|stddev|
+------+
|   NaN|
+------+

What am I doing wrong? 

A:

Given your data, both mean(col*col) and mean(col)*mean(col) will be larger than a maximum value of Long.  You can try casting input columns to double first:
df.agg(stddev(col("data").cast("double")).as("stddev"))

but in general it won't be particularly stable on very large numbers.