19.04.2017 Views

Learn to Program with Small Basic

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Round()<br />

-4 -3.5 -3 0 3 3.5 4<br />

Round() behaves mostly how you’d expect it <strong>to</strong><br />

when rounding a number. Above 0.5, it’ll round<br />

up; below 0.5, it’ll round down.<br />

Floor()<br />

-4 -3.5 -3 0 3 3.5 4<br />

Floor() always rounds down, throwing away<br />

any partial fraction of a whole. Look at what<br />

happens <strong>with</strong> negative values!<br />

Ceiling()<br />

-4 -3.5 -3 0 3 3.5 4<br />

Ceiling() always rounds up.<br />

Math.Round(3.4) = 3<br />

Math.Round(3.6) = 4<br />

Math.Round(-3.4) = -3<br />

Math.Round(-3.6) = -4<br />

Math.Floor(3.4) = 3<br />

Math.Floor(3.6) = 3<br />

Math.Floor(-3.4) = -4<br />

Math.Floor(-3.6) = -4<br />

Math.Ceiling(3.4) = 4<br />

Math.Ceiling(3.6) = 4<br />

Math.Ceiling(-3.4) = -3<br />

Math.Ceiling(-3.6) = -3<br />

Figure 7-1: The rounding methods <strong>with</strong> example arguments and return values<br />

Traditional Rounding<br />

Be careful when you use the Round() method if the fraction part of the number<br />

is exactly 0.5. In this case, the Round() method rounds <strong>to</strong> the nearest<br />

even integer (this is called banker’s rounding). For example, 0.5 and –0.5 are<br />

rounded <strong>to</strong> 0, 1.5 and 2.5 are rounded <strong>to</strong> 2.0, and –1.5 and –2.5 are rounded<br />

<strong>to</strong> –2. This is different from what you learned in algebra, where the 0.5 fractions<br />

always round up <strong>to</strong> 1! Even though it’s not what you’re used <strong>to</strong>, banker’s<br />

rounding is very common and is regularly used by bankers, which gives it<br />

its name.<br />

But how can we make <strong>Small</strong> <strong>Basic</strong> round numbers the way you learned<br />

in school (where the 0.5 fraction is always rounded up)? We’ll do some fancy<br />

footwork using the Floor() method instead of the Round() method, like this:<br />

Math.Floor(x + 0.5)<br />

Using this trick, x represents whatever value you want <strong>to</strong> round. So if x<br />

is 0.6, then x + 0.5 = 1.1, and Floor(1.1) = 1. Cool! That’s exactly how we’d<br />

expect it <strong>to</strong> work.<br />

But let’s say x is 2.5. If we just used Math.Round(2.5), we would get 2, which<br />

isn’t the result you would want if you wanted <strong>to</strong> use traditional rounding.<br />

We want <strong>to</strong> round up and get 3. Using our fancy trick, you’d get x + 0.5 =<br />

3.0, and Floor(3.0) = 3. Now that’s more like it! This gets the values you’d<br />

expect if you wanted <strong>to</strong> round a number <strong>with</strong> a .5 fraction.<br />

Empowering <strong>Program</strong>s <strong>with</strong> Math 87

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!