Code archives/Algorithms/Diminish ()

This code has been declared by its author to be Public Domain code.

Download source code

Diminish () by BlitzSupport
(Posted 26 years ago)
This code brings a value towards zero by the specified amount, regardless of whether it's less than or greater than zero. Simple, but very useful!
Function Diminish# (value#, amount#)
	If Abs (value) <= Abs (amount) Then Return 0
	value = value - (amount * Sgn (value))
	Return value
End Function

; Example...

speed# = -100 ; 100

Repeat
    speed = Diminish (speed, 0.1)
    Cls: Locate 10, 10: Print speed: Flip
Until speed = 0

Comments

alanc5
(Posted 20 years ago)
Could anyone give an example of how this would be used in a game?

John Blackledge
(Posted 20 years ago)
Well, out of eight trials I see that only 3 exited with a value of zero.
The others (despite the last line of code) exit with a value such as -0.02.
Why is that?

Floyd
(Posted 20 years ago)
It always exits with a zero, but doesn't display properly.

I think the default behavior of front and back buffers changed some time in the last five years. The example really should be writing to the back buffer and flipping.

Code Archives Forum