Permalink
Browse files

fix bug in handling of arrow keys; allow sig figs to be controlled

  • Loading branch information...
1 parent 522d21d commit 10657775d590f72451421ca2d71459afc5b00401 Ben Crowell committed Apr 6, 2009
Showing with 70 additions and 29 deletions.
  1. +3 −3 Complex.js
  2. +27 −1 Num.js
  3. +4 −8 Parser.js
  4. +6 −2 Terminal.js
  5. +1 −1 Test.js
  6. +29 −14 index.html
View
@@ -70,13 +70,13 @@ com.lightandmatter.Complex =
c.ceil = function() {return com.lightandmatter.Complex(Math.ceil(c.x),Math.ceil(c.y));};
c.toString = function() {
- if (c.y===0) {return c.x.toString();}
+ if (c.y===0) {return com.lightandmatter.Num.convert_to_string(c.x);}
if (c.x===0) {
if (c.y==1) {return 'i';}
if (c.y== -1) {return '-i';}
- return c.y.toString()+' i';
+ return com.lightandmatter.Num.convert_to_string(c.y)+' i';
}
- return c.x + ' + ' + c.y + ' i';
+ return com.lightandmatter.Num.convert_to_string(c.x) + ' + ' + com.lightandmatter.Num.convert_to_string(c.y) + ' i';
};
return c;
View
28 Num.js
@@ -163,14 +163,38 @@ com.lightandmatter.Num.is_zero = function(c) {
};
com.lightandmatter.Num.is_real = function(c) {
- //return true;
var t = com.lightandmatter.Num.num_type(c);
if (t=='r') {return true;}
if (t=='c') {return c.y===0;}
if (t=='q') {return true;}
if (t=='l') {return c.is_real();}
};
+com.lightandmatter.Num.convert_to_string = function(x) {
+ var t = com.lightandmatter.Num.num_type(x);
+ if (t=='r') {
+ s = x.toString();
+ if (x!=Math.floor(x)) {
+ try { // toExponential and toFixed don't exist before JS 1.5
+ var prec = com.lightandmatter.Num.precision;
+ var a = Math.abs(x);
+ if (a>1e-4 && a<1e4) {
+ s=x.toFixed(prec);
+ }
+ else {
+ s=x.toExponential(prec);
+ }
+ }
+ catch(foo) {}
+ }
+ s = s.replace(/NaN/,"undefined");
+ return s;
+ }
+ else {
+ return x.toString();
+ }
+ };
+
// typically we promote to the "higher" type:
com.lightandmatter.Num.height = function (t) {
return {'q':1,'r':2,'c':3,'l':4}[t];
@@ -179,3 +203,5 @@ com.lightandmatter.Num.height = function (t) {
com.lightandmatter.Num.debug = function(s) {
document.getElementById("debug").innerHTML=document.getElementById("debug").innerHTML+' '+s+' ';
};
+
+com.lightandmatter.Num.precision = 5; // number of digits of precision to maintain on output
View
@@ -15,11 +15,7 @@
// lc_lambda z = lambda "order of magnitude" operator
// array z = list of lists of q's and a_q's.
// These would be useful in the test suite.
-// Array thing is awkward right now because I don't have any easy way to form nested lists syntactically;
-// can do (1,(2,3)), but because it's left-associative, ((1,2),(3,4)) evaluates to [1,2,[3,4]].
-// This makes it impossible to use arrays to check LC numbers in test suite, which was the main application of arrays I had in mind.
-// Possible solution is to have a flatten operator. I'm not clear on how to implement something like JS's [[1,2],[3,4]] in my parser,
-// and I'm not sure I want to dedicate square brackets to this purpose. I can stick dummy elements on the front, like (0,(1,2)), but that's lame.
+// It's awkward and wrong that the code for evaluating the parse tree is in Parser. It should be in its own module.
var com;
if (!com) {com = {};}
@@ -73,7 +69,8 @@ com.lightandmatter.Parser =
};
// get and set the variable by calling this function rather that by looking something up in the symbol table:
this.sym_side_effect = {
- 'levi_civita_n':com.lightandmatter.LeviCivita.change_n
+ 'levi_civita_n':com.lightandmatter.LeviCivita.change_n,
+ 'precision':function (p) {com.lightandmatter.Num.precision = p;}
};
this.builtin_constants = {}; // They're not allowed to overwrite these.
for (var builtin in this.sym) {
@@ -198,8 +195,7 @@ com.lightandmatter.Parser =
// Intervene to keep it from flattening arrays of arrays, which is what toString() normally does on arrays.
s = this.array_to_string(s);
}
- s = s.toString().replace(/NaN/,"undefined");
- return s;
+ return com.lightandmatter.Num.convert_to_string(s);
};
this.array_to_string = function(a) {
View
@@ -42,6 +42,7 @@
// - The default in Firefox, for example, is to have a serif font for html text, and sans serif for user input. This would make
// the user's input suddenly change in style as it scrolled upward after the user hit enter. To avoid this, the default is
// to set serif as the input style.
+// Emacs keybindings: ctl-p works in firefox without firemacs, doesn't work with firemacs. Doesn't work with konqueror. Works in galeon.
//
// To do:
// Provide a way for the user to clear the terminal, and call a handler routine when that happens.
@@ -110,12 +111,15 @@ com.lightandmatter.Terminal =
try { code = e.charCode; } catch (foo) {}
try { code = e.keyCode; } catch (foo) {}
if (code==0) {try { code = e.which; } catch (foo) {}} // necessary for ctl keys in FF
+ var special = false;
+ try {special=(e.which!==undefined && e.which===0)} catch (foo) {}
var enter = 13; // unicode for enter key
+ // Most browsers don't generate a keypress for arrow keys. Firefox does.
var up_arrow = 38;
var down_arrow = 40;
var ch = String.fromCharCode(code);
- var go_up = (e.ctrlKey && ch=='p') || (code==up_arrow);
- var go_down = (e.ctrlKey && ch=='n') || (code==down_arrow);
+ var go_up = special && ((e.ctrlKey && ch=='p') || (code==up_arrow));
+ var go_down = special && ((e.ctrlKey && ch=='n') || (code==down_arrow));
if (go_up || go_down) {
if (go_up) {t.in_history --;}
if (go_down) {t.in_history ++;}
View
@@ -45,7 +45,7 @@ com.lightandmatter.Test =
["((1,2),3)==(1,2,3)",false], // closed_array, parens have extra significance beyond grouping in the case of arrays
["array[1/(1-d)]==[[0,1],[1,1],[2,1],[3,1],[4,1]]",true],
["array(exp(d))==[[0,1],[1,1],[2,0.5],[3,0.16666666666666666],[4,0.041666666666666664]]",true],
- ["array cos sin cos sin d==[[0,0.6663669686453382],[2,0.2014303014197919],[4,-0.0298164555452246],[6,-0.05417070055498078],[8,0.047167710820525316]]",true],
+ ["array cos sin cos sin d==[[0,0.6663669686453381],[2,0.20143030141979187],[4,-0.029816455545224607],[6,-0.05417070055498075],[8,0.047167710820525295]]",true],
["(1+d)^pi"],
["d^pi",null],
["[sqrt(d+d^2)]^2","d+d^2"],
View
@@ -47,17 +47,18 @@
<h2>About Inf</h2>
+<h3>Basic use</h3>
+
<p>
Inf is a calculator that can handle infinite and infinitesimal numbers.
</p>
<p>
Special constants and variables:
</p>
<ul>
-<li> i -- the square root of -1
-<li> pi -- the ratio of a circle's circumference to its diameter
<li> d -- an infinitesimally small number
-<li> levi_civita_n -- sets the number of terms to maintain in series expansions of Levi-Civita numbers
+<li> pi -- the ratio of a circle's circumference to its diameter
+<li> i -- the square root of -1
</ul>
<p>
Built-in operators:
@@ -79,19 +80,8 @@
<li> asin, acos, atan
<li> sqrt, abs
<li> exp, ln
-<li> floor, ceil
-<li> array -- converts a Levi-Civita number, complex number, or rational number to a representation in terms of an array
</ul>
<p>
-User-defined functions:
-</p>
-<ul>
-<li> f x = x^2
-</ul>
-<p>
-You can use up-arrow and down-arrow to get back lines you've typed in previously.
-</p>
-<p>
Inf represents infinite and infinitesimal quantities as <a href="http://en.wikipedia.org/wiki/Levi-Civita_field">Levi-Civita numbers</a>.
The Levi-Civita numbers obey all the same <a href="http://en.wikipedia.org/wiki/First-order_logic">elementary</a> axioms of arithmetic as the real numbers.
If you try evaluating the expression 1/0, you'll see that it comes up as undefined, not an infinite number. This is because one of the elementary
@@ -131,6 +121,31 @@
To learn more about the application of infinitesimals
to calculus, see my <a href="http://www.lightandmatter.com/calc/">free calculus book</a>.
</p>
+<h3>Fancy features</h3>
+<p>
+More special variables:
+</p>
+<ul>
+<li> levi_civita_n -- sets the number of terms to maintain in series expansions of Levi-Civita numbers
+<li> precision -- sets the number of digits of precision to print in output
+</ul>
+<p>
+More built-in functions:
+</p>
+<ul>
+<li> floor, ceil
+<li> array -- converts a Levi-Civita number, complex number, or rational number to a representation in terms of an array
+</ul>
+<p>
+User-defined functions:
+</p>
+<ul>
+<li> f x = x^2
+</ul>
+<p>
+You can use up-arrow and down-arrow to get back lines you've typed in previously.
+</p>
+
<h2>Open source</h2>
<p>
Inf is open source software by <a href="http://www.lightandmatter.com/area4author.html">Ben Crowell</a>

0 comments on commit 1065777

Please sign in to comment.