Skip to content

Commit

Permalink
lib: Improve type correctness in scale_and_offset.
Browse files Browse the repository at this point in the history
Avoid integer overflow when determining the range of a signed integer
type (e.g. INT_MAX - INT_MIN) in the scale_and_offset<>() functor by
taking advantage of the fact that the range for the corresponding
unsigned integer type is the same, e.g. (INT_MAX - INT_MIN) == (UINTMAX -
UINT_MIN).  The previous implementation relied on converting the
integer values to floating point values before performing the
subtraction, which introduced potential inaccuracies.
  • Loading branch information
ossama-othman committed May 1, 2024
1 parent bc581af commit 43234b0
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/marc/details/scale_and_offset.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @file details/scale_and_offset.h
*
* Copyright (C) 2017-2018 Ossama Othman
* Copyright (C) 2017-2018, 2024 Ossama Othman
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
Expand All @@ -14,6 +14,7 @@

#include <limits>
#include <cmath>
#include <type_traits>


namespace MaRC
Expand Down Expand Up @@ -90,13 +91,13 @@ namespace MaRC
"scale/offset calculation");

/*
Avoid integer overflow by performing a floating point
subtraction. No overflow will occur since we already
know:
T_max - T_lowest < std::numeric_limits<double>::max()
Avoid integer overflow when determining the range of a
signed integer type (e.g. INT_MAX - INT_MIN) by
taking advantage of the fact that the range for the
corresponding unsigned integer type is the same.
*/
constexpr double type_range =
static_cast<double>(T_max) - T_lowest;
constexpr auto type_range =
std::numeric_limits<std::make_unsigned_t<T>>::max();

double const data_range = max - min;

Expand Down

0 comments on commit 43234b0

Please sign in to comment.