comparison liboctave/array/idx-vector.cc @ 31204:a81fad5c9fef

idx_vector: Fix wrong length calculation (bug #62968). * liboctave/array/idx-vector.cc: Fix wrong calculation for m_len in (start, limit, step) constructor to account for incomplete intervals. * liboctave/array/idx-vector.h: Add comment to (start, limit, step) constructor.
author Maged Rifaat <magedrifaat@gmail.com>
date Sat, 27 Aug 2022 13:41:44 +0200
parents 796f54d4ddbf
children e88a07dec498
comparison
equal deleted inserted replaced
31203:4c2b83516eb7 31204:a81fad5c9fef
101 101
102 idx_vector::idx_range_rep::idx_range_rep (octave_idx_type start, 102 idx_vector::idx_range_rep::idx_range_rep (octave_idx_type start,
103 octave_idx_type limit, 103 octave_idx_type limit,
104 octave_idx_type step) 104 octave_idx_type step)
105 : idx_base_rep (), m_start(start), 105 : idx_base_rep (), m_start(start),
106 m_len (step ? std::max ((limit - start) / step, 106 // Round length away from zero to catch incomplete intervals
107 static_cast<octave_idx_type> (0)) 107 m_len (step
108 ? std::max ((limit - start + step - (step > 0 ? 1 : -1)) / step,
109 static_cast<octave_idx_type> (0))
108 : -1), 110 : -1),
109 m_step (step) 111 m_step (step)
110 { 112 {
111 if (m_len < 0) 113 if (m_len < 0)
112 err_invalid_range (); 114 err_invalid_range ();