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.
ID:158711
Jul 11 2009, 6:31 pm
|
|
#define isint(x) ((x) == round(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) 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
|
|
Zagreus wrote:
I'd use the mod operator. 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. > proc/isDecimal(x) edit: heh, had my if/else backwards, whoops. 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 |
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) |
That grabs the number's integer, and if it is not equal to the original number, then it's a decimal.