<!--
title: Type-safe numerical algorithms
domain: ml-systems
topic: Algorithms
language: C++
source: deeplearning4j/deeplearning4j
updated: 2018-06-27
url: https://awesomereviewers.com/reviewers/deeplearning4j-type-safe-numerical-algorithms/
-->

When implementing numerical algorithms, always use appropriate data types to prevent overflow and preserve precision:

1. Use `Nd4jLong` or `auto` instead of `int` for array indices, dimensions, and strides, especially when working with potentially large data structures:

```cpp
// Poor implementation - may cause overflow
const int iStride2 = iH * iW;
const int oStride2 = oH * oW;

// Better implementation
const Nd4jLong iStride2 = iH * iW;
const Nd4jLong oStride2 = oH * oW;
```

2. Implement direct type conversions between data types rather than casting through intermediate types (especially floating point) to maintain precision:

```cpp
// Problematic - may lose precision due to floating point limitations
z[i] = static_cast<T>(static_cast<float>(x[i]));

// Better - direct conversion preserves precision
z[i] = static_cast<T>(x[i]);
```

This is particularly important when converting between integer types of different sizes, as floating-point conversions can cause inaccuracies for large values. For specialized numeric types, implement proper conversion operators between them to ensure consistent and precise data handling.
