Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / determining if a number is a float?

Author
Message
LiamBradbury
13
Years of Service
User Offline
Joined: 28th Apr 2011
Location: Manchester, UK
Posted: 3rd Jul 2013 04:58
I need to determine whether or not a number has decimal places, how would I do this within the code?

so if I'm counting from 1 to 10 and dividing each number by 2, I can say if the result is an integer or a float.

example:


audiosparx.com/liambradburymusic - professional music for your games.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Jul 2013 05:06 Edited at: 3rd Jul 2013 05:07
Try this:


Rough code, but you can get the idea.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 3rd Jul 2013 05:06 Edited at: 3rd Jul 2013 05:15
Try this:
'

edit: haha, posted at the same time
either way should work

edit2:
Here's couple of similarly useful functions I made a while back


LiamBradbury
13
Years of Service
User Offline
Joined: 28th Apr 2011
Location: Manchester, UK
Posted: 3rd Jul 2013 05:18
Thanks guys! works great!

audiosparx.com/liambradburymusic - professional music for your games.
LiamBradbury
13
Years of Service
User Offline
Joined: 28th Apr 2011
Location: Manchester, UK
Posted: 3rd Jul 2013 07:18
been playing around with this some more, Still not working.

My problem seems to be AppGameKit not recognizing floats properly.

This simple maths code should give me the answer 5.5 but instead returns 5



audiosparx.com/liambradburymusic - professional music for your games.
LiamBradbury
13
Years of Service
User Offline
Joined: 28th Apr 2011
Location: Manchester, UK
Posted: 3rd Jul 2013 07:20
hmmm just tried this and it worked, weird?!?!



didn't realize they all had to be floats??

audiosparx.com/liambradburymusic - professional music for your games.
easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 3rd Jul 2013 07:28
I'm pretty sure the inc command/statement only supports integers as well, I always use n#=n#+1 now for floats.

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 3rd Jul 2013 07:40
Quote: "a=11
b=2
c#=a/b

print c#"

This is an integer division and will always return an integer (as will any equation containing only integers). As you've found out, they need to be floats for it to work.

Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Jul 2013 11:27
Seriously people? did you all forget about MOD ?



----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Jul 2013 13:33
Hmmm. So what if it's 1.0?

In any case, if the variable is declared correctly you should know whether it's a float or an integer.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Jul 2013 13:36
Quote: "
I need to determine whether or not a number has decimal places
"


I guess .0 is the same as no decimal places.

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
LiamBradbury
13
Years of Service
User Offline
Joined: 28th Apr 2011
Location: Manchester, UK
Posted: 3rd Jul 2013 16:19 Edited at: 3rd Jul 2013 16:25
Thanks for the help guys, I haven't yet tried the 'Mod' way, but MissJoJos way of "if round(a#)<>a#" worked fine

audiosparx.com/liambradburymusic - professional music for your games.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Jul 2013 16:38
MissJoJo's method is the one most often used. I just wasn't thinking in those terms (having not remembered that there was a Round command in the AppGameKit set).

Impetus7's idea works as well, but only in the tightly defined code that you showed as your example.

If you don't know what is generating the value, the Round method will work.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Jul 2013 16:50
But a# IS a float, so what's the problem?

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Jul 2013 17:00
I think the issue is determining if there is a fractional component.

1.0 vs 1.1, for instance.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Jul 2013 18:01


Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 3rd Jul 2013 19:48

or...

which is probably a tad faster

or if the source is not an int (similar to MissJoJo);

if floor(result#) <> ceil(result#)
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Jul 2013 20:37
I like the floor/ceil method.

It is good for the generic case where you have no knowledge about how the value was created.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Jul 2013 22:46
Floor(0 and Ceil() do NOT work for integer values.

Ceil(2.0) is 2.0
Floor(2.0) is 2.0

It would be necessary to add a small additional fractional part when floor() = ceil() and then test again.

I'd still like to know a circumstance when this is needed!

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Jul 2013 22:56
Well, in the case you show, the value (2.0) is not a float in the sense that there is not a non-zero value after the decimal point. Which is what I believe to be the goal of the initial poster.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Jul 2013 00:33
Marl, how does this:
if t mod 2 = 1

Check for a float value? It's just going to return 0 or 1 for even and odd numbers. Isn't mod just for integer division anyway?

Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 4th Jul 2013 00:45 Edited at: 4th Jul 2013 00:48
Technically it doesn't.

(and technically, that was Impetus73's code - I was offering an optimization but put code instead of quote like a noob)

As AL pointed out, for this thread, the definition of "is a float" has been adapted to suite the OP's request - to determine if a number has any non-zero digits after the decimal place.

So the point of this particular instruction is - as you rightly point out - to determine if the source integer being halved is odd or even - Which would ultimately lead to whether decimal places become involved.

If working from an Int source, checking with Int instructions will be faster than converting and checking the float. If the result shows no decimal places then float maths may not be needed at all.
LiamBradbury
13
Years of Service
User Offline
Joined: 28th Apr 2011
Location: Manchester, UK
Posted: 4th Jul 2013 01:13
Just to clarify I wanted to be able to read if there were any decimal places at all on the number, so for example 5/2 = 2.5, were as 6/2 = 3 , I made a program which searches for the highest prime number and in order to do so I needed to divide the number I was testing by all the numbers before it and if any of them divided perfectly into an integer value (such as 6/2=3) then that equals 1 prime factor. Prime numbers only have 2 prime factors, 1 and itself.

See this post for the full code

audiosparx.com/liambradburymusic - professional music for your games.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 4th Jul 2013 01:43
I did the same program in Fortran way back when I was working in the computer room during my freshman year. We had a card reader that was hooked up to the mainframe several hundred miles away.

I let my program run for two minutes. And I got a huge ration of grief for using up so much time for something so trivial. Two minutes on a shared mainframe back in 1977 was very expensive. There were no PCs then and certainly no terminals for entering programs.

I had fun doing it and never wasted computer resources like that again.

Now, I can use all the time I want on any of my computers and no one complains.

In your case, since are working strictly with integers to begin with, simply use the Mod function with the two values. If it returns anything other than zero, then you have a fractional part if you do the division.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 4th Jul 2013 08:53
Liam - Are you trying to find prime factors, or just generating primes?

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 4th Jul 2013 10:18
Quote: " I needed to divide the number I was testing by all the numbers before it"

I believe you only need to test up to the sqrt of the given number.

LiamBradbury
13
Years of Service
User Offline
Joined: 28th Apr 2011
Location: Manchester, UK
Posted: 4th Jul 2013 11:02 Edited at: 4th Jul 2013 11:07
@jim hawkins, the program finds both, it determines if it is a prime number judging on how many prime factors the number has.

@hodgey Not sure, I'll have to look into it more, but I found that if I test up to half of the number being tested then I can make sure all prime factors are accounted for. I did this by dividing each number by 2 up to half of the testing number, if the result is a number requiring no decimal places (eg 2.0) then it is a prime factor.

example:



audiosparx.com/liambradburymusic - professional music for your games.
LiamBradbury
13
Years of Service
User Offline
Joined: 28th Apr 2011
Location: Manchester, UK
Posted: 4th Jul 2013 11:03
@Ancient Lady - cool story! I wasn't even born then! haha 1986 I was born!

audiosparx.com/liambradburymusic - professional music for your games.
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 4th Jul 2013 21:20
My solution, solved the problem presented in the original post, what else do you want? haha

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.

Login to post a reply

Server time is: 2024-05-03 12:50:42
Your offset time is: 2024-05-03 12:50:42