Show HN: Kalk, A calculator with math syntax, complex numbers
kalk.strct.net
kalk.strct.net
Superscripts: firstly numerals, like 3² = 9, but ideally then letters as well, e.g. 3ⁱ = 3^i. Might be suitable to support superscripts to define nth roots, too, e.g. ⁵√. tan¯¹ can be a thing too.
Subscripts: same deal; log₂ 31 = log(31, 2).
Proper arithmetic operators: times (×), minus (−), division (÷). Also fraction slash (⁄), which can be preceded and succeeded either by normal numerals (e.g. 12⁄34) or by superscripts and subscripts (e.g. ⁵⁶⁄₇₈). (Full font support for fraction slash is weak, but generally tolerable even if imperfect.) Also the prebaked fractions like ½ and ⅜.
Also mixed fractions: I reckon 1 2/3 should be parsed as 1+2/3, not 12/3. Them Unicode fractions can join in here too, with 1⅔ (using a prebaked fraction) being 1+2/3, or 1²³⁄₄₅ (using superscript, fraction slash, subscript) being 1+23/45.
Cubic and quartic roots: ∛ and ∜.
At this point it’s worth just looking through the whole Symbol, Math Unicode category and implementing everything applicable. Hmm, set operations, lots of fun symbols there.
Lots of fun spaces too, e.g. where I wrote log₂ 31 earlier I wanted to use NARROW NO-BREAK SPACE after the ₂, but alas, HN normalises it to a plain old space. That space gets used as a number grouping separator especially, avoiding the whole ,/. localisation problem without being too wide.
1) It handles 1 000 000 * 3 just fine, but returns 3000000. So it would be nice if it would format output as 3 000 000 as well for readability. At least as an optional setting (even though I have no idea why someone wouldn't want that).
2) Some way of referencing last result. Often you need to perform operations step by step, when, say, estimating something. So either 10 \n + 20 \n * 3 ..., or just referencing last output like some special variable. I though I could do it more verbosely by just x = 10; x = x + 20 ..., but that gives "Variable references itself."
3) Not sure about this one, since this is not something natural at all, and I don't know what to propose to make it natural (at least without proposing map/filter/reduce and some sort of multi-line inputs). Neither is it a real problem, but it's just something I probably use a calculator most often: simply adding up a dozen of numbers, copied from somewhere as a column. So currently I must first open vim, convert it to and expression bc will handle ( 15 + 21 + 32 ...), and then paste in to bc. With kalk it would be similar. Would be great if one could skip that step since this (I assume) would be a common use-case. But, yeah, I don't have an actual suggestion on how to naturally address that.
Also, it actually does have a way to reference the last result, I forgot to document it. It's a variable called "ans"!
The last thing, yeah, that would be neat. I'll think about it...
Copy/pasting? At least each three zeroes could alternate in color, to make it easier to paste the numbers elsewhere.
(The pre-built Linux binary segfaults before it can show the result on my ArchLinux box, but the cargo installed one works like a charm.)
Hm that's strange though, it seems to work for me (also on Arch). What calculation did you do?
>> 1+2
fish: Job 1, './kalk-linux' terminated by signal SIGILL (Illegal instruction)And thanks for fixing the two issues so fast.
As they say, "the answer will surprise you" :)
2 + 2 × 10% = 2.2
At the risk of admitting my ignorance, yet more interested in learning something new: should this answer (2.2) surprise me?On MacOS the answer is 2.4
In kalk it's 2.002
On Windows it's 0.4 in simple calc, and 2 in programmer calc (and there's no % in engineering calc)
> and there's no % in engineering calc
Use 0.1 instead of 10%, it has the same vibe ;)
[0] https://www.google.com/search?gbv=1&q=2%2B2%C3%9710%25&oq=&a...
$ units
You have: 2 + 2 * 10%
Definition: 2.2
ITYM "the answer may surprise you", and in this case it did not.It seems like an expression like "1 * 10%" in kalk is computed as if it means "1% * 10%" => "0.01 * 0.1" => "0.001"
But, the moment you type `%`, it instead replaces `10` with `0.1`. So in the end the screen shows: 2+2x0.1 = 2.2
I guess not as surprising as you expected :-]
2 dup 100 10 */ swap 100 * + .S 220 OK >> e^iπ+1
2.6974097548 + 2.6435590641i
>> e^iπ
1.6974097548 + 2.6435590641i
In fairness, I still don’t know where “e” comes from in complex number notation. The transition from cos(angle) + i sin(angle) seems to become “e^i(angle), lol” and no one explains it further. Before I started doing signal processing, I too would have thought that e^i pi is something like 1.69 + 2.64, so it’s correct, in a certain sense. But perhaps not the common mathematical sense.EDIT: Ah, I see the problem. I suspected that e^iπ was being turned into πe^i incorrectly, but when I typed the open parenthesis for e^() and put my cursor inside it, typing "i" resulted in e^i() rather than e^(i). So it was impossible to parenthesize correctly, and I assumed e^iπ had the usual meaning.
The reason I posted my comment is because e^(i pi) is obviously supposed to be -1+0j. But I see now that I was using the calculator incorrectly (I was doing the equivalent of πe^i) but there was a bug in the parenthesis operator preventing me from typing it that way on mobile.
It really isn't, beyond the fact that exp(1) = 2.718…. Then, by definition exp(it) = exp(1)^(it) = e^it. It isn't much more than a notational artifact as far as I'm concerned. For that matter, raising any base b to a power is equivalently just scaling it by ln(b) and plugging it into exponential function, that is, b^it = cos(ln(b) * t) + i sin(ln(b) * t).
You can throw together a program that will show numerically that this both converges to e^x for real x and that e^ix converges to cos(x)+i*sin(x). You can also find proofs online I’m sure.
I don't think either the numerical or symbolic proofs really help build intuition though. This video [1] kinda helps, although it glosses over the critical fact "multiplying by i rotates counterclockwise 90°."
I agree that this is one way to interpret the result. But is it literally equivalent to matrix multiplication by a 2D rotation matrix? Someone tried to say it was, which I felt skeptical about.
(Also, thank you.)
[cos π/2 -sin π/2] [x] = [0 -1] [x] = [0 - y] = [-y]
[sin π/2 cos π/2] [y] [1 0] [y] [x + 0] [ x]
Compare to:i (x + iy) = ix + i²y = -y + ix
For the general case of rotation in the complex plane, multiplying by a complex number with an absolute value of 1 (i.e. on the unit circle) rotates by that number's angle from the positive reals. Normally you'd write such a number as e^iθ, but since that's what we're trying to get to, we can instead write it as cos θ + i sin θ.
[cos θ -sin θ] [x] = [x cos θ - y sin θ]
[sin θ cos θ] [y] = [x sin θ + y cos θ]
Compare to:(cos θ + i sin θ) (x + iy)
= x cos θ + ix sin θ + iy cos θ + i²y sin θ
= (x cos θ - y sin θ) + i (x sin θ + y cos θ)
Maybe the cause of your skepticism is that it seems unintuitive that complex numbers, which are 2-dimensional in linear-algebra-speak, could be as powerful as 2×2 matrices, which are 4-dimensional. But keep in mind that 2×2 matrices can perform any linear transformation on 2-dimensional vectors using only matrix multiplication, whereas complex number multiplication can only scale and rotate, and you need to use complex addition to get translation, and I'm not sure off the top of my head if it's possible to get shear transformations using complex numbers.
(I'm a former gamedev, so rotation matrices are very intuitive. But for some reason, I never made the connection to complex numbers, except in an abstract math-y sort of way.)
@mayfer also tried to explain to me that it was a rotation matrix. This tweet chain was helpful; posting it here in case anyone needs further convincing: https://twitter.com/theshawwn/status/1400591835052056580
I am afraid this domain is a prime nerd-sniping range, because there are so many variations on building a better, more interactive calculator. No profits here besides learning and great fun!
Nice work btw!
When I input 3^4^5, it outputs infinity.
It also has units, many built-in physical constants, currency conversion, matrices and probably tons of other stuff I don't know about.
I don't think it can be easily ran in a browser though.
>> f(x) = x^2
>> f''(4)
0.9995297887Ignoring these, I really like Kalk!
There is already opensource app for that: microMathematics Plus (μMath+) — Extended visual calculator for Android.[0,1]
[0] https://github.com/mkulesh/micromathematics
[1] https://f-droid.org/packages/com.mkulesh.micromath.plus/
http://isthe.com/chongo/tech/comp/calc/
On Fedora:
sudo dnf install calc ; alias bc=calc
Any chance in the future that Kalk will support quaternion numbers[1]?
Oh my... aren't those really complicated? Haha