Page 1 of 1

Number questions

Posted: Fri May 08, 2015 7:31 pm
by Bindie
Hey, been wondering this for some time. How do I compare a number too see if it's decimal or an integer?

Re: Number questions

Posted: Fri May 08, 2015 7:56 pm
by ivan

Code: Select all

if math.floor(number) == number then
  -- is integer
else
  -- non-integer
end
There may be some robustness issues of course but it should work as
long as the number is not very large or small.

Re: Number questions

Posted: Fri May 08, 2015 9:15 pm
by T-Bone
If that's what you want to do, ivan's solution should work just fine. It sounds to me however like something you would never need to do. If you need an integer, but aren't sure it is one, just use math.floor(datNumber) (or math.floor(datNumber + 0.5) if you want to round it).

Re: Number questions

Posted: Fri May 08, 2015 10:23 pm
by davisdude
You could also check if a number is an integer by doing this:

Code: Select all

function isInteger( x )
     return x % 1 == 0
end

Re: Number questions

Posted: Sat May 09, 2015 5:50 pm
by Bindie
Both solutions work for me. :) Basically I was trying to make this kind of grid:

Code: Select all

{
{w,w,w,w,w,w,w,w,w}
{w,0,0,0,0,0,0,0,w}
{w,0,i,0,i,0,i,0,w}
{w,0,0,0,0,0,0,0,w}
{w,0,i,0,i,0,i,0,w}
{w,0,0,0,0,0,0,0,w}
{w,0,i,0,i,0,i,0,w}
{w,0,0,0,0,0,0,0,w}
{w,w,w,w,w,w,w,w,w}
}                           -- i = indestructable
                            -- w = wall
                            -- 0 = floor
+1 point for anyone who guesses right what kind of clone I'm making?

Re: Number questions

Posted: Sun May 10, 2015 2:01 pm
by zorg
Bindie wrote:+1 point for anyone who guesses right what kind of clone I'm making?
Just a shot in the dark

Re: Number questions

Posted: Sun May 10, 2015 2:20 pm
by Bindie
Bull's eye, +1!

Re: Number questions

Posted: Sun May 10, 2015 3:35 pm
by T-Bone
Why would you need to know if a number is an integer or not to produce such a grid?

Re: Number questions

Posted: Sun May 10, 2015 3:52 pm
by s-ol
T-Bone wrote:Why would you need to know if a number is an integer or not to produce such a grid?
My best guess is he halfed the x coordinate and checked whether it was integer?

In that case he should've just take the modulo-2:

Code: Select all

if x%2 == 0 then -- x is even end

Re: Number questions

Posted: Sun May 10, 2015 6:58 pm
by Bindie
S0lll0s wrote:My best guess is he halfed the x coordinate and checked whether it was integer?

In that case he should've just take the modulo-2:

Code: Select all

if x%2 == 0 then -- x is even end
That's right, I found an easier solution though using false and true values.