Back to all reviewers

Type-safe numerical algorithms

deeplearning4j/deeplearning4j
Based on 2 comments
C++

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:

Algorithms C++

Reviewer Prompt

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:
// 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;
  1. Implement direct type conversions between data types rather than casting through intermediate types (especially floating point) to maintain precision:
// 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.

2
Comments Analyzed
C++
Primary Language
Algorithms
Category

Source Discussions