Jump to content
Sign in to follow this  
qUiLL

Bizarre finding with equivalent numbers that arent

Recommended Posts

i just wanna mention something weird ive found. for some reason 2 variables, one which i specifically set to 120, and another which was also definitely 120 werent equalling each other. i used hint format to print them out and sure enough both said 120. well i subtracted on from the other and the result was 7.9......e-6 (0.0000079.....)

has anyone had this problem before? and how the heck did such a tiny decimal get onto the end of a number that was set from another number that was written in hard code, 120?

Share this post


Link to post
Share on other sites

can u say some more details (a little excerpt of ur script, maybe)?

Share this post


Link to post
Share on other sites

well the scripts are a bit complicated so i gotta take from several for you to get the picture

part of init.sqs

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

radiopowerlist = [150,140,130,120,110,100,90,80,70,60,50,40,30,20,10,0]

radiopower = [1,1,1]

part of script where a dialog is displayed to change power

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_defaultset = -1

_i = 0

#loop3

?(radiopowerlist select _i) != 0:_listtext = format["%1", (radiopowerlist select _i)]

?(radiopowerlist select _i) == 0:_listtext = "Off"

lbAdd [211,_listtext]

; the extra leeway of .01 compensates for a bizarre bug with OPF adding a very very small number to the end of 120's power in radiopower array

?(radiopowerlist select _i) >= (((radiopower select _tower)*100)-0.01) and (radiopowerlist select _i) <= (((radiopower select _tower)*100)+0.01):_defaultset = _i

_i = _i + 1

?_i < (count radiopowerlist):goto "loop3"

_oldtowerpower = radiopowerlist select _defaultset

;?_defaultset == -1:_defaultset = 3

lbSetCurSel [211,_defaultset]

@!(ctrlVisible 212)

_indexreturn = indexreturn

indexreturn = nil

?_indexreturn == -1:exit

?((radiopowerlist select _indexreturn) != _oldtowerpower and (radiopowerlist select _indexreturn) <= 100):[2,_tower,((radiopowerlist select _indexreturn)/100)] exec "requestsender.sqs"

?((radiopowerlist select _indexreturn) != _oldtowerpower and (radiopowerlist select _indexreturn) > 100):goto "confirm"

exit

this is an excerpt from the script that changes the power (its in a different script in order to syncronize it being changed on all computers

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

#radiopower

_changed = _param1

_power = _param2

radiopower set [_changed,_power]

@incontact

(radioareamkr select _changed) setmarkersize [((radiorange select _changed)*(radiopower select _changed)),((radiorange select _changed)*(radiopower select _changed))]

?((armytech select (radioside select _changed)) countside [player] == 1 and (radiopower select _changed > 0)):hint format["%1's power input changed to %2%3", (radioname select _changed), ((radiopower select _changed) * 100),"%"]

?((armytech select (radioside select _changed)) countside [player] == 1 and (radiopower select _changed == 0)):hint format["%1's power has been turned off", (radioname select _changed)]

exit

have fun reading that mess :-D

Share this post


Link to post
Share on other sites

Numbers are not guaranteed across the network. They will be very close, but you can not depend on them 100%.

Share this post


Link to post
Share on other sites

Use "mod" command as last resort. Works well.

example:

number=(number-(number mod 0))

this will subtract your 0.000079 and give you an integer.

Share this post


Link to post
Share on other sites

oops.. forgot that it will not work if number is 0.99999

you must add 0.5 to number before using "mod" to get it to round.

Better yet:

create a function with this code called "RoundInt.sqf"

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">private {"_num"};

_num=this select 0;

_num=_num+0.5;

_num=_num-(_num mod 1);

then you will have your own rounding function that rounds numbers straight to integers

(how could BIS miss such a common procedure as this?)  tounge_o.gif

EDIT:MY FAULT - I PUT "_num mod 0" ---meant as Toadlife pointed out "_num mod 1"

Share this post


Link to post
Share on other sites
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_num=_num-(_num mod 0);

Well, this actually results in a crash or in an error, since you are dividing a number by 0 crazy_o.gif to get its remainder.

Did you mean "_num mod 1" ?

Share this post


Link to post
Share on other sites

is that possible? in the manual it says mod is the remainder of a divided by b. but if b is 0, how can u divide by 0?

edit: nm just read the post ontop smile_o.gif

Share this post


Link to post
Share on other sites

no it's supposed to be: number = (number - (number mod 1))

Share this post


Link to post
Share on other sites

If I recall from a long time ago (I forget what I was scripting, it may have been a time clock), that .000000076 is ofp's way of saying zero.

I think if you try subtracting 119.999999999999 from 120 and round it you'd get a closer result.

I can't remember when/where that cropped up, though.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×