Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

## Post a reply

Write your message and submit
|
Options

## Topic review (newest first)

MathsIsFun
2009-01-20 08:13:37

#### random_fruit wrote:

Be careful!  If the type of 'angle' is an integer, the real maths involved in the second example can result in a loss of accuracy.

True, depending on language. But in pseudocode it works perfectly

Ricky
2009-01-20 07:17:54

So the "gem" should be written as:

#### Code:

angle -= round(angle/360.0 - 0.5)*360; // this little gem makes sure angle is >=0 and <360

Noting the ".0" on the first 360.

Also, the first piece of code does nothing where the value of the angle is already less than zero.

Actually, I'm pretty sure the first piece of code won't run.  I don't think "etc..." is a proper function call.

random_fruit
2009-01-20 05:57:39

Be careful!  If the type of 'angle' is an integer, the real maths involved in the second example can result in a loss of accuracy.  What may be better for an integer, might be to take the value modulo 360.  Also, the first piece of code does nothing where the value of the angle is already less than zero.

MathsIsFun
2009-01-19 16:05:31

Ever have little programming gems?

My example:

Instead of code like this to make sure an angle is between 0 and 360:

#### Code:

while (angle>360) {
angle -= 360;
}
etc...

You can use this "little gem":

#### Code:

angle -= round(angle/360 - 0.5)*360; // this little gem makes sure angle is >=0 and <360