Be a Supporter!

c++ 0 divided by 0 crashes prog?!

  • 595 Views
  • 13 Replies
New Topic Respond to this Topic
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
c++ 0 divided by 0 crashes prog?! 2005-07-17 02:55:05 Reply

just with a simple console program i tried this:

#include <iostream>

using namespace std;

int main()
{
cout << (0/10) << endl;
cout << (0/5) << endl;
cout << (0/0) << endl;
system("PAUSE");
return 0;
}

in the console i get:
0
0

and then it crashes saying it has encountered a problem and needs to close??? wtf
i might understand this happening if you did 10/0 (which devc++ gives me a warning for) but 0/0 = 0 not undefined like 10/0


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-17 02:58:12 Reply

or atleast thats what ive been taught in maths that 0/0 = 0


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
White-Rhyno
White-Rhyno
  • Member since: Apr. 28, 2003
  • Offline.
Forum Stats
Member
Level 38
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-17 02:59:20 Reply

At 7/17/05 02:58 AM, dELta_Luca wrote: or atleast thats what ive been taught in maths that 0/0 = 0

What kind of retarded math teacher taught you that?

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-17 03:07:19 Reply

the reason that i brought this up is that according to wilkipedia - the natural exponention of a quaternion is exp(p) = exp(a)(cos(|u|)+sgn(u)sin(|u|))

where the quaternion 'p' = (a,u) with u being a vector
and sgn(p) = p divided by |p| or the unit quaternion

yet, like with complex numbers if you discard the other values as 0's then the complex and quaternion number acts exactly like a normal number in all of its functions and operations such that

(10,0,0,0) + (20,0,0,0) = 10+20 and (10,0,0,0)*(5,0,0,0) = 5*10 = 10*5 (wheras normally quaternion multiplication is non-commutive)

yet following wilkipedias formula for the exp() of a quaternion - if you try and find it with a quaternion of (10,0,0,0) - i need up getting this - (-1.#IND, -1.#IND, -1.#IND, -1.#IND)
the only reason i can think of this is because of the sgn(u) part which is finding the unit vector - except that the vector is 0,0,0 and that vector divided by its length 0 - is returning the #IND part


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-17 03:09:32 Reply

At 7/17/05 02:59 AM, White_Rhyno wrote:
At 7/17/05 02:58 AM, dELta_Luca wrote: or atleast thats what ive been taught in maths that 0/0 = 0
What kind of retarded math teacher taught you that?

well thats what ive been taught ever since i started using division in math and since im still only in the 9th year and havnt even started my GCSE's yet (end of school exams) and that alot of kids in my class dont understand trigonemtry still i guess my teachers have never thought to TRY and teach us any different


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-17 03:53:13 Reply

ah well, i managed to figure my way around it -

Quad qexp ()
{
double m = v.mod();
m = m == 0 ? 1 : m;
Vector l = v*(1/m);
Quad ex (exp(s)+cos(m), l*sin(m));
return (ex);
}

works fine even with a quaternion of (10,0,0,0) which returns (22027,0,0,0) (accuracte enough as exp(10) = 22026.5)

does anyone know how you can be more accuracte than a double - like a double double or sumtin


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-17 04:01:22 Reply

ah wait, i figure out why it wasnt perfectly accuracte with the exp() of the number - the formula is exp(a) times the rest - i was doing it as an add to the cos() -

Quad cexp ()
{
double m = v.mod();
double m2 = m == 0 ? 1 : m;
Vector l = v*(1/m2);
double exps = exp(s);
Quad ex (exps*cos(m), l*(sin(m)*exps));
return (ex);
}

returns a perfect exponential

exp 120 = 1.30418e+52
exp (120,0,0,0) = (1.30418e+52,0,0,0)


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
PONGpaddle
PONGpaddle
  • Member since: Sep. 23, 2003
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-17 06:16:01 Reply

0/0 can't be calculated by processors. Doing it in any language will crash the program (unless you have an exception handler). Only God knows how you "got around it".

And GCSEs Delta? I'm starting them next term too. Except, I'll be finishing off my A-Level at the same time :)

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-17 12:37:40 Reply

well by 'got around it' i mean i just added an exception handler which in the case of the vector having a length of 0 - it just leaves it as it is instead of dividing each element by the length


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
DarkJeroyd2000
DarkJeroyd2000
  • Member since: Oct. 3, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-17 12:46:49 Reply

Thanks, dELta_Luca for helping me make a moving image. Here is what I made which is also on ny avatar.

c++ 0 divided by 0 crashes prog?!

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-17 14:18:19 Reply

actually anything/0 = infinity, deviding by 0 is a mathematical nono, C will crash doing it... and for a good reason

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-17 14:31:29 Reply

i thought a positive number /0 = infinity and a negative number divided by 0 = -infinity?


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Ravens-Grin
Ravens-Grin
  • Member since: Jun. 3, 2003
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-18 01:41:44 Reply

At 7/17/05 02:31 PM, dELta_Luca wrote: i thought a positive number /0 = infinity and a negative number divided by 0 = -infinity?

That is true, but 0/0 is definetely an indeterminate number. I'm going to assume everyone knows what a limit is....

Let's say we have two functions...
f(x)= 5x/x
g(x)= 2x/x

Now let's determine the limit of each function as it approaches zero from the left and right... At zero, both will equal the fraction of 0/0, but what do the limits look like?

For function f it's going to be 5, and for function g it's going to be 2. Yes this function does indeed have removable discontinuity, but all functions that end up with a value of 0/0 have removable discontinuity. If you have gone through calculus, you know that little trick, plug in the value that the function is approaching. If it's an irrational number with a zero in the denominator, then you know that it is approaching either infinity or -infinity, depending on the signs. But, if you plug in the value and it's 0/0, it's a clue for you to check for removable discontinuity.
Yes this function

CronoMan
CronoMan
  • Member since: Jul. 19, 2004
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to c++ 0 divided by 0 crashes prog?! 2005-07-18 06:42:25 Reply

Dividing anything by 0 in C++ will cause a crash, whether you like it or not.
Dividing 0/6 is legal, 6/0 is not legal.


"no sound in ass"