Number questions

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

Number questions

Post by Bindie »

Hey, been wondering this for some time. How do I compare a number too see if it's decimal or an integer?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Number questions

Post 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.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Number questions

Post 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).
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Number questions

Post 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
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

Post 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?
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Number questions

Post by zorg »

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 :3True 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

Post by Bindie »

Bull's eye, +1!
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Number questions

Post by T-Bone »

Why would you need to know if a number is an integer or not to produce such a grid?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Number questions

Post 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

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests