12 Other important unary operations

12.1 Scope of the prototype

To perform tests of the merits of unums, we will need the sort of functions you find on the buttons of scientific calculators. One category of functions not in the prototype is inverse trigonometry functions like arctangent. Those are left as an exercise for the reader to develop; they are not needed for any of the experiments in this book.

“Unary” operations are operations that take just one input value and produce one output value. Unlike the basic + – × ÷ operations and the xy function, unary operations increment the ubitsmoved tally by just the bit counts in the one input and one output ubound, and increment numbersmoved by 2 instead of 3.

The section on trigonometric functions contains a somewhat radical proposal, one that neatly solves the problem of argument reduction mentioned in Section 5.4.2 but may take some getting used to.

12.2 Absolute value

Usually, the absolute value function is the most trivial task imaginable: Set the sign bit to zero.

With unums and ubounds, absolute value has a few things to watch out for. We have to remember to reverse the order of the interval endpoints if both are less than zero. If zero is inside the interval, then a closed zero is the lower bound, and the maximum of the endpoint absolute values is the right-hand endpoint. The devil is again in the details, and the details are in Appendix C.16.

Notice that when negating reverses the left-right positions of the values, they carry their open or closed state with them. So, the absolute value of the ubound that represents [-3, -2) should be (2, 3] :

Images

Even though the operation seems trivial, absu updates the ubitsmoved and numbersmoved tallies. When the endpoints switch places, it is necessary to move numbers and bits, which takes energy. It also may be unnecessary to have a g-layer version of something that can clearly be done in the u-layer without any scratchpad facilities, but it is included in the prototype for completeness.

12.3 Natural logarithm, and a mention of log base 2

The inverse operation for x, the natural logarithm, has three special exact values. The logarithms of 0, 1, and ∞ are exactly – ∞, 0, and ∞, respectively. Of course we return NaN for any negative real argument, since the logarithm is complex-valued there. The code for logg and logu is in Appendix C.16. Here is an example of finding the log of the range [0, 1], a result with peculiarly exact bounds:

Images

As with the exponential function, we can get many more exact results from logarithm functions if we use 2 instead of ⅇ as the base. There is a recurring theme here: Why use functions that are inexact almost everywhere when there are similar functions that have the promise of exact answers for some exact inputs? If that philosophy is applied to trigonometric functions, there are a number of computational advantages.

12.4 Trig functions: Ending the madness by degrees

When students first learn trigonometry, the subject is usually introduced with angles measured in degrees. Everyone knows angles like “180 degrees” and “45 degrees” even if they have never taken trigonometry. Later, courses at the calculus level and above teach that radians are a better way to measure angle because formulas for derivatives and differential equations look simpler that way. Because of the clout of higher math, all the standard language libraries use radians as the arguments for sine, cosine, tangent, and so on. If you want to compute the sine of 90°, you have to get as close to the radian equivalent as you possibly can, π2, within the vocabulary of floats. Imagine asking for the tangent of 90°; using the float that is a little below π2 produces an enormous positive number, but being the one a little above π2 produces an enormously negative number.

The use of radians discards useful opportunities to express exact answers to exact questions. So why not define the trigonometry functions with degrees for the arguments instead of radians? If you ask for the sine of 30 degrees, you get exactly 12. The tangent of 135 degrees is exactly –1. The unum function for tangent knows to return NaN as the tangent of an exact 90° angle.

Once you commit to using radians, you discard any hope of landing on such a special angle value again using numbers you can represent as floats or unums. Computers are like humans in this respect: They have an easier time working with degrees than with radians!

The benefit of using degrees is bigger than this, however: It solves the problem of argument reduction. Recall the example in Section 5.4.2 where evaluating a trig function of a large number required “unrolling” its value with a huge number of bits to store π to sufficient accuracy. What if we simply had to know the remainder of dividing a big float by the integer 180? That does not take much work at all, and certainly it does not require storage of hundreds of bits of the value of the transcen-dental number π. If anyone makes the goofy request that a computer evaluate the cosine of 10100 treated as an exact number, in degrees the argument 10100 is the same as ten degrees, exactly.

The prototype includes several trig functions, though not a complete set:

Function

g-layer version

u-layer version

cosine

cosg[g]

cosu[u]

sine

sing[g]

sinu[u]

tangent

tang[g]

tanu[u]

cotangent

cotg[g]

cotu[u]

The approach for interval bounds is to reduce the lower bound argument to a manageable range near zero, then reduce the upper bound argument to something no more than two periods greater than the lower bound. This catches cases where the function has a minimum or maximum somewhere within the input interval.

For example, imagine going 2/3 of the way around the unit circle, from 60° to 300 °, with the angular interval open at both ends:

Images

The cosine is the horizontal projection of that angle interval, which ranges from –1 to cos(60°) = π2 exactly, but the function value is open on the right (the blue bar is rounded on the right, to show this).

Images

The sine is the vertical projection of that angle interval, and while sin(60°) and sin(300°) are not expressible as exact unum values, that does not matter because the overall bound is the entire range of the unit circle, a closed interval:

Images

With the basic operations defined, we are ready to start experimenting with test problems to compare unums and floats. However, there is one bit of unfinished business: Definition of operations that may be fused, that is, computed in the g-layer such that the value returned to the u-layer is at the highest accuracy possible for the u-layer environment. This is what assures portability (bit-identical results across computer systems) while raising the accuracy. It is the opposite of the approach used for Java, where bit-identical results were assured by dumbing down the operations to match that of the least-accurate system. One consequence of having well-defined fused operations is that it can greatly diminish the need for large fraction and exponent fields in the numerical representation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset