Number questions

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Number questions

Hey, been wondering this for some time. How do I compare a number too see if it's decimal or an integer?

ivan
Party member
Posts: 1549
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Number questions

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.

T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Number questions

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).

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Number questions

You could also check if a number is an integer by doing this:

Code: Select all

function isInteger( x )
return x % 1 == 0
end
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim

Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: Number questions

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?

zorg
Party member
Posts: 2807
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Number questions

Bindie wrote:+1 point for anyone who guesses right what kind of clone I'm making?
Just a shot in the dark
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: Number questions

Bull's eye, +1!

T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Number questions

Why would you need to know if a number is an integer or not to produce such a grid?

s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Number questions

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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
baby:hurt(me)
end

Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: Number questions

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.

Who is online

Users browsing this forum: No registered users and 19 guests