-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
787 lines (687 loc) · 34.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="Assets/reset.css">
<link rel="stylesheet" href="Assets/style.css">
<link rel="stylesheet" href="Assets/dark.css" id="theme">
<!-- import external stuff -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.9/ace.js"></script>-->
<script src="ace-src-min/ace.js"></script>
<link href='https://fonts.googleapis.com/css?family=Poiret+One' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Raleway:300' rel='stylesheet' type='text/css'>
<link href="https://fonts.googleapis.com/css?family=Comfortaa:300,400" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Advent+Pro:200,400" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab:100,300" rel="stylesheet">
<!-- Local scripts -->
<script src="tools.js"></script>
<script src="bigMath.js"></script>
<script src="parseExpr.js"></script>
<script src="math.js"></script>
<script src="exprMath.js"></script>
<script src="arbitrary.js"></script>
<script src="autoTest.js"></script>
<script src="simpExpr.js"></script>
<script src="vars.js"></script>
<script src="toString.js"></script>
</head>
<body>
<div id="mainPage">
<div id="input" title="Please insert mathy things"></div>
<br>
<div class="button btnEnter btnActive" onclick="parseAndCalc()">Enter
<div class="cb cbAuto cbInactive cbInBtn" onclick="toggleEnter()" title="Toggle automatic evaluation"></div>
</div>
<a id="ans" title="Answer in decimal form">1.4142135623730951</a>
<div id="rerender"></div>
</div>
<div class="extraBox" id="features" onclick="toggleCollapse(featColEl, featBoxEl, featCollapsed)"
title="This is a great place to get started">
<h1>Functions and operators by
<a href="https://en.wikipedia.org/wiki/Order_of_operations">precedence</a>
</h1>
<div id="featCollapseBtn" class="collapseBtn">></div>
<div class="colBox" id="featBox">
<div id="featNames" class="featCol" title="Name: Type this or click the button to use"></div>
<div id="featAlis" class="featCol"
title="Alias: If you type one of these it'll automatically change into the name on the left"></div>
<div id="featDescs" class="featCol" title="Description: Do I really need to describe the description?">
</div>
</div>
</div>
<script>//{
var featureEl = document.getElementById("features");
var nameEl = document.getElementById("featNames");
var aliEl = document.getElementById("featAlis");
var descEl = document.getElementById("featDescs");
var featColEl = document.getElementById("featCollapseBtn");
var featBoxEl = document.getElementById("featBox");
var featOpList = [
[{ name: ',', description: 'Tuple: For multiple arguments or keeping things apart' }],
[
{ name: '⋎', description: 'Logical or' },
{ name: '⊻', description: 'Logical xor: a⊻b=(a⋎b)⋏¬(a⋏b)' },
],
[
{ name: '⋏', description: 'Logical and' }
],
[
{ name: '=', description: 'Equals' },
{ name: '≈', description: 'Approximately equal to: a≈b ⇔ |a/b-1|<0.001' },
{ name: '<', description: 'Less than' },
{ name: '>', description: 'Greater than' },
{ name: '≤', description: 'Less than or equal to' },
{ name: '≥', description: 'Greater than or equal to' }
],
[
{ name: '+', description: 'Plus' },
{ name: '-', description: 'Minus' }
],
[
{ name: '×', description: 'Times' },
{ name: '/', description: 'Divided by' }
],
[
{ name: '^', description: 'To the power of' }
]
]
var featFuncList = [
[
{ name: 'sin', description: 'Sine' },
{ name: 'cos', description: 'Cosine' },
{ name: 'tan', description: 'Tangent: tan(a)=sin(a)/cos(a)' },
{ name: 'asin', description: 'Arcsine' },
{ name: 'acos', description: 'Arccosine' },
{ name: 'atan', description: 'Arctangent' },
{ name: 'ln', description: 'Natural logarithm' },
{ name: 'lg', description: 'Decadic logarithm' },
{ name: '√', description: 'Square root: (√a)^2=a' },
{ name: '∛', description: 'Cube root: (∛a)^3=a' },
{ name: '∜', description: 'Tesseract root: Is that even a word?' },
{ name: '!', description: 'Factorial: !(a)=Π(1,a,k) <a style="color: red">- !(a) is incorrect notation</a>' },
{ name: '‼', description: 'Semifactorial: ‼(a)=Π(0,a/2-1,2×(a-k)) <a style="color: red">- ‼(a) is incorrect notation</a>' },
{ name: 'abs', description: 'Absolute value: abs(a)=|a|, if a≥0: |a|=a, else: |a|=-a for all real a' },
{ name: 'sgn', description: 'Sign: a<0 ⇔ sgn(a)=-1, a=0 ⇔ sgn(a)=0, a>0 ⇔ sgn(a)=1' },
{ name: '⌊', description: 'Floor: ⌊a⌋ is the greatest integer less than or equal to a' },
{ name: '⌈', description: 'Ceiling: ⌈a⌉ is the smallest integer greater than or equal to a' },
{ name: 'round', description: 'Round: round(a) is the integer closest to a, fives rounded up' },
{ name: '¬', description: 'Logical not: ¬t=0, ¬f=1, ∀t>0,f≤0' },
{ name: 'date'/*📅'*/, description: '<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse">Javascript date</a>: date(a) returns number of ms between 00:00 jan 1 1970 and a' },
{ name: 'years', description: 'Years: ms to years' },
{ name: 'weeks', description: 'Weeks: ms to weeks' },
{ name: 'days', description: 'Days: ms to days' },
{ name: 'hours', description: 'Hours: ms to hours' },
{ name: 'minutes', description: 'Minutes: ms to minutes' },
{ name: 'seconds', description: 'Seconds: ms to seconds' },
],
[
{ name: 'Σ', description: 'Sum: Σ(a,b,f(k))=f(a)+f(a+1)+...+f(b-1)+f(b)' },
{ name: 'Π', description: 'Product: Π(a,b,f(k))=f(a)×f(a+1)×...×f(b-1)×f(b)' },
{ name: 'lgn', description: 'Logarithm to base n: lgn(n,x)=ln(x)/ln(n)' },
{ name: 'P', description: 'Permutations: P(n,r)=n!/(n-r)!' },
{ name: 'C', description: 'Combinations: C(n,r)=P(n,r)/r!' },
{ name: 'js', description: 'Javascript: js(a) executes a as JS code' }
]
];
var rowColors = ['rgba(255,255,255,0.2)', 'rgba(0,0,0,0)'];
var colorInd = 0;
renderFeatures(featOpList, nameEl, descEl, aliEl, ' featOp');
renderFeatures(featFuncList, nameEl, descEl, aliEl, ' featFunc');
function renderFeatures(featList, nameEl, descEl, aliEl, featNameClass = '') {
for (var i in featList) {
var nameCode = '<div class="featNames">';
var aliCode = '<div class="featAlis">';
var descCode = '<div class="featDescs">';
for (var j in featList[i]) {
var style = ' style="background-color: ' + rowColors[colorInd] + '"';
colorInd++;
colorInd %= rowColors.length;
aliCode += '<div class="feat featAli"' + style + '>';
for (var k in strRepl) {
var found = false;
if (featList[i][j].name == strRepl[k].to) {
aliCode += strRepl[k].from[0];
for (var l = 1; l < strRepl[k].from.length; l++) {
aliCode += ', ' + strRepl[k].from[l];
}
found = true;
break; //Assuming that each operator/function only appears once in strRepl
}
}
if (!found)
aliCode += '<br>';
aliCode += '</div>';
nameCode += '<div class="feat featName' + featNameClass + '">' + featList[i][j].name + '</div>';
descCode += '<div class="feat featDesc"' + style + '>' + featList[i][j].description + '</div>';
}
nameEl.insertAdjacentHTML('beforeend', '<div class="featOoo">' + nameCode + '</div></div>');
descEl.insertAdjacentHTML('beforeend', '<div class="featOoo">' + descCode + '</div></div>');
aliEl.insertAdjacentHTML('beforeend', '<div class="featOoo">' + aliCode + '</div></div>');
}
}
$('.featCol').click(function (e) {
e.stopPropagation();
});
$('.featName').click(function (e) {
var el = e.currentTarget;
var inputEl = document.getElementById('input');
var inpVal = editor.getValue();
inpVal = inpVal.substr(0, inputEl.selectionStart) + el.innerHTML + inpVal.substr(inputEl.selectionEnd);
editor.setValue(inpVal);
onInput();
});
var featCollapsed = { val: false };
toggleCollapse(featColEl, featBoxEl, featCollapsed);
function toggleCollapse(colBtnEl, contBoxEl, isCollapsed, colProp = 'height', btnCode = ['v', '>']) {
if (isCollapsed.val) {
contBoxEl.style = colProp + ': auto';
colBtnEl.innerHTML = btnCode[0];
}
else {
contBoxEl.style = colProp + ': 4px';
colBtnEl.innerHTML = btnCode[1];
}
isCollapsed.val = !isCollapsed.val;
}
//}</script>
<div class="extraBox" id="prefs"
title="Not happy with the behavior of my calculator? If you're lucky I might just have a setting for it">
<h1>Preferences</h1>
<div id="prefCollapseBtn" class="collapseBtn" onclick="toggleCollapse(prefColEl, prefBoxEl, prefCollapsed)">>
</div>
<div class="colBox" id="prefBox">
<div class="extraSubBox pref" title="Not a fan of dark colors? Try something else">
Theme
<br>
<select id="themeDD" onchange="setTheme()">
<option value="bright">Bright</option>
<option value="dark" selected>Dark</option>
<option value="soft">Soft</option>
<option value="black">Black</option>
</select>
</div>
<div class="extraSubBox pref"
title="Into heavy math? You might wanna turn this on for a more responsive (and boring) experience">
Press button to calculate
<div class="cb cbAuto cbInactive" onclick="toggleEnter()"></div>
</div>
<div class="extraSubBox pref"
title="Don't want my calculator to mess with your beautiful input? I recommend leaving this on in most cases but suit yourself">
Interpret text input
<div class="cb cbReRend" onclick="toggleReRend()"></div>
</div>
<div class="extraSubBox pref"
title="I recall this being a little buggy when doing some weird stuff... Leave it on as long as your parenthesis act nice">
Auto complete parenthesis
<div class="cb cbAcPar" onclick="toggleAcPar()"></div>
</div>
<div class="extraSubBox pref"
title="This is sort of the whole point of this calculator but sure, go ahead and turn it off">
Simplify expression
<div class="cb cbSimp" onclick="toggleSimp()" title="Really?"></div>
</div>
<div class="extraSubBox pref"
title="This stuff is mostly for debugging, except the Max time, which you might wanna increase for heavier calculations">
If simplified is far from original
<br>
<select id="wrongSimpDD">
<option value="alert">Alert</option>
<option value="original">Use original</option>
<option value="simple" selected>Use simplified</option>
</select>
<br> If max time is exceeded
<br>
<select id="outOfTimeDD">
<option value="alert">Alert</option>
<option value="original">Use original</option>
<option value="simple" selected>Use simplified</option>
</select>
<br> Max time
<br>
<input type="number" value="500" class="inputPref" id="wsTime" onchange="setWrongSimp()"
title="If it takes longer than this, the calculator will simply give up">
<select id="wsTimeUnit" onchange="setWrongSimp()"
title="Time unit for max time, just so you won't have to type a bunch of zeros">
<option value="0.0001" title="Microseconds">μs</option>
<option value="1" title="Milliseconds" selected>ms</option>
<option value="1000" title="Seconds, duh">s</option>
<option value="60000" title="Minutes... why though?">m</option>
<option value="3600000" title="No.">h</option>
</select>
</div>
<div class="extraSubBox pref" title="Please, please, please enable this one">
Remove zeros in fractions
<div class="cb cbDecFracs cbInactive" onclick="toggleDecFracs()" title="Yassss click it"></div>
</div>
</div>
</div>
<script>//{
var prefEl = document.getElementById("prefs");
var prefColEl = document.getElementById("prefCollapseBtn");
var prefBoxEl = document.getElementById("prefBox");
var prefCollapsed = { val: false };
toggleCollapse(prefColEl, prefBoxEl, prefCollapsed);
function setTheme(name = document.getElementById('themeDD').value) {
document.getElementById('theme').href = 'Assets/' + name + '.css';
}
function setWrongSimp() {
var timeUnit = document.getElementById('wsTimeUnit').value;
maxTime = document.getElementById('wsTime').value * timeUnit;
}
//}</script>
<div class="extraBox" id="news" title="This is where I explain my roughly annual updates">
<h1>News</h1>
<div id="newsCollapseBtn" class="collapseBtn" onclick="toggleCollapse(newsColEl, newsBoxEl, newsCollapsed)">>
</div>
<div class="colBox" id="newsBox">
<div class="extraSubBox news">
<span class="date">2021-10-11</span>
<h2>date() Improvement</h2>
I ditched the 'now' keyword because '' is shorter. So, date() now returns the JS representation of the current time.
</div>
<div class="extraSubBox news">
<span class="date">2021-10-01</span>
<h2>date() Bugfix</h2>
Dates with negative JS representations would previously mess up certain types of calculations.
</div>
<div class="extraSubBox news">
<span class="date">2021-09-24</span>
<h2>date() Fix and Improvement</h2>
The date() function seemed to only work properly for one instance at a time. I've fixed that and added the keyword 'now' to get the current time. I also added some more time conversion functions, although I deliberately excluded months since they vary so much in length.
</div>
<div class="extraSubBox news">
<span class="date">2021-09-17</span>
<h2>Switching from bigInt to BigInt</h2>
The old bigInt library I was using has been removed, presumably because the built-in BigInt is better.
This broke my entire calculator but I've changed a bunch of syntax and it seems to work again.
</div>
<div class="extraSubBox news">
<span class="date">2020-08-25</span>
<h2>Important Bugfix</h2>
As kindly pointed out in <a href="https://github.com/Effektgubben/Calculator/issues/3">this issue,</a>
'*' was changed to '××' rather than '×'.
</div>
<div class="extraSubBox news">
<span class="date">2019-12-18</span>
<h2>Critical Simplification Bug Fixed</h2>
Sums were incorrectly simplified when containing products or quotients of functions of k.
</div>
<div class="extraSubBox news">
<span class="date">2018-02-11</span>
<h2>Stupid Inequality Bug Fixed</h2>
I just realized that ≤ calls the greater or equal function while ≥ calls the less or equal function. I
have now reversed them to fix this.
<br>
<br>
<h2>Hash Encoding</h2>
Backwards compatibility for sharing links to expressions is now broken since the <a
href="https://en.wikipedia.org/wiki/Fragment_identifier">hash</a> must be <a
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent">encoded</a>
to avoid trouble with special characters. This might also cause problems if you try to write an
expression directly to the hash.
</div>
<div class="extraSubBox news">
<span class="date">2017-12-10</span>
<h2>Syntax Highlighting</h2>
Added syntax highlighting to the input box using
<a href="https://ace.c9.io/">Ace</a>.
<br> The highlighting language is currently set to Haskell so it might not make sense for JS code.
However, I
find Haskell more suitable for mathematical expressions since it's a more mathematical language.
</div>
<div class="extraSubBox news">
<span class="date">2017-12-05</span>
<h2>Simplification Update</h2>
a^(b/c) is now simplified to (a^b)^(1/c). This might seem stupid and sometimes it is but I think it's
worth it for the times
when a^b turns out to be something nice.
<br> a^b×a^c is now simplified to a^(b+c).
</div>
<div class="extraSubBox news">
<span class="date">2017-12-03</span>
<h2>a/1 Simplified to a</h2>
Another weird but not incorrect simplification fixed.
</div>
<div class="extraSubBox news">
<span class="date">2017-11-25</span>
<h2>Another Important Bugfix</h2>
Turns out a+0 and 0+a were simplified to 0 for certain non-zero a.
<br>
<br>
<h2>Simplify a×1</h2>
Might sound obvious but 1×a and a×1 will now always be simplified to a.
</div>
<div class="extraSubBox news">
<span class="date">2017-11-10</span>
<h2>Important Bugfix</h2>
I just found out that a^b, was floored for negative b. This is fixed now.
</div>
<div class="extraSubBox news">
<span class="date">2017-11-09</span>
<h2>Shorter Fractions</h2>
If there are zeros at the end of a numerator or a denominator, those can be cut off while moving the
decimal point of the
denominator or the numerator respectively. Turn it on in the preferences and try something like
1751/7900
to see how it works.
<br>
<br>
<h2>Printing Roots</h2>
a^(1/2), a^(1/3) and a^(1/4) will now be printed as √a, ∛a and ∜a respectively.
</div>
<div class="extraSubBox news">
<span class="date">2017-11-08</span>
<h2>Product Simplification</h2>
As promised yesterday :)
<br>
<br>
<h2>BigInt</h2>
I've always been using bigInts but they would usually turn into ordinary JS numbers in the
simplification and were thus quite
useless. This shouldn't be the case anymore.
<br> Working with bigInts means the calculator can handle arbitrarily big numbers and should avoid
certain rounding
errors.
<br> Note that bigInts are used in the parsing and simplification only and still have to be converted to
JS numbers
before the final answer is calculated. Thus, the answer cannot be arbitrarily big, even though the
numbers
in the simplified expression can.
</div>
<div class="extraSubBox news">
<span class="date">2017-11-07</span>
<h2>News Box</h2>
Added this news box where I'll tell you about new features and stuff.
<br>
<br>
<h2>Updated Expression Simplification</h2>
Sums and powers can now be simplified. I've also fixed a few weird but not really wrong simplifications.
<br> Product simplification coming soon.
<br>
<br>
<h2>Simplification Settings</h2>
Added settings so you can decide what happens if simplication seems wrong or takes too long time.
<br> When an expression is simplified, its answer will be compared to that of the raw expression unless
it exceeds
your time limit.
</div>
</div>
</div>
<script>//{
var newsEl = document.getElementById("news");
var newsColEl = document.getElementById("newsCollapseBtn");
var newsBoxEl = document.getElementById("newsBox");
var newsCollapsed = { val: false };
toggleCollapse(newsColEl, newsBoxEl, newsCollapsed);
//}</script>
<div class="extraBox" id="about">
<h1>About</h1>
<div id="aboutCollapseBtn" class="collapseBtn" onclick="toggleCollapse(aboutColEl, aboutBoxEl, aboutCollapsed)">
></div>
<div class="colBox" id="aboutBox">
This is a hobby project that I've been working on for a while. The original idea was to simplify expressions
and use infinite
series to get rid of rounding errors. While it does try to simplify your expression (check it out below the
enter
button), there's no infinite series magic going on behind the scenes... yet...
<br> If you write π, it will simply use a predefined constant with a decent amount of decimal places.
<br>
<br> Sooo - when will I do the series stuff? Well, first of all, the expression objects that your text input
is parsed
to kinda suck. Way too many strings and stuff.
<br> Second, I have <a href="https://github.com/Effektgubben/Noodle">another project</a> that I'm pretty
excited about. My hope is that that project will provide a foundation for
building a new and more powerful calculator.
<br>
<br> Pro tip: don't write js(setTheme('darkOld'))
</div>
</div>
<script>//{
var aboutEl = document.getElementById("about");
var aboutColEl = document.getElementById("aboutCollapseBtn");
var aboutBoxEl = document.getElementById("aboutBox");
var aboutCollapsed = { val: false };
toggleCollapse(aboutColEl, aboutBoxEl, aboutCollapsed);
function setTheme(name = document.getElementById('themeDD').value) {
document.getElementById('theme').href = 'Assets/' + name + '.css';
}
//}</script>
<script>
//Variables{
var inputBox = document.getElementById("input");
var ansBox = document.getElementById("ans");
var btnEnter = document.getElementById("btnEnter")
var cbAuto = document.getElementById("cbAuto");
var rerendBox = document.getElementById("rerender");
var uvarInd = 0;
var autoCalc = true;
var simp = true;
var acPar = true;
var reRender = true;
//Ace{
var Range = require('ace/range').Range;
var editor = ace.edit('input');
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/haskell");
editor.setOptions({
fontSize: '3.3vw',
maxLines: 18446744073709551616
});
editor.renderer.setShowGutter(false);
//}
var maxTime = 500;
//}
//inputBox.value.onchange = parseAndCalc()
if (!(location.hash == '' && location.href[location.href.length - 1] != '#')) {//If hash "exists"
editor.setValue(decodeURIComponent(location.hash).substr(1));
}
rerenderStr(editor.getValue());
parseAndCalc();
/*
inputBox.focus();
inputBox.selectionStart = 0;
inputBox.selectionEnd = inputBox.value.length;
*/
var prevStr = '';
var lockInput = false;
var onInput = function () {
var str = editor.getValue();
if (!lockInput && str != prevStr) {
rerenderStr(str);
if (autoCalc) {
try {
parseAndCalc();
}
catch (e) {
console.info('Failed to parse "' + editor.getValue() + '"');
err('Invalid expression');
}
}
var hash = encodeURIComponent(editor.getValue());
/*for(var i in strRepl){
hash.replace(strRepl[i].to, strRepl[i].from[0]);
}*/
document.location.hash = hash;
}
prevStr = str;
}
editor.getSession().on('change', onInput);
function toggleEnter() {
autoCalc = !autoCalc;
//$(".btnEnter").toggleClass("btnActive");
$(".cbAuto").toggleClass("cbInactive");
}
var failed = false;
function err(msg) {
ansBox.innerHTML = msg;
failed = true;
}
function toggleSimp() {
simp = !simp;
$('.cbSimp').toggleClass('cbInactive');
parseAndCalc();
}
function parseAndCalc() {
failed = false;
ansBox.innerHTML = "";
if (editor.getValue().length > 0) {
var expr = purifyStr(editor.getValue());
expr = parseExpr(expr);
if (simp) {
var wrongSimpOpt = document.getElementById('wrongSimpDD').value;
var ootOpt = document.getElementById('outOfTimeDD').value;
var simpExpr = simplify(expr);
//Max time stuff {
var time;
var nootAns;
var ootExpr;
var nootExpr;
//Set ootExpr{
//If simple should be used for out of time
if (ootOpt !== 'original') {
ootExpr = simpExpr;
nootExpr = expr;
}
else {
ootExpr = expr;
nootExpr = simpExpr;
}
//}
var startTime = new Date().getTime();
var ootAns = calc(ootExpr);
time = new Date().getTime() - startTime;
var oot = time > maxTime;
//If oot was calculated quickly enough, calculate nootAns
if (!oot) {
nootAns = calc(nootExpr);
}
else if (ootOpt === 'alert') {
alert('Max time exceeded');
nootAns = 'Timeout';
}
var origAns;
var simpAns;
if (ootOpt === 'original') {
origAns = ootAns;
simpAns = nootAns
}
else {
origAns = nootAns;
simpAns = ootAns
}
//}
var ans;
if (!aprEq(origAns, simpAns) && !oot && !(typeof origAns === 'object' && typeof simpAns === 'object')) {
ans = simpAns;
if (wrongSimpOpt === 'alert') {
alert("Whoops! Something went wrong when simplifying your expression.\nThe simplified answer is " + simpAns + " while the non simplified answer is " + origAns + ".\nIf this doesn't feel like an acceptable rounding error, please send me your expression at effektgubben" + " @."[1] + "gmail.com.");
ans = origAns; //Alert means you don't trust simplified answer, right?
}
console.warn('Simplified far from original')
}
else if (oot) {
ans = ootAns;
}
else
ans = simpAns;
rerendBox.innerHTML = exprToString(simpExpr, '<a class="expr" title="Simplified expression, perhaps a nice fraction if you\'re lucky">', '</a>');
}
else {
var ans = calc(expr);
rerendBox.innerHTML = exprToString(expr, '<a class="expr" title="Enable simplify to see simplified expression here">', '</a>');
}
if (!failed)
ansBox.innerHTML = ans;
}
}
function toggleAcPar() {
acPar = !acPar;
$('.cbAcPar').toggleClass('cbInactive');
}
function toggleReRend() {
reRender = !reRender;
$('.cbReRend').toggleClass('cbInactive');
}
var oldCursorPos;
var oldLen;
var newLen;
function rerenderStr(str) {
oldCursorPos = editor.session.selection.getRange().start.column;
oldLen = str.length;
var oldStr = str;
if (reRender) {
for (var i = 0; i < str.length; i++) {
var newStr = "";
var oldLength = 0;
var found = false;
for (var replInd = 0; replInd < strRepl.length && !found; replInd++) {
var from = strRepl[replInd].from;
for (var fromInd = 0; fromInd < from.length && !found; fromInd++) {
str = editor.getValue();
if (begins(from[fromInd], str.substr(i))) {
found = true;
oldLength = from[fromInd].length;
newStr = strRepl[replInd].to;
//str = str.substr(0, i) + newStr + str.substr(i + oldLength);
var row = 0;//editor.selection.lead.row;
editor.session.replace(new Range(row, i, row, i + oldLength), newStr);
//Compensate for what seems to be a bug in Ace
if (oldLength - newStr.length <= 1)
editor.selection.setRange(new Range(row, i - 0, row, i - 0));
}
}
}
}
}
if (acPar) {
/*if (str[str.length - 1] == '(')
editor.setValue(editor.getValue() + ')');
else */if (str[str.length - 1] == '⌊') {
editor.setValue(editor.getValue() + '⌋');
}
else if (str[str.length - 1] == '⌈') {
editor.setValue(editor.getValue() + '⌉');
}
}
}
function purifyStr(str) {
var jsInd = str.indexOf("js(");
var jsEnd;
while (jsInd > -1) {
jsEnd = skipPars(str, jsInd + 2, true);
var jsStr = str.substring(jsInd + 3, jsEnd - 1);
str = str.substring(0, jsInd) + eval(jsStr) + str.substr(jsEnd);
jsInd = str.indexOf("js(");
}
//Turn dates into JS number representation
var dateInd = str.indexOf("date");//📅(");
var dateEnd;
while (dateInd > -1) {
dateEnd = skipPars(str, dateInd + 4, true);
var dateStr = str.substring(dateInd + 5, dateEnd - 1);
if (dateStr === '') {
dateStr = Date.now();
}
else {
dateStr = '('+Date.parse(dateStr + ' gmt')+')';
}
str = str.substring(0, dateInd) + dateStr + str.substr(dateEnd);
dateInd = str.indexOf("date(");
}
str = str.replace(/\ /g, '');//Remove spaces before parsing
//Make floor and ceil parseable
str = str.replace(/\⌊/g, '⌊(');
str = str.replace(/\⌈/g, '⌈(');
str = str.replace(/[⌋⌉]/g, ')');
return str;
}
document.onkeypress = function (e) {
if (e.keyCode == 10 || e.keyCode == 13) {//If enter is pressed
location.href = 'https://www.google.se/search?q=' + editor.getValue();
}
}
</script>
</body>
</html>