Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Stupid LUA logic help? [simple math] [closed]

Asked by 9 years ago

I'm currently making a table and will have values of 0 which will be divided. Is there anyway to bypass the stupid answer it gives me? 5/0 is Undefined, which I assume means 0, but when I set B as the undefined value and just add one. It gives me the same thing.

Blank= {0,5}

print(Blank[1]/Blank[2])   ---Prints 0
print(Blank[2]/Blank[1])   ---Prints 1.#INF
b= Blank[2]/Blank[1]
print("wait")   ---Prints "wait"
print(b+1)   ---Prints 1.#INF
0
its cause in line 4 and 7 it is infinite. 5/0 is infinity techincally NinjoOnline 1146 — 9y
0
...So nothing equals infinity... Funny. So how would I bypass it? Orlando777 315 — 9y
0
division by zero is the proof that math is not an exact science Tesouro 407 — 9y
1
Actually, mathematics *is* an exact science in the most literal of senses. Nothing can ever be equal to infinity, because infinity isn't a number, only a concept. In the case of division by zero, there is no answer, as context could imply it to be approaching positive or negative infinity, or actually some constant in cases such as 0/0 when dealing with Calculus. adark 5487 — 9y
0
just jk Tesouro 407 — 9y

Locked by adark, Spongocardo, and BlueTaslem

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

A standard programming convention for numbers is that there are four non-real numbers:

  • NaN (not a number) or Indeterminate which is when an operation has no reasonable value.

  • -0 which is just like 0 for almost everything except dealing with these strange ones

  • Infinity and -Infinity which are the result of 1/0 == -1/-0 and 1/-0 == -1/0. Any operation on +-infinity will produce +-infinity. Multiplying or dividing by a negative number will produce the opposite infinity. Multiplying by 0 will give NaN.


You're doing 5/0 which is +Infinity (Lua expresses this as "1.#INF" in the output)

+Infinity + 1 is still infinity (which makes sense since you shouldn't be able to make it larger)


As an aside about the math:

You "can't divide by zero" because 1 / 0 is not a real number real as in the sense of the set of real numbers

Programming languages don't implement the real numbers. You can't, because that would require infinite precision (and hence infinite memory and processing time).

It's also inconvenient, in cases like 1 / 0 where in many cases, this could be assigned the value of an extremely larger number (when approaching from the positive direction, anyway).

Because these are real numbers, they can choose to make their own conventions for dealing with operations that the reals leave undefined.

It should also be noted that Infinity is also not a real number and so this is just an extension.


Secondary aside: Ignoring things like 1/0 and Infinity, floating point numbers (as opposed to "fixed point numbers" like integer types in most languages) don't behave like the reals.

They are not associative:

local A = 1e-6
local B = 1e5
local C = B * 0.99

print( B - C + A == B - (C + A) ) -- false

They are not continuous / dense:

local A = 1e1
local B = 1e-10

print(A + B == A) -- true
print(A - B == A) -- true
Ad