Discuss Scratch

Zro716
Scratcher
1000+ posts

Infinite Precision Calculator

Hi everyone! I haven't posted a new project in forever, but I have something worthy to share (well, it has been shared for a while but…)

Some of you may have been frustrated by the limits on numbers in Scratch. Why can't you add 19375325829347345 + 1795316489264198352375 and get a reasonable outcome? Well now you can! And more!


(sorry for the clickbait-tier text :P)

What does it do?
It lets you do math to any precision you want: 3 decimals, 3 thousand decimals, 3 million decimals even (though I would be wary to avoid that much precision). In Scratch, it would truncate large or small values and lead to frustration and/or crying when you need that precision the most.

Besides letting you do more precise calculations, the project can also convert from scientific notation, evaluate expressions, store variables, create counters, and probably more than that….

How do I use it?
The project as of now supports basic math operations, such as addition, subtraction, multiplication, division, integer exponentiation, factorial, square root, nth root, absolute value, and greatest common divisor. I plan to add many more operations, such as trignometric functions, logarithms, noninteger exponentiation… when I can find the necessary calculations….

There are two ways to use these functions. You can use the blocks directly, prefixed EVAL, or you could use the Evaluate() block for more compact calculation. More information about the Evaluation() block below.

Since the library is packaged in one sprite, you can drag the sprite into your backpack and use it for your projects.

How do I use Evaluate()?
You enter an expression like you would in code. In other words, this is what you should enter for each operation:
Operator  | Name                      | Usage     | Example                | Notes
------------------------------------------------------------------------------------------------------------------------
+ Addition A + B 5 + 2.3 = 7.3 Handles subtraction as well
- Subtraction A - B 5 - 2.3 = 2.7 --
* Multiplication A * B 5 * 2.3 = 11.5 Uses long multiplication method
/ Division A / B 5 / 2.3 = 2.1739... Uses the Newton-Raphson method
^ Exponentiation A ^ B 2.3 ^ 5 = 64.36343 Does not support non-integer exponents yet
% or mod Modulus A % B 5 % 2.3 = 0.4 Is not optimized for large differences in magnitude. Use with caution
= Assignment* A = B var = 5 + 2.3 Assigns the value of 5+2.3 to "var", does not compare them
! Factorial A ! 5! = 120 --
sqrt Square Root sqrt A sqrt 5 = 2.2360... Uses the Babylonian method. Very heavy to calculate.
nroot Nth Root nroot A B nroot 5 2.3 = 1.18126... Uses a variation of the Babylonian method. Very heavy to calculate.
abs Absolute Value abs A abs -5 = 5 --
gcd Greatest Common Divisor gcd A B gcd 25 10 = 5 Uses the Euclidean method. Performance may vary.
*Variables that are created with assignment remain for the rest of the session. To clear variables, reclick the green flag. If you want to delete variables in your program, use the Delete Var() block.

You can surround things in (parentheses) to clarify order of operations.

To get the result of the last evaluation, use the “ans” variable.

What bugs are there?
A project this big is full of bugs, I'm sure. Some bugs are a lot tougher to fix than others. When I was updating it recently, I had to patch up some nasty number glitches, and add the scientific number converter.

Some sage advice: be absolutely careful with your inputs. My project lacks a ton of input validation to make up for optimizations. One delicate mistake will probably crash the script. Don't let this discourage you, because I will add input sanitation soon enough.

Anything else?
It's largely been a work in progress for over a year now, and just recently have I been debugging scripts and adding more functions.

I would be very excited if someone with JavaScript experience could help me develop an extension for it. Here's the repository https://github.com/Zro617/scratch-ext-prec-ops

Thanks for reading! Let me know if you have comments, concerns, suggestions, and bugs to report.

Last edited by Zro716 (March 17, 2016 05:19:13)

gtoal
Scratcher
1000+ posts

Infinite Precision Calculator

Good show! Though it had better not be infinite precision - what does it say if you calculate 1/3 ?

Oh - wait a minute - it's *arbitrary* precision, not *infinite* precision :-)

If you ever find yourself extremely bored, here's a problem for you that you may find challenging and which you probably won't find an easy solution for by Googling :-) … design a model of programming that *does* cope with infinite precision without requiring infinite storage… it _can_ be done, but it requires thinking about programming languages and hardware in a completely different way from the von Neumann architecture (which I guess is in itself a big hint).

Just to be clear I'm not talking about rationals. The solution I'm thinking of would cope with, for example, multiplying pi by e…!

G
coderbraytest
Scratcher
23 posts

Infinite Precision Calculator

This calculator looks cool!
-Rex-
Scratcher
500+ posts

Infinite Precision Calculator

Bug:
“(1/9)*9” “>0.999999999999999”
Maybe store multiplication and division operations as a fraction and then divide it at the end of the calculation, if that's possible.
Zro716
Scratcher
1000+ posts

Infinite Precision Calculator

-Rex- wrote:

Bug:
“(1/9)*9” “>0.999999999999999”
Maybe store multiplication and division operations as a fraction and then divide it at the end of the calculation, if that's possible.
I'm not sure that's really a bug. Any calculator you do this with will always evaluate exactly to that. It's unfortunately a problem with floating-points and how they are stored. 1/9 gets stored as 0.11111111111111…, so you can imagine a buffer with all 1's in it. When you multiply it all by 9, no integer value anywhere in the buffer exceeds 9, so there's no carry over to make it round up to exactly 1.
-Rex-
Scratcher
500+ posts

Infinite Precision Calculator

Zro716 wrote:

-Rex- wrote:

Bug:
“(1/9)*9” “>0.999999999999999”
Maybe store multiplication and division operations as a fraction and then divide it at the end of the calculation, if that's possible.
I'm not sure that's really a bug. Any calculator you do this with will always evaluate exactly to that. It's unfortunately a problem with floating-points and how they are stored. 1/9 gets stored as 0.11111111111111…, so you can imagine a buffer with all 1's in it. When you multiply it all by 9, no integer value anywhere in the buffer exceeds 9, so there's no carry over to make it round up to exactly 1.
Sorry if I'm acting clueless, but while doing division, you could detect if any section in the decimals repeats, and somehow display it with a line above it. If there is a repeating nine, change the digit to the left of it by 1. In addition, you could, like I said earlier, your calculator could store the value as a fraction until the end of the calculation, when it calculates the fraction, so it would divide 1/9 to get 1/9 (the fraction), then multiply it by nine to get the fraction 9/9, which would divide to 1, although I'm not sure if that would be possible.
lucas523053070
Scratcher
1 post

Infinite Precision Calculator

Bug: the calculator gives an output of -2 when I type in -10/-2

Powered by DjangoBB