Need some people with C/C++ compilers to try a line of code for me...

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
cout << 1/0.0 << endl;

A friend just IMed me saying this printed out 'inf' on his computer. He says he's using gcc. My position is that his compiler has a line of code in it that traps for division like this instead of giving him the seg fault he so richly deserves.
 

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
I get 1.#INF when I run it on gcc (dev-c++). However, if I do a similar piece of code using C, it crashes:

#include <stdio.h>

int main()
{
int x = 0;
int y = 5;
printf("%d\n",y/x);
return 0;
}
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0
When you do floating point division by zero, you will get the 1.#inf When you do integer division by zero, you crash.
 

geoff2k

Golden Member
Sep 2, 2000
1,929
0
76

The C++ compiler in MS Visual Studio 6.0 refuses to even make an executable:

#include <iostream>
using namespace std;

void main()
{
cout << 1 / 0.0 << endl;
}

c:\>cl /GX z.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

z.cpp
z.cpp(6) : error C2124: divide or mod by zero
 

Ameesh

Lifer
Apr 3, 2001
23,686
0
0
Originally posted by: geoff2k
The C++ compiler in MS Visual Studio 6.0 refuses to even make an executable:

#include <iostream>
using namespace std;

void main()
{
cout << 1 / 0.0 << endl;
}

c:\>cl /GX z.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

z.cpp
z.cpp(6) : error C2124: divide or mod by zero

w00t!
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
Originally posted by: Ameesh
Originally posted by: geoff2k
The C++ compiler in MS Visual Studio 6.0 refuses to even make an executable:

#include <iostream>
using namespace std;

void main()
{
cout << 1 / 0.0 << endl;
}

c:\>cl /GX z.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

z.cpp
z.cpp(6) : error C2124: divide or mod by zero

w00t!

That's some interesting output you got when you tried the line, Ameesh. What compiler are you using?
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0
Visual C++ 6.0 will give you that error, but there are slick ways around it. As in:

float monkey = 1.0;
int mouse = 0;
monkey /=mouse;
return monkey;

edit:

For those not in the know, monkey /=mouse; is the equivalent of monkey = monkey/mouse;
What the code given above translates into is 1.0/0 which is floating point division. It should be no surprise, then, that you get 1.#INF
The same would be true for 1.0/0.0, 1/0.0, (float)1/0, etc.
Also, even though your program won't crash if this happens, it will likely give you unexpected results elsewhere, and it most definitely will slow your program to a crawl if you're doing lots of calculations with those numbers, coming up with an answer of 1.#INF is more time consuming than just about any other floating point operation.
 

geoff2k

Golden Member
Sep 2, 2000
1,929
0
76
Visual C++ 6.0 will give you that error, but there are slick ways around it. As in:

float monkey = 1.0;
int mouse = 0;
monkey /=mouse;
return monkey;

I suspect that some form of optimizer is what is actually catching the "1/0.0" in:

cout << 1 / 0.0 << endl;

since it probably tries to reduce any source code expressions with constants in it to the result of the expression to save doing the actual calculation at run time.

Perhaps if you made monkey and mouse const's it would catch it as well (or maybe not).
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0
Using consts does not give an error, but a warning:

Compiling...
main.cpp
D:\Desktop\programs\test\main.cpp(12) : warning C4723: potential divide by 0
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: KingNothing
cout << 1/0.0 << endl;

A friend just IMed me saying this printed out 'inf' on his computer. He says he's using gcc. My position is that his compiler has a line of code in it that traps for division like this instead of giving him the seg fault he so richly deserves.

You should not get any kind of errors for divide by zero when dealing with non-integers. That is the way it is supposed to work. The same is, of course, not true for integer division.