Author Topic: Division by Zero  (Read 13099 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Division by Zero
« on: November 10, 2021, 10:32:30 am »
So, as we all know, math isn't my strongest suit. Wondering if I could get your thoughts on if there may be a math formula to do something with division by zero. In my case I have multiple calculation which produce for me a trending value. In the calculations I often come to the point where there is zero trending (ie where the divisor is zero). Now the direction of the trend is also being followed and once the divisor gets to zero, in my routines negative trending is allowed. Therefore I have struggled with trying to determine if "0" is positive or negative and I'm using the trending to make that determination. So a falling trend at zero, the formula needs to product a negative value, whereas a trend that has been rising suddenly hits a zero, I want that same math formula to produce a positive value. That sounds so convoluted but I hope you can grasp the idea.

Anyway. I have never been able to find a math formula to do what I have just described, so I have forced values. Here is an idea of what I mean by forced the value

Code: QB64: [Select]
  1. a = 25
  2. b = 0
  3. c = a / b
  4. Print " 25/0 = "; c
  5. a = 25
  6. b = 0
  7. c = a / b
  8. If b = 0 Then c = -.001 ' value forced for a falling trend, would be +.001 for rising trend
  9. Print " 25/0 = "; c

By plugging a forced value I think I'm introducing some inaccuracies to my outputted values. Maybe there is a positive or negative number that I could use that is just 1 short of inf?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Division by Zero
« Reply #1 on: November 10, 2021, 11:11:04 am »
What is the thing that might be 0 that also may be used as a divisor?

Most people would program:
if b <> 0 then c = a/b else 'handle the case when b is 0. eg, c = a or c = 0 usually depends on what b is.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Division by Zero
« Reply #2 on: November 10, 2021, 05:33:37 pm »
It seems a little strange and a little unfair when you think about it... You can 'add' zero to anything as well as 'subtract' and even 'multiply', but not 'divide'? Oh well. 3 out of 4 ain't bad... lol
Logic is the beginning of wisdom.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Division by Zero
« Reply #3 on: November 11, 2021, 09:04:11 am »
I agree with you @johnno56 . The value "0" is clearly a void which should mean "no number either positive or negative". A division by zero should be the same value as the numerator. 25/0 = 25 seems to make sense to me. How it becomes INF suggests we are trying to divide by an infinite number of values. I realize there is an apparent identical value if the divisor is 1 ie 25/1 = 25, which on the surface seems to be the same but there is a difference. If we accept that a division by zero is the same thing as just a repeat of the numerator, whereas a division by 1 is the actual number of times the divisor went into the numerator. This becomes evident for example if the equation is -25/-1 is positive 25 whereas -25/0 is a negative 25. Very difficult to work with a result of INF.

Thanks you guys for your input. I think I'm going to trap for a division by zero and then see what happens if I use a division by zero to mean its a repeat of the immediate previous result.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Division by Zero
« Reply #4 on: November 11, 2021, 09:58:21 am »
If I didn't like INF, I'd give division by 0 the max number per data type.

If data_type is byte, a / 0 = SGN(A) * 127.  (127 or -127)
If data_type is integer, a / 0 =    SGN(A) * 32767 (32867 or -32767)
And on for other data types...

If I was graphing the points on a map, this would show an extreme blip off into infinity.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Division by Zero
« Reply #5 on: November 11, 2021, 02:01:17 pm »
@Dimster

Quote
Therefore I have struggled with trying to determine if "0" is positive or negative and I'm using the trending to make that determination. So a falling trend at zero, the formula needs to product a negative value, whereas a trend that has been rising suddenly hits a zero, I want that same math formula to produce a positive value. That sounds so convoluted but I hope you can grasp the idea.

To calculate the trend - whether it is declining or rising. Please try to specify. Namely, if you have, say, 1000 values, then you get a different trend for the whole range and a different trend for comparing, for example, the last 100 values. What you want to compare it with is also important. Is the default value for trend determination the maximum value from the set of all compared elements for a given period, or is it related to some specific value? I'll give an example.

Let's say you have 900 values ​​from 900 to zero and then another hundred values ​​from zero to hundred. If you want to determine a trend for all thousand numbers, the maximum is 1000 and 999 other values ​​are less than a thousand, so this is a declining trend.

What happens if you compare only the last hundred values? The last hundred values ​​are from zero to a hundred, as we agreed at the beginning of this example. Then it can be said that this is a growing trend, because in the last values ​​99 values ​​are lower than the last value.

If we add another 100 values ​​of the same size to these 100 values ​​and we make a trend from these 100 values, we will find that the 100 values ​​are just the same size and it is stagnation. That's how I look at it.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Division by Zero
« Reply #6 on: November 11, 2021, 04:19:22 pm »
Hi @Petr.. My trending is more a movement towards and away from Zero. Away from zero being positive and toward zero being negative. If there are more than 1 negative movements then a negative trend is forming, whereas more than 1 positive is a positive trend forming. So my conundrum is how to interpret zero. The math formula I'm using comes up with INF as a result when the data is hitting zero in the trending values.

Ideally I should take a INF result as meaning perfect balance and therefore no sign ( ie + or -) can be applied to the resultant of the math formula but the reality is that the events being monitor all have potential to occur. Because they have potential they can never have a perfect zero value therefore I'm trying to decide if "0" is a positive number or negative. I guess it would be similar to that situation where you walk towards a wall but only take 1/2 the distance of the previous step. You theoretically can never reach the wall but always have potential to do so.

Or Petr how do you decide if a variable is in a negative group or a positive group ( is x <= O  the negative group or x < 0 the negative group, while x =>0 the positive group, or x > 0 is the positive group). Seems you have to decide here if O is positive or negative.

In my situation, I have been plugging in a positive value as close to zero as I dared. If the math calculated to INF then I used .000001 as the denominator. In effect conceding that I put zero in the positive group. But thinking more on what trending could mean and what INF could mean in terms of my trending, I'm looking more at it as a stall or a repeat in the trend. I'm going to go with capturing the last math result and just use it again every time the math comes up with INF result.

Boy I didn't mean to go on this long with my reply. Way too much thinking in coding.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Division by Zero
« Reply #7 on: November 11, 2021, 04:45:22 pm »
Alright let me shake this thread up. Who can calculate the value of:

sin(x) / x

at x=0.

I see a handful of different answers coming from this thread, but only one is correct. If you ponder this example enough, you may feel empowered to try division by zero in other contexts.
You're not done when it works, you're done when it's right.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Division by Zero
« Reply #8 on: November 11, 2021, 11:35:37 pm »
ok, weird stuff coming:

division by 0

 first off, zero has NO multiplicative inverse.  which is why a CPU would fail it.

Another interesting division fact, is that dividing by smaller and smaller numbers gives you bigger and bigger answers
Example:
10/2 = 5
10/1 = 10
10/.5 = 20
10/.4 = 25
etc...

as the divisor(?) reaches zero - the answers  head toward infinity

so you would think that 10 / 0 = ∞

multiplicative rule:

0 x ∞ = 1
so
(0 x ∞) + (0 x ∞) = 2
this reduces down to
(0 x ∞) = 2
But this should = 1,  sounds like that stupid school math LOL
so mathematically: 1 = 2 would be correct, if you can prove the above (but not in the real world)

found some more info on this
https://en.wikipedia.org/wiki/Riemann_sphere
how to make division by 0 work. LOL

enjoy




hmmmm

I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Division by Zero
« Reply #9 on: November 12, 2021, 08:55:50 am »
But consider this Example as well:
-10/2 = -5
-10/1 = -10
-10/.5 = -20
-10/.4 = -25
etc...

as the divisor(?) reaches zero - the answers  head toward -infinity

so you would think that -10 / 0 = -∞

Now move 10, -10 closer and closer together and you get -infinitiy adjacent to positive infinity at 0, so I think mathematicians have it right who say division by 0 is undefined.

So that' why I say in real world for practical questions you have to consider the thing you are dividing by and define the situation for what works for your case.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Division by Zero
« Reply #10 on: November 12, 2021, 09:42:11 am »
So how does the infinite universe of negative numbers fit into all of this? If zero is both positive and negative at the same time then, theoretically we can have an equation:

+0 / -0

which would then give you a multiplicative inverse of -0 / +0

All math formulas result in either a positive or negative number. Infinity should be able to be grouped as negative infinity and positive infinity, which therefore should lead to a negative zero and a positive zero. Zero should be banned from the infinite universe as it is not a number. It can not take on a + or - sign and therefore should not exist.

On the other hand it should be treated as a negative value because it has negative connotations to it ... he's a zero .... you have zero money in your bank account .... you have lost zero weight in your diet ... etc, etc...

@STxAxTIC .. the answer is + NAN

@xra7en ... interesting stuff ( a lot over my head ) thanks for it all.

@bplus ... I like how you came up with negative infinity. I'm thinking infinity itself implies all conceivable numbers which would include negative numbers but if we lobby for the separation then at the very least a division by zero in qb64 should at least result in a -INF or +INF. Would help me a lot in my trending decisions.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Division by Zero
« Reply #11 on: November 12, 2021, 10:37:56 am »
Alright so...



Stuff like
Code: [Select]
0 x ∞ = 1
so
(0 x ∞) + (0 x ∞) = 2
this reduces down to
(0 x ∞) = 2
But this should = 1,  sounds like that stupid school math LOL

is something that I hope every curious person writes down at least once in their life... But it's super wrong.



As for the positive/negative infinity example you guys are fleshing out, I think it's summed up in a picture shamelessly screenshotted from my notes:

  [ You are not allowed to view this attachment ]

The take-away here is you always need to specify from which direction you are approaching a limit. From the left, you get one answer, from the right, you get another answer. And that's all there is to say about that.



As for the sin(x) / x problem, I accidentally said the answer above. It's "one":

  [ You are not allowed to view this attachment ]  



What's the real real take-away here? An ounce of mathematics is better than 4 tons of code!


You're not done when it works, you're done when it's right.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Division by Zero
« Reply #12 on: November 12, 2021, 11:03:55 am »
@STxAxTIC - I did not read your notes on this issue before starting this thread, sorry about that.
If i'm following the examples correctly, there is a difference between "sin" and "sinc". It is the "sinc" formula which is giving the distinct negative/positive infinities. It appears sinc is the plugging in of a forced value "x". And this is exactly how I had been treating my INF results. I would just force a value for the denominator. But I was still having a worry that I needed to know if the plugged value should be + or - . Does "sinc" have a different outcome if x is negative? Would the charts be the same however just changing from the lower left/upper right quadrants to upper left/lower right?


Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Division by Zero
« Reply #13 on: November 12, 2021, 01:12:02 pm »
Lim(x->0) sin(x)/x

Bill (STxAxTIC) naturally gives us the mathematician's answer & proof.  An engineer does it with a spreadsheet:
  [ You are not allowed to view this attachment ]  

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Division by Zero
« Reply #14 on: November 12, 2021, 05:03:46 pm »
Dimster - Seems like you want to be searching wikipedia for "even vs. odd functions" (they can do better with pictures than I can with words)... but lemme be honest: this is a total rabbit hole unless you're trying to step into calculus. Otherwise, I think the ready-fire-aim technique that works so brilliantly for shooting potatoes around the farm should get you by. (That is, just code and re-code until you get the behavior you want and explanations be damned.)

Qwerkey - When in doubt, use brute force! Nice number crunching there! I see the bottom results give what's expected.

----

Anyone (looking at you qwerkey, prolly bplus too) want another challenge?

  [ You are not allowed to view this attachment ]  

See that?         y = (n^x - 1) / x

What is the value of y at x=0?
You're not done when it works, you're done when it's right.