April 23, 2024, 06:10:08 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


divide by zero... why?

Started by kryton9, October 03, 2006, 06:58:16 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

K

you use chairs as your example; divide 5 chairs by 0 and you still have 5 chairs

lets use dollars.

Profits are $100 and I have 100 shareholders so each share holder gets a $1.

But what if there is a flaw in my program and I plug in 0 for the number of shareholders.
With your scheme I sub 1 for the 0 and determine that each shareholder should get $100.

I pass that to the check printing program and generate $10,000 worth of checks!

So you say you can add code in my case to check for 0 but in your case you don't want to.

I wouldn't want to have to do it one way sometime and a different way other times.  That would make me more prone to miss a place I should have checked.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Kale

Quote from: kryton9 on October 04, 2006, 11:04:52 AM
I am saying in the real world, physical world I can say John, take that chair and divide it by zero, what do you get , you still get that one chair.

That's because our brains work at high level and have good exception handling. ;)

kryton9

I already conceded to the great posts, but just wanted to clarify
QuoteBut what if there is a flaw in my program and I plug in 0 for the number of shareholders.
With your scheme I sub 1 for the 0 and determine that each shareholder should get $100.

I pass that to the check printing program and generate $10,000 worth of checks!
If you entered 1 then only 1 shareholder would get $100 and the rest nothing, you wouldn't be out $10,000 :)

So I do read all the posts and great points. I agree and conceded and actually my original proposal was wrong in what a compiler could do and worked. And as I wrote in my concession, if a user wanted a zero to be a one, he could code it in anyways.

LarryMc

I would be out the $10,000 if it was a modular program and my module only handled seeing how much each shareholder was to get and the other module was by someone else who took care of the printing.

Both modules were suppose to go to the same place to get the number of shareholders.  The per share module got the wrong one but the print module got the right one.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryA

If you're going to make a case for substitution (and I'm NOT), at least advocate the "more correct" direction and substitute a very large number.  There is nothing even remotely correct about substituting 1... despite the equally flawed chair division logic.

I'll use a trigonometry example since that is where I've most often ran into the possibility of legitimate need (i.e. not an "error") for division by zero.

rise=0.0
run=2.0
angleD = atanD(rise/run) = 0ÂÃ,°

rise=2.0
run=1.0
angleD = atanD(2.0/1.0) = 63.435ÂÃ,°

rise=2.0
run=0.0
angleD = atanD(2.0/0.0) = atanD(infinity) = 90.000ÂÃ,° (correct, but computer won't do it)
angleD = atanD(2.0/0.0) = atanD( 1 ) = 45.000ÂÃ,° (substituting 1... very bad result)
angleD = atanD(2.0/0.0) = atanD( 99999 ) = 89.9994ÂÃ,° (substituting large number... approximately correct result)

I wouldn't advocate this though... unless an operation symbol other than "/" were used, clearly indicating the programmer's intent to accept approximate results, i.e.

angleD = atanD(2.0\\0.0) = 89.9994ÂÃ,°

It wouldn't be good enough in my field, but might be OK for non-critical  games or graphics use... even then it may cause more problems than it solves.

"Aerodynamics are for people who can't build engines." -- Enzo Ferrari
"Turbochargers were for people who can't build engines" -- Keith Duckworth

John S

October 05, 2006, 11:21:28 AM #30 Last Edit: October 05, 2006, 12:14:16 PM by John S
Quote from: LarryA on October 05, 2006, 08:51:19 AM
...I'll use a trigonometry example...
rise=0.0
run=2.0
angleD = atanD(rise/run) = 0ÂÃ,°
...

The programmer needs to test and prepare for this contingency and others


// Divide by Zero example
// compile as a console exe

global sub main()
{

float rise, run, AngleD;
rise = 10.0;

for run = 10.0; run >= 0; run--
{
     if run = 0.0
{AngleD = 90.0;}
else
{AngleD = ATanD(rise/run);}

     writeln ( "\nRun = " + str$(run, 2) + "    Angle = " + str$(AngleD, 2) );
}

while GetKey() = "";

return;
}

John Siino, Advanced Engineering Services and Software

LarryA

October 06, 2006, 04:46:05 AM #31 Last Edit: October 06, 2006, 04:51:31 AM by LarryA
Quote from: John S on October 05, 2006, 11:21:28 AM
The programmer needs to test and prepare for this contingency and others
Yes, I've incorporated that test and quadrant tests in a few languages over the last 30 years and so far no airplanes have crashed as far as I know.

Your example has rise fixed positive, but a general purpose test needs to handle 270ÂÃ,° (neg rise, zero run) also.

Code can get pretty badly cluttered with these tests, not to mention you get sick of inserting them, and they are sometimes a hidden bug if you do a typo... so I usually create alternative "safe" functions where the testing and divison actually take place...

angleD = atanDsafe(rise, run)

The (very large) program I'm working on right now (prototyped in IBpro/GDI, finale in VB.net/GDI+) has so much trig I think the source code would be doubled if tests weren't in alternative functions.
"Aerodynamics are for people who can't build engines." -- Enzo Ferrari
"Turbochargers were for people who can't build engines" -- Keith Duckworth