reptile7's JavaScript blog

Saturday, March 06, 2021

Finger on the Trig

Blog Entry #413

As regards the Super Calculator's

**Special functions**section

we have previously discussed the log, Spec, and PI keys; we'll hit the remaining sin, cos, and tan trigonometry keys in today's post.

Whether logarithm and trigonometry keys are "special" or not is a matter of perspective, I suppose; in my book they are standard calculator functionality, but given that most JavaScript calculators these days don't have them I would seem to be 'outvoted' in this respect. At any rate, I would move the key to the SC's

**Powers**section as it gets the exponent for the e

^{exponent}= power exponentiation and would put the , , and keys in a separate

**Trigonometry**section.

**To the radian**

^{th}degreeClicking the button

`<input type="button" value=" sin " onclick="getinput(sine);">`

initially triggers the

`getinput( )`

function and then a `sine( )`

function`function getinput(func) {`

var a = document.mainform.total; ...

var mode = document.mainform.mode[0].checked ? 1 : 0; ...

if (func == sine) { return sine(mode, a); } ... }

as per usual.

The

`sine( )`

function is the fifteenth-in-source-order function in the supercalc.txt `<script>`

element and closely parallels the `squared( )`

and `cubed( )`

functions, and is detailed below in its entirety (I've spruced it up a bit); Mozilla's current `Math.sin( )`

page is here.`function sine(mode, obj) {`

if (mode == 1) { window.alert("This function returns the sine of any given number."); }

var aa, a;

if (obj.value != "" && obj.value != "0") {

aa = window.prompt("Do you want to find the sine of the number in the total text box?", "y or n");

if (aa == "y") { doit(Math.sin(obj.value), obj); }

else {

a = window.prompt("Enter a number to find the sine of:", "");

more("(" + Math.sin(a) + ")", obj); } }

else {

a = window.prompt("Enter a number to find the sine of:", "");

doit(Math.sin(a), obj); } }

Things go in an equivalent way for the and buttons, which are bound to a

`Math.cos( )`

-based `cosine( )`

function and a `Math.tan( )`

-based `tangent( )`

function, respectively.Can we merge the

`sine( )`

, `cosine( )`

, and `tangent( )`

functions as we did the `squared( )`

, `cubed( )`

, and `power( )`

functions in the previous post, say, via a set of switch statements that handle the help messages and `Math.sin( )`

, `Math.cos( )`

, and `Math.tan( )`

calls? Sure, and I may do just that when I roll out a demo at a later point.Jamie Beyore's Another Great Science Calculator and Saries's Rainbow Calculator also have , , and keys, which for both calculators expect an angle input in degrees, which is reasonable because

(1) a math student first learns about the trigonometric functions in the context of a right triangle and

*then*progresses to their unit circle-based definitions - at least this was true for me back in the day - and

(2) for hand-held calculators having DEG and RAD angular modes, the DEG mode is the default - at least this is true for my trusty Casio fx-85v calculator.

In contrast, the SC's trig keys expect an angle or arc length input in radians, although the

`sine( )`

/`cosine( )`

/`tangent( )`

help messages don't tell the user that.It is simple enough to add a DEG capability to the SC. Drawing inspiration from the help message machinery, we can flank the SC trig buttons with a name="trig_mode" pair ofDEG andRAD radio buttons

`<input type="radio" name="trig_mode" checked="checked" />DEG`

<input type="radio" name="trig_mode" />RAD

and use the trig_mode checked state to control the suitable setting of help messages and angle/arc length inputs, e.g.:

`function sine(mode, obj) {`

var trig_mode, alert_message, aa, a;

trig_mode = document.mainform.trig_mode[0].checked ? 1 : 0; // DEG if true, RAD if false

if (trig_mode) alert_message = "In DEG mode, this function gets the sine of an angle in degrees.";

else alert_message = "In RAD mode, this function gets the sine of an angle/arc length in radians.";

if (mode) window.alert(alert_message);

if (obj.value != "" && obj.value != "0") {

aa = window.prompt("Do you want to find the sine of the number in the total text box?", "y or n");

if (aa == "y") {

a = trig_mode ? obj.value * Math.PI / 180 : obj.value;

doit(Math.sin(a), obj); }

else {

a = window.prompt("Enter a number to find the sine of:", "");

if (trig_mode) a *= Math.PI / 180;

more("(" + Math.sin(a) + ")", obj); } }

else {

a = window.prompt("Enter a number to find the sine of:", "");

if (trig_mode) a *= Math.PI / 180;

doit(Math.sin(a), obj); } }

• Recall that an arc length of

`Math.PI`

radians spans an angle of `180`

degrees.• Note that DEG-RAD bifurcation is needed for each a input point (you can't just bifurcate once at the beginning of the function).

• The *= multiplication assignment operator is detailed here.

Not addressed previously:

Do the

`if (obj.value != "" && obj.value != "0")`

gate and its accompanying `else`

clause serve a purpose here? Getting the sine, cosine, or tangent of 0 degrees or radians is a perfectly legitimate thing to do (unlike getting the of 0, which is not such a perfectly legitimate thing to do); moreover, `Math.sin("")`

, `Math.cos("")`

, and `Math.tan("")`

map the empty string input to 0. You can go ahead and throw out the gate and clause - or not, per your preference.Expressional note:

The

`Math.sin( )`

, `Math.cos( )`

, and `Math.tan( )`

methods balk at (return NaN for) inputs that contain nonnumeric characters, so if you want to get the cosine of (3.141592653589793) radians or the tangent of (3.141592653589793)/4 radians, then you'll have to first get rid of the ( and the ) plus the / by `eval( )`

-uating those inputs via the key and its underlying `calc( )`

function.**Arcsine, arccosine, arctangent**

Now, what if we wanted to go the other way - what if we wanted to map a sine or cosine or tangent to the corresponding angle or arc length - how would we do that?

Neither the SC, Jamie Beyore's calculator, nor Saries's calculator has , , and keys. We noted in the

**Trigonometry**section of Blog Entry #371 that we can get the arcsine, arccosine, or arctangent of a number by typing a relevant JavaScript expression - e.g.,

`Math.acos(-1)`

- into the user input field of Jamie's calculator and then -ing that expression, and we can do the same with the SC and Saries's calculator. However, this is not exactly helpful to users who don't know JavaScript, is it?The , , and keys of my Casio fx-85v calculator have sin

^{-1}, cos

^{-1}, and tan

^{-1}second functions that are accessed via a SHIFT key. Somewhat similarly, the Google calculator widget (this link may or may not take you to the widget, depending on what browser you are using) has an key that toggles the widget's trig keys between // and //.

Of course, we could just add , , and keys to the SC - check out the calculator at Calculator.net - but I like the Google approach better and we'll have a go at applying it to the SC in the following entry.

Actually, reptile7's JavaScript blog is powered by Café La Llave. ;-)