Posted: 2nd Dec 2011 16:09
You never notice things not being there until you need them eh?

I couldn't find sgn() or an equivalent function ( which returns -1 for negative numbers, 1 for positive numbers and 0 for zero).

So here's a workaround if you find you need it;
+ Code Snippet
function mySgn ( thisNum )
	returnVal = ( thisNum > 0 ) - ( thisNum < 0 )
endfunction returnVal


There is another way to do it, but it's not as neat and probably a lot slower because of the division, but here it is for completeness;
+ Code Snippet
function mySgn ( thisNum )
	if thisNum = 0
		returnVal = 0
	else
		returnVal = thisNum / abs ( thisNum ) 
	endif
endfunction returnVal
Posted: 2nd Dec 2011 17:50
The first method is very neat and smart, very nice. The second is wrong and could cause a divide by zero error:
+ Code Snippet
function mySgn ( thisNum )
    if thisNum >= 0
        returnVal = 1
    else
        returnVal = -1
    endif
endfunction returnVal
Posted: 2nd Dec 2011 17:54
baxslash, you do not take into account sgn returning 0 on an input of 0.

+ Code Snippet
function mySgn ( thisNum )
    if thisNum = 0
        returnVal = 0
    else
        if thisNum > 0
            returnVal = 1
        else
            returnVal = -1
        endif
    endif
endfunction returnVal


If you want 0 or greater to return 1 and all negative numbers to return 0, then this code is best:

+ Code Snippet
function mySgn ( thisNum )
	returnVal = (thisNum => 0)
endfunction returnVal
Posted: 2nd Dec 2011 17:58
Oops, yes you're right. I just noticed the divide by zero and didn't think about returning zero.

EDIT: I'm even more stupid than I thought. It was right in the first place. Sometimes my mind plays tricks on me I thought sgn would only return a positive or negative, nevermind...

EDIT2: TFI Friday
Posted: 2nd Dec 2011 19:54
The first method is very neat and smart, very nice.

Thank you very much.

Boolean Algebra is something I'm quite fond of, you can dodge a lot of nested conditionals under the right circumstances.
Posted: 2nd Dec 2011 23:34
The second is wrong and could cause a divide by zero error


There's a more fundamental reason why this is not true
Try forcing a divide by zero in AppGameKit, you'll be surprised
Posted: 2nd Dec 2011 23:49
OK, I did and there was no error. Not sure if I like that or not...

I'd be interested in knowing how they managed that? Is there a way of checking the values of a division before it is done? Wouldn't that unnecessarily increase the workload?

Maybe I've had too many beers to think about it properly but surely doing something like:
+ Code Snippet
function divide(a#, b#)
    if b#=0
        ans#=0
    else
        ans#=a#/b#
    endif
endfunction ans#


...is much slower than:
+ Code Snippet
function divide(a#, b#)
    ans#=a#/b#
endfunction ans#
Posted: 3rd Dec 2011 19:00
I can only guess that dividing by zero does so much untold destruction on some of the devices, that it has to be avoided at all costs. However, the answer should be 999999.... rather than zero.
Posted: 4th Dec 2011 2:14
OK, I did and there was no error. Not sure if I like that or not...

Neither am I. I think it would be nice if the error was reported through the GetErrorOccurred() command as well as being set to 0.
Posted: 4th Dec 2011 13:10
No one that have good functions for something that wraps an angle ?

Iam forced to use dummy sprites for this now.

Iam missing the wrapvalue command from dbp

I couldt find it anywhere in the commands list of agk?
Posted: 4th Dec 2011 14:24
if angle#>360.0 then angle#=angle#-360.0

loop it, if you happens to increase in such enormous steps that it still is above 360.
Posted: 4th Dec 2011 14:29
oop it, if you happens to increase in such enormous steps that it still is above 360.

I had the same idea first but it seams to miss small steps when it wraps it like that.

After a while so did the angles dont match when i compared it with the dummy sprite angle?

But will try to give it a go again

Thanks my norwegian friend
Posted: 4th Dec 2011 20:57
You could try fmod() when you change the angle.
+ Code Snippet
angle# = fmod ( angle# + angleChange# , 360.0 )

I've not used it so can't vouch for it's accuracy (or indeed my use of it).
Posted: 5th Dec 2011 21:48
Pretty good! but it dosent like negative values like turning the other direction

But it works if i do this!

function Wrap(ang#)
result# = fmod ( ang#+(360.0*360.0) , 360.0 )
endfunction result#

For it to fail so do you nead to turn it constantly in only one direction for a long while.
360x360 = 129 600
Is it stupid to do it like that?

The differrence compared to using an sprite dummy is minimal

Thanks for the feedback to you both
Posted: 5th Dec 2011 22:20
You should probably file a bug report. I'm fairly certain a negative value should wrap around. For example, -90 should return 270 with mod 360.
Posted: 6th Dec 2011 0:04
If precision is important, I would resist adding such a large value.

Since floating point only uses a certain number of digits, using 129,600 (as opposed to say 720) will cost you three digits at the bottom end.

If my understanding of floating point is right

Edit:

Better yet would be not to use negative angles at all.

Instead of subtracting 10 degrees, you add 350 degrees and wrap it. Then it should never go minus.

ie. #angle = fmod ( angle# + (360.0 + change#) , 360.0 )

(of course this is no good if you're simply reading the angle of a self-rotating object)