Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #81069

Re: Detect Math Errors in Functions

From Hope Rouselle <hrouselle@jevedi.com>
Newsgroups comp.lang.c++
Subject Re: Detect Math Errors in Functions
Date 2021-09-06 11:04 -0300
Organization Aioe.org NNTP Server
Message-ID <86zgspby1z.fsf@jevedi.com> (permalink)
References <hOudnfXpudHWjbL8nZ2dnUU7-KHNnZ2d@giganews.com> <s88uig9ljth6ueakm5lsgsh4rksmat3h5u@4ax.com> <sgnbcu$3v7$1@dont-email.me> <9o6dnYFV2J_wA7L8nZ2dnUU78XHNnZ2d@brightview.co.uk> <sgod26$pam$1@dont-email.me>

Show all headers | View raw


David Brown <david.brown@hesbynett.no> writes:

> On 01/09/2021 17:38, Mike Terry wrote:
>> On 01/09/2021 08:50, David Brown wrote:
>>> On 01/09/2021 08:54, Barry Schwarz wrote:
>>>> On Wed, 1 Sep 2021 07:31:53 +0200, Joseph Hesse <joeh@gmail.com>
>>>> wrote:
>>>>
>>>>> The famous unproven Collatz math conjecture says that if you start with
>>>>> any positive integer n and generate a sequence by repeatedly apply the
>>>>> rule that if n is even the next term is n/2, otherwise the next term is
>>>>> 3*n + 1, then eventually the sequence will reach 1.
>>>>>
>>>>> I wrote a C++ function that will generate the sequence from any
>>>>> positive
>>>>> starting integer, see below, header files omitted.  Since there is
>>>>> integer arithmetic involved, there might be a math overflow.  How does
>>>>> one write code to detect math underflow or overflow errors in functions
>>>>> that do math computations?
>>>>>
>>>> In this particular case, it seems to me to be easier to prevent the
>>>> overflow before it occurs rather than try to detect it after it
>>>> occurs.  You could replace the previous assignment with
>>>
>>> I agree with that.  I've always preferred the "look before you leap"
>>> approach, rather than the "close your eyes, make the jump and let
>>> someone else sweep up the mess" tactic.
>>>
>>> (The rest is to the OP, rather than Barry.)
>>>
>>> Another option is to use gcc's overflow-detecting builtins, or other
>>> equivalent extensions for other compilers.  If you are trying to do this
>>> checking as fast as possible, you might be looking at SIMD support
>>> through compiler vector extensions or processor-specific intrinsics.
>>>
>>> For unsigned types, you can of course just do "y = 3 * x + 1;" and check
>>> for "y < x", as the overflow is defined as wrapping.
>> 
>> That kind of test works for pure addition, but not when multiplication
>> is involved.
>> 
>> For example, mod 16 we have if x = 8, then y = 3*8 + 1 = 9, and y >= x.
>> So your test would fail, but there was overflow!  Barry's test is the
>> way I've gone in the past, or use some library class built for the
>> purpose of overflow detection.
>
> You are right, of course - sorry for posting without thinking here.

It's quite alright.  If you had thought a bit more perhaps I wouldn't
now have the references to the short documentaries on the Collatz
conjecture.  (I'm not working on the problem!  I swear.  Lol.  But it's
quite fun to talk about it.)

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Detect Math Errors in Functions Joseph Hesse <joeh@gmail.com> - 2021-09-01 07:31 +0200
  Re: Detect Math Errors in Functions Barry Schwarz <schwarzb@delq.com> - 2021-08-31 23:54 -0700
    Re: Detect Math Errors in Functions Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-01 09:30 +0200
      Re: Detect Math Errors in Functions Barry Schwarz <schwarzb@delq.com> - 2021-09-01 07:53 -0700
        Re: Detect Math Errors in Functions Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-01 17:50 +0200
        Re: Detect Math Errors in Functions James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-09-01 12:28 -0400
        Re: Detect Math Errors in Functions Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2021-09-01 17:38 +0100
    Re: Detect Math Errors in Functions David Brown <david.brown@hesbynett.no> - 2021-09-01 09:50 +0200
      Re: Detect Math Errors in Functions Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2021-09-01 16:38 +0100
        Re: Detect Math Errors in Functions David Brown <david.brown@hesbynett.no> - 2021-09-01 19:25 +0200
          Re: Detect Math Errors in Functions Hope Rouselle <hrouselle@jevedi.com> - 2021-09-06 11:04 -0300
      Re: Detect Math Errors in Functions Cholo Lennon <chololennon@hotmail.com> - 2021-09-06 00:20 -0300
        Re: Detect Math Errors in Functions "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-09-06 07:23 +0200
          Re: Detect Math Errors in Functions Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2021-09-06 20:34 -0400
  Re: Detect Math Errors in Functions Juha Nieminen <nospam@thanks.invalid> - 2021-09-01 18:22 +0000
  Re: Detect Math Errors in Functions Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-01 21:02 +0200
    Re: Detect Math Errors in Functions David Brown <david.brown@hesbynett.no> - 2021-09-02 11:53 +0200
  Re: Detect Math Errors in Functions Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-01 21:10 +0200
    Re: Detect Math Errors in Functions Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-02 05:31 +0200
      Re: Detect Math Errors in Functions Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-02 07:51 +0200
    Re: Detect Math Errors in Functions Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2021-09-06 20:47 -0400
      Re: Detect Math Errors in Functions red floyd <no.spam.here@its.invalid> - 2021-09-06 20:24 -0700
        Re: Detect Math Errors in Functions Juha Nieminen <nospam@thanks.invalid> - 2021-09-08 10:20 +0000
          Re: Detect Math Errors in Functions David Brown <david.brown@hesbynett.no> - 2021-09-08 13:17 +0200
            Re: Detect Math Errors in Functions Hope Rouselle <hrouselle@jevedi.com> - 2021-09-08 10:24 -0300
      Re: Detect Math Errors in Functions Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-07 09:10 +0200

csiph-web