From f6028348037035b1930f4e1871b23e96c5717427 Mon Sep 17 00:00:00 2001
From: Lukas Cejka <lukas.ostatek@gmail.com>
Date: Sun, 18 Nov 2018 19:16:28 +0100
Subject: [PATCH] Fixed mistake with setSlice, ceil function used RealType and
 division, this would cause integer division at certain format declarations,
 now uses float only.

---
 src/TNL/Matrices/ChunkedEllpack_impl.h | 27 ++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/src/TNL/Matrices/ChunkedEllpack_impl.h b/src/TNL/Matrices/ChunkedEllpack_impl.h
index 20dbfa6834..9d5a0ddee0 100644
--- a/src/TNL/Matrices/ChunkedEllpack_impl.h
+++ b/src/TNL/Matrices/ChunkedEllpack_impl.h
@@ -179,10 +179,29 @@ bool ChunkedEllpack< Real, Device, Index >::setSlice( ConstCompressedRowLengthsV
     */
    IndexType maxChunkInSlice( 0 );
    for( IndexType i = sliceBegin; i < sliceEnd; i++ )
-      maxChunkInSlice = max( maxChunkInSlice,
-                          ceil( ( RealType ) rowLengths[ i ] /
-                                ( RealType ) this->rowToChunkMapping[ i ] ) );
-   TNL_ASSERT( maxChunkInSlice > 0,
+   {       
+//       ALL OF THE FOLLOWING std::couts are for troubleshooting purposes, can be deleted.
+//       std::cout << "Troubleshooting invalid ceil operation: " << std::endl;
+//       std::cout << "maxChunkInSlice = " << maxChunkInSlice << std::endl;
+//       std::cout << "( RealType ) rowLengths[ i ] = " <<  ( RealType ) rowLengths[ i ] << std::endl;
+//       std::cout << "( RealType ) this->rowToChunkMapping[ i ] = " <<  ( RealType ) this->rowToChunkMapping[ i ] << std::endl;
+//       std::cout << " ceil( RealType / RealType ) = " << ceil( ( RealType ) rowLengths[ i ] / ( RealType ) this->rowToChunkMapping[ i ] ) << std::endl;
+//       std::cout << "( int ) rowLengths[ i ] = " <<  ( int ) rowLengths[ i ] << std::endl;
+//       std::cout << "( int ) this->rowToChunkMapping[ i ] = " <<  ( int ) this->rowToChunkMapping[ i ] << std::endl;
+//       std::cout << " ceil( int / int ) = " << ceil( ( int ) rowLengths[ i ] / ( int ) this->rowToChunkMapping[ i ] ) << std::endl;
+//       std::cout << "( float ) rowLengths[ i ] = " <<  ( float ) rowLengths[ i ] << std::endl;
+//       std::cout << "( float ) this->rowToChunkMapping[ i ] = " <<  ( float ) this->rowToChunkMapping[ i ] << std::endl;
+//       std::cout << " ceil( float / float ) = " << ceil( ( float ) rowLengths[ i ] / ( float ) this->rowToChunkMapping[ i ] ) << std::endl;
+//       The ceil function doesn't work when rowLengths and the other this.->... is 
+//       typecasted into ( RealType ), because when RealType is int, it will perform 
+//       an integer division and return the int as a double, which in this case 
+//       will be zero and make the assertion fail ( https://stackoverflow.com/questions/33273359/in-c-using-the-ceil-a-division-is-not-working ).
+//       To fix this, typecast them to ( float ), instead of ( RealType )
+       maxChunkInSlice = max( maxChunkInSlice,
+                          ceil( ( float ) rowLengths[ i ] /
+                                ( float ) this->rowToChunkMapping[ i ] ) );
+   }
+      TNL_ASSERT( maxChunkInSlice > 0,
               std::cerr << " maxChunkInSlice = " << maxChunkInSlice << std::endl );
 
    /****
-- 
GitLab