# Dividing by 0

This was a test I did after watching computerphile to see how the os/codea handles dividing by 0. The results are it treats 0 as infinity

-- Divide By 0
-- by codea forum user kirorp

-- this is a test to see how the os/codea handles dividing by 0

function setup()
a = 0
b = 5
print(b/a)
end

hmm…strange

That’s not strange at all. If you divide “b” by “a” and “a” gets smaller, the result gets bigger. So as “a” approaches 0, the result approaches infinity. That’s the way it’s supposed to be.

@dave1707 If you divide a by b, the result will be larger the smaller b is, except for 0. Dividing anything by 0 is 0. Infinity is the odd result.

@SkyTheCoder I didn’t say I was dividing anything by 0. I said if “b” / “a” and “a” approaches 0, the result approaches infinity. So it’s not strange for the the code to return “inf” when there is a division by 0. There are certain math operations that are undefined, so the math code that does those operations has to return something, the program can’t just blow up. So that’s why you get results like “nan” or “inf”, etc. It’s up to you to write your code to look for results like those, or your program will give unexpected results in those situations.

I usually clamp the value to 0.000001 so it looks like nothings happening, better than using conditional statements to make sure it’s over 0, otherwise it just crashes.

@SkyTheCoder

Dividing anything by 0 is 0

nope.
a/0 is infinite when a is anything but 0.
0/0 is undefined (there is not one single solution possible).
You’ll learn that later when you go to advanced math courses.

@Jmv38

a/0 is infinite when a is anything but 0.

Nope. Division is undefined when the denominator is 0.

(If you go to really advanced maths courses then you might just possibly learn about meadows where you are allowed to define the reciprocal of 0, and therefore to divide by 0. But meadows haven’t really made it into mainstream mathematics as yet.)

@LoopSpace that’s what I was taught

weird. I though dividing anything (but 0) by 0 would tend to infinity (in absolute value).

It doesn’t matter what anyone says when dividing by 0. If you’re using Codea, this is all you need to know, “inf”, “-inf”, “nan”.

function setup()
print(string.format("5/0     %f",5/0))
print(string.format("-5/0   %f",-5/0))
print(string.format("5/-0    %f",5/-0))
print(string.format("-5/-0  %f",-5/-0))

print(string.format("0/0     %f",0/0))
print(string.format("-0/0    %f",-0/0))
print(string.format("0/-0    %f",0/-0))
print(string.format("-0/-0   %f",-0/-0))
end

“Infinity”, in this context, is not a “thing”, and certainly not a number. We can’t say “a/0 = ∞” because ∞ is not allowed here. Only numbers are allowed.

There are ways to treat ∞ as a number, but then division isn’t allowed so even then the statement doesn’t make any sense.

The definitions of ±Inf and NaN in Codea are from the IEEE floating point standard. These are unique binary representations that handle special-case values. They are not intended to be mathematically correct — they are there so you can handle undefined cases in your code.

@Simeon math.huge can be used to compare for “inf”, but I haven’t found anything so far for “nan”.

EDIT: -math.huge can be used to check for -inf.

@dave1707 I believe NaN can be tested for because it is the only value that is not equal to itself.

So:

local notanumber = 0/0

if notanumber ~= notanumber then
-- NaN detected
end

```

@Simeon Thanks.