On Jun 17, 2005, at 4:21 PM, Benjamin Franksen wrote:
Regarding Jeff's argument as for the advantages of programming with
numbers that are garanteed to be non-negative. I would argue that this
is also mostly valid in C/C++, where arrays are always indexed starting
with zero. Many languages allow upper /and/ lower index bounds to be
arbitrary (signed) integers, or even any other data type, provided the
programmer can specify a one-to-one mapping onto a bounded interval of
integers. Thus, non-negativity seems to be a somewhat arbitrary
guarantee (why not, for instance, strict positivity?). Furthermore,
with regard to the efficiency question (only one range-check for upper
bound instead of two for upper and lower), in C/C++ you are always
free to apply the zero-cost type cast from signed to unsigned, thereby
mapping negative numbers to large positive ones, and then range-test
only for the upper bound. This will fail for /exactly/ the cases where
the original check failed, as long as you don't rely on the upper half
of the possible range, something you (Jeff) suggested is to be avoided
anyway.
The inefficiency is only in typing a few extra characters on your
keyboard. Any modern optimizing compiler is going to optimize
if ((i < 0) || (i >= j))
(where i and j are signed integer values) into a single test without
you needing to help it by performing the cast.
--
Eric Norum <[email protected]>
Advanced Photon Source
Argonne National Laboratory
(630) 252-4793
- Replies:
- RE: Fundamental Types document / unsigned integers Jeff Hill
- References:
- RE: Fundamental Types document Jeff Hill
- Re: Fundamental Types document Marty Kraimer
- Re: Fundamental Types document Ralph Lange
- Re: Fundamental Types document / unsigned integers Benjamin Franksen
- Navigate by Date:
- Prev:
Re: V4 Data Types: Request for tagged unions Benjamin Franksen
- Next:
Re: Fundamental Types document Benjamin Franksen
- Index:
2002
2003
2004
<2005>
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
- Navigate by Thread:
- Prev:
Re: Fundamental Types document / unsigned integers Benjamin Franksen
- Next:
RE: Fundamental Types document / unsigned integers Jeff Hill
- Index:
2002
2003
2004
<2005>
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
|