ID:158711
 
Hi people I had an idea for a program. But I need to know how to make a check that will check if the var given is a decimal.

So for example: (Note this isn't what it will be used for)

var/damage = rand(1,S)
damage /= 2


Now the problem is that say:
S = 5

So 5 / 2 = 2.5

I want my program to do something like this.
if damage is a decimal blah blah blah...

Now i don't want to know how to round i want to know how to detect a decimal. THANKS XD and i hope you understand.




proc/isDecimal(num)
if(round(num) != num)
return TRUE


That grabs the number's integer, and if it is not equal to the original number, then it's a decimal.
#define isint(x)    ((x) == round(x))
#define isnotint(x) (!isint(x))
Edit 4: None of this works! Don't even bother, mod only works on whole numbers.


I'd use the mod operator. This will return the remainder after a division. Divide any whole number by 1 and you should have a remainder of 0, divide any partial number by 1 and you will always have at some sort of remainder.

So
proc/isDecimal(x)
if(x % 1) return 1

mob/verb/testNumber()
if(isDecimal(1.5))
//This will be shown
src << "1.5 has a decimal value."
else
//You won't see this.
src << "1.5 is a whole number."


edit: heh, had my if/else backwards, whoops.
edit2: I fail at return values.
edit3: Sorry! I suck. Big time. I'm on pain meds for my back right now though and not doing that hot. No excuse though.
In response to Zagreus
Aye, no go there: [link]
In response to Zagreus
Zagreus wrote:
I'd use the mod operator.
edit: heh, had my if/else backwards, whoops.
edit2: I fail at return values.
edit3: Sorry! I suck. Big time. I'm on pain meds for my back right now though and not doing that hot. No excuse though.

Well, ignoring the programming mistakes, your concept is the most efficient.

Using this method is slightly faster than round()... About 0.1 second for me in a million calculations. Not huge, specially considering the quantity, but everything makes a difference.

EDIT: Well Kaioken just ruined that... Apparently the check isn't actually returning correct results... Which means the speed difference is irrelevant. -.-' /wrists.
In response to Kaioken
Doh!
In response to Kaioken
Ha! :D Victory is mine!
In response to Popisfizzy
Wouldn't isnotint() create an infiniteloop? Probably meant (!isint(x)) instead.
In response to Kaiochao
Actually, in this case it wouldn't even compile (preprocessor rather than a proc), but it's still an error. I just fixed it.
In response to Zagreus
Zagreus wrote:
Edit 4: None of this works! Don't even bother, mod only works on whole numbers.


I'd use the mod operator. This will return the remainder after a division. Divide any whole number by 1 and you should have a remainder of 0, divide any partial number by 1 and you will always have at some sort of remainder.

So
> proc/isDecimal(x)
> if(x % 1) return 1
>
> mob/verb/testNumber()
> if(isDecimal(1.5))
> //This will be shown
> src << "1.5 has a decimal value."
> else
> //You won't see this.
> src << "1.5 is a whole number."
>

edit: heh, had my if/else backwards, whoops.
edit2: I fail at return values.
edit3: Sorry! I suck. Big time. I'm on pain meds for my back right now though and not doing that hot. No excuse though.


Sorry i have been sooooo late with the reply i have been really busy.

I have tried this but it always says it is a whole number no matter what i put in.
In response to Spunky_Girl
So how would i use this?

proc/isDecimal(num)
if(round(num) != num)
return TRUE


Do i replace (num) with my var?
In response to ReaperRoflCopter
ReaperRoflCopter wrote:
I have tried this but it always says it is a whole number no matter what i put in.

If you read the other posts you'll see that this actually doesn't work lol. You'll have to use the round()/comparison method.
In response to ReaperRoflCopter
Yus.
//example
proc/isDecimal(num)
return round(num)!=num
mob/verb/CheckDecimal(n as num)
src<<"It's [isDecimal(n)?"":"not "]a decimal!"
In response to Kaiochao
Using the ? operator is a really stupid way to illustrate something to somebody. It really just makes things harder to understand.

mob/verb/CheckDecimal(n as num)
if(isDecimal(n))
src << "It's a decimal!"
else
src << "It's not a decimal!"
Not actually mine, given to me by Kaioken (sorta XD). Probably suggested to you before:

if(number != round(number,1))


If it returns 1, number is a decimal.

If it returns 0, number is an integer (if I got that right. I should probably pay attention in math >_<).