Skip to main content
Version: Java (Groovy)

Casting

Casting is one of the operators used to construct formulas.

Usage

(type) casting - Casts from one numeric primitive type handled by Java to another.

  • byte
  • short
  • int
  • long
  • float
  • double
note

Deephaven column expressions are Java expressions with some extra features, the rules of casting and numbers are consistent with Java.

Each number type has an allotted number of bytes used to store information. Depending on your data needs, consider the data type used in your application.

TypeBytesDescriptionExampleExample
byte1signed whole numbers-123123
short2signed whole numbers-30,00030,000
int4signed whole numbers-2,634,1232,634,123
long8signed whole numbers-8,293,193,4968,293,193,496
float4signed floating point numbers-8,293,193,496.29482938,293,193,496.2948293
double8signed floating point numbers-64,123,542,927,328,293,193,496.294829323164,123,542,927,328,293,193,496.2948293231

Example

Widening conversion

When operations are applied on a type of number that widen the type, the casting will automatically change.

In the following example, column A is assigned an integer row element in the source table. When operations are applied to that number that require more precision than integer, type allows the new columns to be casted to doubles.

source = emptyTable(10).update(
"A = (long) i",
"B = A * sqrt(2)",
"C = A / 2"
)

result = source.meta()

Manually casting

When writing queries, one might need to narrow the casting of the number type. The following example takes a number and reduces the bytes used to store that information. Since the bytes are truncated when narrowing the casting, spurious numbers will result if the number requires more bytes to hold the data.

The table below shows the minimum and maximum values for each data type.

caution

The boundary point of each number type might be assigned unexpected values, such as null or infinity. If the data is near these boundaries, use a type that allows for more storage.

numbersMax = newTable(
doubleCol("MaxNumbers",(2 - 1 / (2**52)) * (2**1023) ,(2 - 1 / (2**23)) * (2**127), (2**63) - 1, (2**31)-1, (2**15) - 1, (2**7) - 1),
).view(
"DoubleMax = (double) MaxNumbers",
"FloatMax = (float) MaxNumbers",
"LongMax = (long) MaxNumbers",
"IntMax = (int) MaxNumbers",
"ShortMax = (short) MaxNumbers",
"ByteMax = (byte) MaxNumbers",
).formatColumns("DoubleMax=Decimal(`0.0E0`)", "FloatMax=Decimal(`0.0E0`)", "LongMax=Decimal(`0.0E0`)")

numbersMin = newTable(
doubleCol("MinNumbers",1 / (2**1074), 1 / (2**149), -(2**63)+513, -(2**31)+2, -1*(2**15)+1, -(2**7)+1)
).view(
"DoubleMin = (double) MinNumbers",
"FloatMin = (float) MinNumbers",
"LongMin = (long) MinNumbers",
"IntMin = (int) MinNumbers",
"ShortMin = (short) MinNumbers ",
"ByteMin = (byte) MinNumbers ",
).formatColumns("DoubleMin=Decimal(`0.0E0`)", "FloatMin=Decimal(`0.0E0`)", "LongMin=Decimal(`0.0E0`)")

numbersMinMeta = numbersMin.meta().view("Name", "DataType")
numbersMaxMeta = numbersMax.meta().view("Name", "DataType")