public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Haley <aph@redhat.com>
To: Ricardo Telichevesky <ricardo@teli.org>, gcc-help@gcc.gnu.org
Subject: Re: Compiler warnings, overflow
Date: Thu, 31 Jul 2014 17:12:00 -0000	[thread overview]
Message-ID: <53DA78E1.2080803@redhat.com> (raw)
In-Reply-To: <53DA7643.2000004@teli.org>

On 07/31/2014 06:00 PM, Ricardo Telichevesky wrote:
> Hi, hope this is the right list.
> 
> Here is my code and output, at the bottom of the e-mail. y is
> "correct", w and z obviously have problems - multiplying two 32-bit
> integers "hoping" the result would be correct assigning to 64-bit -
> I guess it is the same problem as double oneThird= 1/3; the result
> being zero, and not 0.3333.
> 
> I was wondering if there is any strict warning that would flag the w
> and z assignments below, or the 1/3 above - the whole right hand
> side is evaluated as a 32-bit integer number, and assigned to a
> 64-bit integer or double. Not advocating this should be a default,
> but turning it on would help me detect some flaws in the code. Took
> me hours to catch a similar bug in my code, trying to solve a sparse
> system that has hundreds of millions of variables...

To quote the standard:

"A computation involving unsigned operands can never overflow, because
a result that cannot be represented by the resulting unsigned integer
type is reduced modulo the number that is one greater than the largest
value that can be represented by the resulting type."

So, from C's point of view there is nothing to warn about.

Andrew.

  reply	other threads:[~2014-07-31 17:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-31 17:00 Ricardo Telichevesky
2014-07-31 17:12 ` Andrew Haley [this message]
2014-07-31 17:53 ` Manuel López-Ibáñez
2014-08-01  8:53 ` David Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=53DA78E1.2080803@redhat.com \
    --to=aph@redhat.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=ricardo@teli.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).