Divide by zero

Compiler option. Project preference, and pragmas in code to switch on/off.
If possible, I’m not against people opting for unsafe code if they know what they are doing.

1 Like

I did create a case for this just last week:

68144 - Raise an exception when dividing by 0

3 Likes

[quote=“Paul_Lefebvre, post:22, topic:69484”]
I did create a case for this just last week:

68144 - Raise an exception when dividing by 0
[/quote

Thanks @Paul_Lefebvre , at least now it’s under consideration. And I agree with @Kem_Tekinay: “I’d argue that old code that is dividing by zero is already broken, and this change would merely expose the existing bug.”

5 Likes

:+1:t2:

I just want to add that in that case you said Swift does not trap div by zero. That’s what I get with Swift 5 and 5.1, both terminated:

:point_up:t2: The last case the 5.1 compiler detects div by zero at compile time and cancels

Thanks for that. It’s possible I was looking at an older version of Swift.

1 Like

That’s not how division works. 0 is not the number of cuts, but the number of desired portions. How many portions of 0 cake can you get? Infinity.

In order not to break existing code it would need to be disabled by default and enabled like this:

#pragma EnableDivZeroException

Virtually all of my Xojo programming is very math intensive, and I’ve never felt the need for a divide by zero exception. In fact, the IEEE method of generating and propagating NaN’s is in my opinion a far better way of detecting math errors, but it applies only to floating point numbers.

2 Likes

Exactly. A floating divide by zero should yield the IEEE codes (but we do need ways to check values to see if a variable in fact contains one of these). But an integer divide by zero can’t be signaled in the variable contents, so it needs to raise an exception. Also, coercing an infinite or NaN vale to an integer should be an exception.

6=12/2
50%=100% / 2
or
50%+50%=100% ← my 2 pieces of cake

My cake is 100, I will loop until all my parts of cake reach 100 :
Loop until a = 100
a = a + 0
End loop

It will loop indefinitely

1 Like

Yes, that is correct. What’s your point? I like the loop idea that @Thomas_ROBISSON made. To elaborate:

Var Total As Integer = 12
Var Result As Integer
Const Divisor = 2
While Total > 0
  Total = Total - Divisor
  Result = Result + 1
Wend

Result will be 6. Now set to the divisor to 0:

Var Total As Integer = 12
Var Result As Integer
Const Divisor = 0
While Total > 0
  Total = Total - Divisor
  Result = Result + 1
Wend

Total will remain 12 because nothing is ever removed. It is an infinite loop. Result is infinity, not 0.

One thing I really like about this example is that code after the loop won’t execute, further demonstrating the problems with dividing by zero. This is why most languages will throw an exception. There’s basically no other logical way to handle the scenario.

(Also, this loop won’t give correct answers when the divisor is something that doesn’t divide 12 evenly. That’s fine, this loop isn’t intended to be a replacement for division.)

from my point of view
100%=100% / 0 ← no divide, its untouched, i do not need a error here
50%=100% / 2
25%=100% / 4

Your point of view is irrelevant. Math is math and doesn’t give a damn about your point of view.

100 / 2 = 50
100 / 1 = 100
100 / 0 = Also 100? WTF?

You’re confusing dividing by 1 with dividing by 0.

6 Likes

So you are telling us that a construct like this:

image

is not a fraction?

No, it can NOT be 0 as a result of such calculation (nor a large positive or negative integer as Xojo does when doing int math). It can be a zero IF you desire trapping math errors and ASSUMING zero because you want and know what you are doing.

Markus, this is standard. No need to reinvent behaviors or distort how math works.

sure
in math you can divide a cake by 0.5 and you will have 2 :exploding_head:

Markus, division is the inverse of the multiplication. Can you get what am I saying?

You think that is unnatural 1 / (1/2) = 2, but probably don’t care when 2 * 1/2 = 1

You have to be trolling me at this point. So are you suggesting that multiplication doesn’t exist, simply because I can’t multiply a cake by 4 to have 4 cakes? You also can’t divide a cake by 6,000,000,000 because then you just have dust. Can you still call it a cake? OMG, math is a lie isn’t it? What if it’s all just cake?

Let’s tone it down and get back on topic.

2 Likes

No Kem, we will loop until Markus agree with us ! :slight_smile: :smiley: .

I’m not on my computer then I can’t test. But if I well understood under Xojo a = 3 / 0 does not generate an error ? If a is an integer or a double it should as it is supposed to contain a number, not a string or a boolean or a folderitem. The result of a number divided by 0 is not a number then couldn’t be stored in a variable supposed to contain number.

Variables supports values. Floating point values can be numbers but also some “special values” that aren’t “numbers”. +Infinity, -Infinity and for all invalid operations a “Not a number” NaN. Floats as Doubles have special bits/constructs to carry such information.

For x = y / 0, x is not a number, it can be NaN, +Inf or -Inf depending on y. And values as NaN and Inf have no way to be stored in a Integer scalar. And that’s why such expressions rises exceptions in most current languages. If you move its bits to ints, it will result in any valid random number causing confusion and being used for mistaken calculations.