This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: why does g++ not warn about division by zero?


Hi Martin -

I believe what he was saying is that you will only see this if you do
NOT use that flag, and only when you perform integer division (note
that below, only integer/integer performs integer division since the
other operations cast the integer operand to a float).

  Brian

On Sun, Jun 21, 2009 at 7:13 AM, Martin Ettl<ettl.martin@gmx.de> wrote:
>
> Hello Axel,
>
> i played around with the suggested option ? -Wno-div-by-zero, but g++ does not warn about the divsion by zero. I tested the following:
>
> foo(4./0) ? // float/integer
> foo(4/0) ? ?// integer/integer
> foo(4/0.) ? // integer/float
> foo(4./0.) ?// float/float
>
> g++ does not warn about this cases. Here the extended testcase:
>
>
> #include <iostream>
>
> template <typename T1, typename T2> inline T1 foo (T1 const& a, T2 const& b)
> {
> ? ? ? ?// ....
> ? ?return a/b;
> }
>
> int main()
> {
> ? ? ? ?std::cout << foo(4.,0) << std::endl;
> ? ? ? ?std::cout << foo(4,0.) << std::endl;
> ? ? ? ?std::cout << foo(4,0) << std::endl;
> ? ? ? ?std::cout << foo(4.,0.) << std::endl;
> ? ? ? ?return 0;
> }
>
> Best regards
>
> Martin
>
>
> Hello Martin,
>
> the reason is given in the manual:
> ?-Wno-div-by-zero
> ? ? ? ? ? Do not warn about compile-time integer division by zero.
> ? ? ? ? ? Floating point division by zero is not warned about, as
> ? ? ? ? ? it can be a legitimate way of obtaining infinities and NaNs.
>
> So you should obtain a warning with "foo(4,0)", but not with "foo(4.,0)"
>
> Axel
>
>
> -------- Original-Nachricht --------
>> Datum: Sun, 21 Jun 2009 08:34:50 -0400
>> Von: John Fine <johnsfine@verizon.net>
>> An: Martin Ettl <ettl.martin@gmx.de>
>> CC: gcc-help@gcc.gnu.org
>> Betreff: Re: why does g++ not warn about division by zero?
>
>> Have you tried that at various optimization levels?
>>
>> At lower optimization levels, the "inline" keyword should not cause the
>> function to actually get inlined.
>>
>> If the function is not actually inlined, gcc cannot be aware of the
>> divide by zero.
>>
>> I don't happen to know whether gcc would warn if it were aware of the
>> divide by zero, nor if actually inlining is sufficient to make it
>> aware. ?I'm just saying actually inlining should be necessary to make it
>> aware of the divide by zero.
>>
>> Martin Ettl wrote:
>> > template <typename T1, typename T2> inline T1 foo (T1 const& a, T2
>> const& b)
>> > {
>> > ? ? // ....
>> > ? ? return a/b;
>> > }
>> >
>> > int main()
>> > {
>> > ? ? std::cout << foo(4.,0) << std::endl;
>> > ? ? return 0;
>> > }
>> >
>> > I compiled the testprogramm with following command:
>> > g++ -W -Wall -pedantic -o test test.cpp
>> >
>
> --
> GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate und Telefonanschluss
> für nur 17,95 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02
>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]