Wednesday, July 06, 2016

Calculator Capabilities, Continued

Blog Entry #371

Before we get started, and in the name of completeness...

Regarding the previous post's As for the log10e and log2e key labels... subsection, it has come to my attention that the JavaScript Math object has

(1) a LOG10E property, which returns log

_{10}(e), and

(2) a LOG2E property, which returns log

_{2}(e),

but these constants play no role in the AGSC script's base10_log( ) and base2_log( ) functions.

We wrap up our discussion of the Another Great Science Calculator (AGSC) script with today's post, specifically, we'll (inter alia)

(1) go over the trigonometry part of the script,

(2) briefly compare Jamie's calculator to an HTML Goodies calculator that we checked over previously, and

(3) finally get to that demo I promised you.

**Trigonometry**

Jamie's calculator offers , , and keys for respectively determining the sine, cosine, and tangent of an angle in degrees.

`var rad = 3.141592654 / 180;`

function sin_form(form) { form.expr.value = Math.sin(form.expr.value * rad); }

function cos_form(form) { form.expr.value = Math.cos(form.expr.value * rad); }

function tan_form(form) { form.expr.value = Math.tan(form.expr.value * rad); }

<td><input type="button" value=" sin " onclick="sin_form(this.form);"></td>

<td><input type="button" value=" cos " onclick="cos_form(this.form);"></td>

<td><input type="button" value=" tan " onclick="tan_form(this.form);"></td>

The JavaScript Math.sin( ), Math.cos( ), and Math.tan( ) methods all expect an angle (arc length) argument specified in radians. The calculator anticipates that the user will input an angle in degrees and converts the user's input to an angle in radians via a rad = π radians / 180° conversion factor before feeding it to Math.sin( ), Math.cos( ), or Math.tan( ).

If you want to take the sine/cosine/tangent of an angle in radians, or if you want to 'go backward' from a length ratio to an angle via the JavaScript Math.asin( ), Math.acos( ), Math.atan( ), or Math.atan2( ) method, then you can, as per last time, input a relevant expression into the expr field and eval( ) it, e.g., Math.atan2(5, 5) * 180 / Math.PI outputs 45.

**One last function**

Clicking the calculator's key calls a clear_display( ) function that clears the expr field by setting its value to a space character.

`function clear_display(arg) { arg.expr.value = " "; }`

`<td><input type="button" value=" C " onclick="clear_display(this.form);"></td>`

Could we replace this code with an

`<input type="reset" value="Clear Entry">`

input? That we could.**Jamie vs. Saries**

This is actually our second go at a calculator script: not quite ten years ago we discussed the "rainbow calculator" script of HTML Goodies' JavaScript Script Tips #52-55 in Blog Entries #74 and #75. I have recently restored the missing demos and images in these entries, and you can see and make use of the rainbow calculator at the top of Blog Entry #74.

Crafted by "Saries" in 1999, the rainbow calculator has the following keys that Jamie's calculator doesn't have:

(1) , for subtracting the rightmost character in the name="text" input field;

(2) , for exponentiating x to the y power;

(3) , for returning the absolute value of x;

(4) , which inputs an E for expressing a number in scientific notation;

(5) , for rounding a floating-point number to the nearest integer;

(6) , for converting a number to its additive inverse;

(7) , for inputting a random floating-point number between 0 (inclusive) and 1 (exclusive); and

(8) , for inputting Math.PI.

All of these operations can be carried out 'by hand' with Jamie's calculator, however. You're up to the task of typing

`Math.random( )`

in the expr field and clicking the key, aren't you?Conversely, Jamie's calculator has two logarithm keys that Saries' calculator doesn't have. However, Saries' calculator itself uses a

`document.calculator.text.value = eval(document.calculator.text.value);`

command to evaluate arithmetic expressions, and we can enter and eval( ) Math.log(x)-containing expressions in the text field of Saries' calculator just like we can in the expr field of Jamie's calculator.**Decimal ↔ hexadecimal**

Let me note one more cool and useful thing that you can do with Jamie's (or Saries') calculator: convert decimal numbers to hexadecimal numbers and vice versa.

Syntax #1:

`eval("numeric_literal .toString(radix)")`

100 .toString(16) outputs 64

0xaa .toString(10) outputs 170

Yep, that's a space between the numeric_literal and the .toString(radix) call: it's there to prevent the dot property accessor from being interpreted as a decimal point*. This peculiarity of the toString( ) method of the Number object is buried in the JavaScript 1.5 Core Reference - there's no mention of it at the current Mozilla Number.prototype.toString( ) page. Note that hexadecimal integer literals are prefixed with 0x (or 0X).

*In practice with the OS X browsers on my computer, the space is actually unnecessary when converting a hexadecimal number to a decimal number but is definitely needed when converting a decimal number to a hexadecimal number.

Syntax #2:

`eval("(numeric_literal).toString(radix)")`

(960).toString(16) outputs 3c0

(Thanks, W3Schools.)

In both cases, JavaScript automatically converts the numeric_literal to a temporary Number object, calls the toString( ) method, and then discards the temporary Number object, or at least I'm guessing that's what happens based on what happens when a String object method is called on a string literal.

Other decimal ↔ hexadecimal syntaxes are possible, e.g.,

`eval("x = numeric_literal; x.toString(radix)")`

, but I'd go with the ones above.**But wait, there's more...**

JavaScript (ECMAScript) has recently added seventeen new methods to the Math object including

(a) a cbrt( ) method for calculating cube roots,

(b) a hypot( ) method that can carry out a c = (a

^{2}+ b

^{2})

^{½}Pythagorean theorem calculation from a and b inputs, e.g.,

`Math.hypot(5, 12)`

returns 13,(c) a log10( ) method for calculating common logarithms, and

(d) a log2( ) method for calculating binary logarithms.

The (a-d) methods can be used with Jamie's (or Saries') calculator IF you are running the calculator with an up-to-date version of Firefox, Google Chrome, Opera, or Safari; IE doesn't support them yet.

**Demo + changes**

Table architecture

The demo calculator is laid out with one table. The Jamie's Calculator heading and the expr field (and their td/tr containers) have been placed in a

`<thead>`

; a `<tbody>`

holds the rest of the content.Key labels

The key now reads , the key now reads , the key now reads , and the key now reads . Re the last key, the

`√`

radical symbol and the `text-decoration:overline;`

vinculum over the x don't quite connect, but that's life.Dot, binary, minus

`var decimalsign = " . ";`

is now `var decimalsign = ".";`

and `var base_2 = Math.LN10;`

is now `var base_2 = Math.LN2;`

. Also, the negativesign-inputting key has been replaced with a key that when clicked converts a number to its additive inverse:`<input type="button" value=" +|- " onclick="addinverse(this.form);">`

function addinverse(form) { form.expr.value = - form.expr.value; }

All right, here we go:

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