## 14 Replies - 1305 Views - Last Post: 05 September 2009 - 01:00 AM

### #1

# Dividing numbers into Zero

Posted 04 September 2009 - 04:05 AM

and please show the possible output!!

thanks!!

##
**Replies To:** Dividing numbers into Zero

### #2

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 04:42 AM

Quote

Please post like this:

Thank you for helping us helping you.

Here's an idea though. Prompt the user to input a number. By dividing the number by any bigger numbers, you'll get an output of zero. Is that what you want?

This post has been edited by **AntonWebsters**: 04 September 2009 - 04:43 AM

### #3

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 04:45 AM

int devIntoZero(double toDiv) { return 0; }

Edit: Got method name completely wrong. I'm a failure.

This post has been edited by **syfran**: 04 September 2009 - 05:36 AM

### #4

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 05:08 AM

syfran, on 4 Sep, 2009 - 10:45 AM, said:

int devByZero(double toDiv) { return 0; }

Nice, but it asked about dividing _into_ zero

int devIntoZero(double myNutsAreHuge) { return 0; }

will give a far more accurate result

### #5

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 05:31 AM

syfran, on 4 Sep, 2009 - 03:45 AM, said:

int devByZero(double toDiv) { return 0; }

lol!

Back to topic

What is it that you want exactly. Any number divided by cero is cero.

There is another posible outcome but is not measurable wich could be infinite f3

I think you need to think about what you need.

### #6

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 06:01 AM

One is, you want to divide 0 by some number (IE: 0 / X), and in this case, the result will ALWAYS be zero, unless X is

also 0. On the other hand, you could be asking to divide a number by zero (IE: X / 0), then you get an undifined

result or infinity. Either way, what your trying to do will always return a constant result; either 0 or infinity/undifined.

Thanks,

Zach

### #7

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 06:08 AM

Quote

also 0.

Omg, need to revise my code... this shouldn't be that hard...

int divIntoZero(int toDiv) { return 1 ^ toDiv; // that works right? }

This post has been edited by **syfran**: 04 September 2009 - 06:12 AM

### #8

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 08:20 AM

### #9

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 10:57 AM

There is usually a reason you are dividing in the first place! Typically you are multiplying by the reciprocal of some solution. Such a You are taking the average of N items. But if there weren't any items because the list was empty then you're pre-handled response is my average is zero!

If you are normalizing a number by multiplying their products by their magnitude for each item or component then that can be easily pre-handled too!

Mag = sqrt( XX+YY+ZZ+WW ) nX = X / Mag nY = Y / Mag nZ = Z / Mag nW = W / Mag

But if the magnitude (result of pythagorean theroeom) is zero

they you're trying to divide by zero. (PLUS all those divides cost CPU time.) So instead....

Mag = sqrt( XX+YY+ZZ+WW ) // As Mag is approaching zero (NUMBER is really close to zero, leave the original number alone! if (Mag > 0.0001) { Mag = 1.0 / Mag nX = X * Mag nY = Y * Mag nZ = Z * Mag nW = W * Mag }

If you pre-handled the reason for the zero divisor before the division, for that particular case, you never need the divide by zero! Leaving it alone is the same as multiplying by one. The Algebraic Law of Multiplicative Identity. X1=X

You can think of it another way. As you are approaching a zero divisor, the result is approaching infinity, thus the result is negligible therefore has the same effect as if doing nothing.

Ex: 8 / 8 = 1 8/4=2 8/2=4 8/1=8 8/0.5=16 8/0.1=80 8/0.0001=80,000 8/0.0000001=80,000,000 etc. until the result is over the limit of the processor, thus infinite!

This post has been edited by **wildgoose**: 04 September 2009 - 11:05 AM

### #10

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 11:46 AM

### #11

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 01:30 PM

Division by zero is undefined mathematically and generally results errors when done in code: example:

int main(){ int a = 100; int b = 0; int c = a/b; //<-- crashes program printf("%d",c); return 0; }

This results in an Exception 0xc0000094 -- integer divide by zero on windows.

So if you have a program and you "might" divide by zero then you may want to do a check to ensure that you don't:

int c = a / (b !=0 ? b : 1); -- will divide by 1 rather than zero

or int c = (b==0 ? 0 : a/; -- will return 0 rather than doing the div by zero.

If you are using floating point values than you can divide by a very very small number rather than zero -- which may be a totally wrong answer but it will not crash your program.

### #12

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 03:04 PM

Quote

Yep!

I thought I was being clear, even with two examples, but I guess not!

"As X approaches zero it becomes an "asymptote'" <-- IamASexy, watch your spelling on 'terminology' when you can!

NickDMax, your solution was less verbose but more clear!

My kids hate it when I 'help' them with their homework as I mention things like Divide by Zero may mean "Undefined" to you now, but when you get to Calculus, it means "More Work!"

Admittedly, my pre-check solutions is in that "More Work" category!

This post has been edited by **wildgoose**: 04 September 2009 - 03:12 PM

### #13

## Re: Dividing numbers into Zero

Posted 04 September 2009 - 03:14 PM

### #14

## Re: Dividing numbers into Zero

Posted 05 September 2009 - 12:15 AM

ImaSexy, on 4 Sep, 2009 - 02:14 PM, said:

Me too (2nd time)... But otherwise, dividing by zero is either undefined or leads into negative or positive infinity, until you hit L'Hospitals Rule, in which case you can usually find an answer.

### #15

## Re: Dividing numbers into Zero

Posted 05 September 2009 - 01:00 AM

This is why the code we write should not be blindly written and expect to work. Error handling has to be incorporated into our code such as predictive results if a case of zero occurs! In most cases it can be pre-handled with special handling on a case by case basis. If you are dividing by zero, then either you have a programming error, or a case had been allowed to occur to reach a divide by zero operation.

I think enough said on this topic!