Jump to content

Howdy!

 

I've a little side project I want to do with multiple math calculations. However, I'm not sure the best way to go about it or what programming language would be best?

 

I'm trying to achieve the following:
____÷____=____÷____=____+4=____
____÷3=____x2=____÷3=3.33=+4=____

 

I want a simple form where I can input numbers and it splits out the answer to avoid having to do maths every 15-23 minutes.

 

It's likely best in a app in C/C++ or something. However, due to iPhone I think it'd need to be a website or something that I can pull up in Safari web browser. Since app development is crazy expensive

 

This data doesn't need to be stored just spitted out then cleared on form reset/page refresh as it's one time use only.

 

Any ideas?

 

I don't expect to be "spoon-fed" the code but if someone can suggest which language would be best I'm totally fine with researching and looking up.

 

Thanks.

 

Link to comment
https://linustechtips.com/topic/1503633-doing-multiple-math-calculations/
Share on other sites

Link to post
Share on other sites

8 minutes ago, manikyath said:

why not just use the windows calculator though?

 

i'd suggest python and PySimpleGUI if you want to keep it easy, and basicly you'd end up building a calculator widget...

 

I could! Although using iPhone I could use the built in iOS calculator. However, to avoid having to do all the calculations I could just type the first number and the rest is da-da! done and ready. I don't need the answers in-between just the final number really.

 

So instead of doing all this on the calculator:

 

____÷____=____÷3.33=____+4=____

 

750÷3=250x2=500÷4÷3.33+4=42 -  I could just type "750" into the form and it throws out "42" - Its actually 41.53 but we round up. http://30dns.net/demo

Link to post
Share on other sites

5 minutes ago, RayJohnson48 said:

 

I could! Although using iPhone I could use the built in iOS calculator. However, to avoid having to do all the calculations I could just type the first number and the rest is da-da! done and ready. I don't need the answers in-between just the final number really.

 

So instead of doing all this on the calculator:

 

____÷____=____÷3.33=____+4=____

 

750÷3=250x2=500÷4÷3.33+4=42 -  I could just type "750" into the form and it throws out "42" - Its actually 41.53 but we round up. http://30dns.net/demo

just use python, either with a pysimplegui, or just use a terminal input.

Link to post
Share on other sites

27 minutes ago, RayJohnson48 said:

However, due to iPhone I think it'd need to be a website or something that I can pull up in Safari web browser.

In that case you're pretty much set on JS/TS already.

 

Of course you could probably just open WolframAlpha or some other site that can already do that.

1 minute ago, RayJohnson48 said:

____÷____=____÷3.33=____+4=____

 

750÷3=250x2=500÷4÷3.33+4=42 -  I could just type "750" into the form and it throws out "42" - Its actually 41.53 but we round up. http://30dns.net/demo

So you want to be able to enter some type of formula, then just replace the input? Your example isn't quite clear, since you have more than one placeholder. So do you need to enter more than one number or are all numbers except "x" fixed?

 

The typical way to write a formula like this would be

f(x) = x ÷ 3 x 2 ÷ 4 ÷ 3.33 + 4

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

  

16 minutes ago, Eigenvektor said:

IOf course you could probably just open WolframAlpha or some other site that can already do that.

 

Unsure what WolframAlpha is but I will Google!

 

17 minutes ago, Eigenvektor said:

So you want to be able to enter some type of formula, then just replace the input? Your example isn't quite clear, since you have more than one placeholder. So do you need to enter more than one number or are all numbers except "x" fixed?

 

The typical way to write a formula like this would be

f(x) = x ÷ 3 x 2 ÷ 4 ÷ 3.33 + 4

For that one, yes. However the "÷4" would depend on if it's 2 row selected if it's a 1 row it'd be ÷8.

 

I was able to do this in some PHP website I found which would be be fine without the fancy HTML form and just edit the $a, $b, $c and $d instead.

 

However, I'm not sure..

1) How to get the answer from the previous into the next - like instead of 6060 it uses the answer to the line above or me entering 3030 it gets it from the above

2) How to round up the answer it throws?

 

image.thumb.png.a190cede5678b8159ef583d9a06b76d1.png

Link to post
Share on other sites

5 hours ago, Eigenvektor said:

In that case you're pretty much set on JS/TS already.

Not entirely, if they really wanted to, they could write a backend in anything with the equation and every request would just be an http request which has the input as a param, and it would just serve the answer back as plain text.

 

4 hours ago, RayJohnson48 said:

1) How to get the answer from the previous into the next - like instead of 6060 it uses the answer to the line above or me entering 3030 it gets it from the above

id convert all that into js, but regardless give previous values an associating variable etc if you want to reuse it, or if you want to do it in the html give the element an id.

4 hours ago, RayJohnson48 said:

2) How to round up the answer it throws?

Most languages have a somewhat math library which has the ceiling function (which rounds up). I do not know php but here is the respective manual link:
https://www.php.net/manual/en/function.ceil.php

Link to post
Share on other sites

5 hours ago, TizzyT566 said:

Not entirely, if they really wanted to, they could write a backend in anything with the equation and every request would just be an http request which has the input as a param, and it would just serve the answer back as plain text.

That's why I said "pretty much" and not "completely" set on JS 😉

 

I did think about suggesting it. But for convenience you'd likely still want to use JS in the frontend to make the request on value change, and in that case you can just implement the logic in JS directly. There's nothing that warrants a backend for this use case, it just adds unnecessary complexity.

 

10 hours ago, RayJohnson48 said:

For that one, yes. However the "÷4" would depend on if it's 2 row selected if it's a 1 row it'd be ÷8.

So on the one hand you want to be able to enter a single number, on the other hand you seem to have additional rules that change the formula. In any case, that doesn't change that writing a formula this way is more universally understood than _ + _ = _ x _ = _ / _.

 

Here's an example of how you could quickly hack something like that together in JavaScript:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Maths</title>
        <meta charset="UTF-8">

        <script>
            window.onload = function () {
                document.getElementById("input").onkeyup = function() {
                    const formula1_v1 = this.valueAsNumber / 3;
                    const formula1_v2 = formula1_v1 * 2;
                    const formula1_v3 = formula1_v2 / 4;
                    const formula1_v4 = formula1_v3 / 3.33;
                    const formula1_v5 = formula1_v4 + 4;
                    const formula1_output = Math.ceil(formula1_v5);

                    const formula2_v1 = this.valueAsNumber / 3;
                    const formula2_v2 = formula2_v1 * 2;
                    const formula2_v3 = formula2_v2 / 8;
                    const formula2_v4 = formula2_v3 / 3.33;
                    const formula2_v5 = formula2_v4 + 4;
                    const formula2_output = Math.ceil(formula2_v5);

                    document.getElementById("output1").innerHTML = formula1_output;
                    document.getElementById("output2").innerHTML = formula2_output;
                };
            }
        </script>
    </head>
    <body>
        <input
            id="input"
            type="number"
            placeholder="Enter a number" />

        <p id="output1">
            Nothing here, yet
        </p>

        <p id="output2">
            Nothing here, yet
        </p>
    </body>
</html>

 

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

Interesting idea, this is actually pretty great as a beginner project!

Breaking it down it looks like there are 3 parts to what you're trying to do:

 

1. Turning text into tokens

This is turning the text of your equation - something like " ____÷____=____÷____=____+4=____" into a sequence of tokens that you can more easily use to do the calculation. One way to do it might to be to create a "token" for each operation, for example "____+4" could become ["plus", 4], where each token is a list that contains the type of operation and the number that's already been filled in. Good google keywords for this are "tokenizer" and "lexer" - although a word of warning, this is a super deep rabbit hole that leads into the details of programming language theory so don't hesitate to abandon searches of these if it looks too complicated, your problem is solvable with very simple techniques.

 

2. Taking tokens and an input number and making an output value

Won't say much here, you can probably get this on your own.

 

3. Connecting everything to a UI

This one you have to do in javascript, since you can't directly read from textboxes and other UI elements from php. I would suggest doing everything in js over php, since php is going to have the limitation that all of your php code runs once, when the page loads. That means if you want anything to happen when you input everything that's either going to require a page reload (not great as far as user experience goes...), or you'll have to do it in js. If you're not familiar with javascript, I reccomend skimming MDN's beginner guide and maybe poking through their other beginner resources to see if they have what you need. (MDN is also the unofficial reference documentation for nearly everything you can do in a browser, it's a site to have in your bookmarks. Most google searches about this stuff will give you better results if you add "mdn" to the end 🙂 )

Link to post
Share on other sites

On 4/29/2023 at 12:07 AM, TizzyT566 said:

Not entirely, if they really wanted to, they could write a backend in anything with the equation and every request would just be an http request which has the input as a param, and it would just serve the answer back as plain text.

 

id convert all that into js, but regardless give previous values an associating variable etc if you want to reuse it, or if you want to do it in the html give the element an id.

Most languages have a somewhat math library which has the ceiling function (which rounds up). I do not know php but here is the respective manual link:
https://www.php.net/manual/en/function.ceil.php

 

Thanks for the response! I've been researching and have seen JS is likely the best option here.

 

I'm a total noob and never programmed or coded anything lol. Did try Fiverr after seeing an LTT/promo code ad but every response back wanted $1100+ and can't afford that lol especially as it's for work and they won't reimburse me..

 

I rather go manual slow route with a £5 calculator and note pad (what we do currently but trying to speed it up and save paper). I'm taking over someone who's done a role for 36 years and he knows it like speed dial and me and numbers don't mix (lol) hate anything to do with mathematics. (hence why I'm trying to cheat here with a tool/calculator, ha!)

 

On 4/29/2023 at 6:17 AM, Eigenvektor said:

That's why I said "pretty much" and not "completely" set on JS 😉

 

I did think about suggesting it. But for convenience you'd likely still want to use JS in the frontend to make the request on value change, and in that case you can just implement the logic in JS directly. There's nothing that warrants a backend for this use case, it just adds unnecessary complexity.

 

So on the one hand you want to be able to enter a single number, on the other hand you seem to have additional rules that change the formula. In any case, that doesn't change that writing a formula this way is more universally understood than _ + _ = _ x _ = _ / _.

 

Here's an example of how you could quickly hack something like that together in JavaScript:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Maths</title>
        <meta charset="UTF-8">

        <script>
            window.onload = function () {
                document.getElementById("input").onkeyup = function() {
                    const formula1_v1 = this.valueAsNumber / 3;
                    const formula1_v2 = formula1_v1 * 2;
                    const formula1_v3 = formula1_v2 / 4;
                    const formula1_v4 = formula1_v3 / 3.33;
                    const formula1_v5 = formula1_v4 + 4;
                    const formula1_output = Math.ceil(formula1_v5);

                    const formula2_v1 = this.valueAsNumber / 3;
                    const formula2_v2 = formula2_v1 * 2;
                    const formula2_v3 = formula2_v2 / 8;
                    const formula2_v4 = formula2_v3 / 3.33;
                    const formula2_v5 = formula2_v4 + 4;
                    const formula2_output = Math.ceil(formula2_v5);

                    document.getElementById("output1").innerHTML = formula1_output;
                    document.getElementById("output2").innerHTML = formula2_output;
                };
            }
        </script>
    </head>
    <body>
        <input
            id="input"
            type="number"
            placeholder="Enter a number" />

        <p id="output1">
            Nothing here, yet
        </p>

        <p id="output2">
            Nothing here, yet
        </p>
    </body>
</html>

 

 

Cheers! Will remember that when writing up any formula in the future. I will give this a whirl tomorrow after work it's getting late here.

 

On 4/29/2023 at 11:21 AM, aiwiguna said:

In my opinion you can use excel or any similar app to do that, it easy and customizable enough for do some formula

I did consider that with MS Excel or Google's Sheets but couldn't find much on doing the format I wanted did give it a play but then threw off the idea, ha!

 

On 4/30/2023 at 12:05 PM, NocTheRocc said:

Interesting idea, this is actually pretty great as a beginner project!

Breaking it down it looks like there are 3 parts to what you're trying to do:

 

1. Turning text into tokens

This is turning the text of your equation - something like " ____÷____=____÷____=____+4=____" into a sequence of tokens that you can more easily use to do the calculation. One way to do it might to be to create a "token" for each operation, for example "____+4" could become ["plus", 4], where each token is a list that contains the type of operation and the number that's already been filled in. Good google keywords for this are "tokenizer" and "lexer" - although a word of warning, this is a super deep rabbit hole that leads into the details of programming language theory so don't hesitate to abandon searches of these if it looks too complicated, your problem is solvable with very simple techniques.

 

2. Taking tokens and an input number and making an output value

Won't say much here, you can probably get this on your own.

 

3. Connecting everything to a UI

This one you have to do in javascript, since you can't directly read from textboxes and other UI elements from php. I would suggest doing everything in js over php, since php is going to have the limitation that all of your php code runs once, when the page loads. That means if you want anything to happen when you input everything that's either going to require a page reload (not great as far as user experience goes...), or you'll have to do it in js. If you're not familiar with javascript, I reccomend skimming MDN's beginner guide and maybe poking through their other beginner resources to see if they have what you need. (MDN is also the unofficial reference documentation for nearly everything you can do in a browser, it's a site to have in your bookmarks. Most google searches about this stuff will give you better results if you add "mdn" to the end 🙂 )


I did bookmark MDN's page previously and have used it for some references. Will look up those keywords you mentioned tomorrow and see what I can find after work :)

 

-

 

I'm no programmer (hence the terrible code) but this is kinda the "end goal" http://30dns.net/demo what I'm trying to achieve.

Link to post
Share on other sites

7 hours ago, RayJohnson48 said:

Cheers! Will remember that when writing up any formula in the future. I will give this a whirl tomorrow after work it's getting late here.

Just copy & paste the code above into a file (e.g. index.html) and open it in a browser. It has an input field that takes a number and spits out two numbers based on

f(x) = x ÷ 3 x 2 ÷ 4 ÷ 3.33 + 4
f(x) = x ÷ 3 x 2 ÷ 8 ÷ 3.33 + 4

You should be able to use that as a starting point. Adjust the numbers and steps as you need. Adding additional calculations should be fairly straightforward.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

7 hours ago, RayJohnson48 said:

I'm a total noob and never programmed or coded anything lol. Did try Fiverr after seeing an LTT/promo code ad but every response back wanted $1100+ and can't afford that lol especially as it's for work and they won't reimburse me..

 

I rather go manual slow route with a £5 calculator and note pad (what we do currently but trying to speed it up and save paper). I'm taking over someone who's done a role for 36 years and he knows it like speed dial and me and numbers don't mix (lol) hate anything to do with mathematics. (hence why I'm trying to cheat here with a tool/calculator, ha!)

So I'm curious as it's for work...is there a specific purpose of the formula or are you trying to implement a more broad kind of formula that is easily tweakable?  I do agree with Eigen, JS sort of solution is the more general approach (but then you do need to host it somewhere or remember to open it in a web-browser...and depending on your workplace rules JS might be disabled for local html files).

 

Specifically I'm wondering how many different numbers might be used in your calculation (or if the formula is changing daily etc).

 

7 hours ago, RayJohnson48 said:

I did consider that with MS Excel or Google's Sheets but couldn't find much on doing the format I wanted did give it a play but then threw off the idea, ha!

Given it is a place of work, Excel might be a really good option for this...honestly it's overlooked a lot what you can do in Excel.

 

For simple formulas as well it's pretty easy to do:

A1, B1, C1 cells are just labels saying the inputs

A2, B2, C2 are the ones where you put in the values

 

D2 is where everything is calculated

So for your first formula ____÷____=____÷____=____+4=____
it becomes like this

D2 is "=((A2 / B2) / C2) + 4"
It's essentially equivalent to A2÷B2=res÷C2=res+4=Final result

 

The bit that is confusing me though is that you mention giving a single number, but the examples you giving has more than 1 variable.  Like in your first formula I assumed you needed 3 variables to put in.

 

Anyways, the above one liner for D2 should give you approximately a good insight into how to do it.  (Remembering BE DM AS  as a rule for order of operations though)

 

Actually, honestly excel I think is your best bet as in case you need to hand it over to someone else more people are comfortable with Excel than doing their own webpage stuff.

 

3735928559 - Beware of the dead beef

Link to post
Share on other sites

14 hours ago, Eigenvektor said:

Just copy & paste the code above into a file (e.g. index.html) and open it in a browser. It has an input field that takes a number and spits out two numbers based on

f(x) = x ÷ 3 x 2 ÷ 4 ÷ 3.33 + 4
f(x) = x ÷ 3 x 2 ÷ 8 ÷ 3.33 + 4

You should be able to use that as a starting point. Adjust the numbers and steps as you need. Adding additional calculations should be fairly straightforward.

 

Perfect! I've been playing about with the code and so far so good. I've changed it to add another input to modify the / 4 (R) part as this can change - it can be R ÷4 or R ÷8 as available here. When working out, here's the 200/Mtr how it looks.

 

Going to give it a whirl and modify a second html page for the 300 and 400 method. All learn from somewhere :-)

 

 

14 hours ago, wanderingfool2 said:

So I'm curious as it's for work...is there a specific purpose of the formula or are you trying to implement a more broad kind of formula that is easily tweakable?  I do agree with Eigen, JS sort of solution is the more general approach (but then you do need to host it somewhere or remember to open it in a web-browser...and depending on your workplace rules JS might be disabled for local html files).

 

I will be hosting the file on my VPS with only my IP listed eventually - not that it matters as it's nothing private and only one time use and isn't stored.


No PC or tablet at work or anything so will just be pulling up the page on my iPhone via Safari (or Add to Home Screen the page as a shortcut).

 

14 hours ago, wanderingfool2 said:

Specifically I'm wondering how many different numbers might be used in your calculation (or if the formula is changing daily etc).

 

Given it is a place of work, Excel might be a really good option for this...honestly it's overlooked a lot what you can do in Excel.

 

The calculation will never change, it's been the same for 37 years. It's a Fin Press machine and it's just getting the figure to enter into the machine.

 

No job is ever the same size or "common" so will never be kept or stored why I'm not fussed about getting database to store it as the chance of something being the same is pretty slim. Here's the 200/Mtr how it looks.

 

Excel will definitely be played with more, just the iOS app isn't the greatest.

Link to post
Share on other sites

On 4/29/2023 at 6:17 AM, Eigenvektor said:

That's why I said "pretty much" and not "completely" set on JS 😉

 

I did think about suggesting it. But for convenience you'd likely still want to use JS in the frontend to make the request on value change, and in that case you can just implement the logic in JS directly. There's nothing that warrants a backend for this use case, it just adds unnecessary complexity.

 

So on the one hand you want to be able to enter a single number, on the other hand you seem to have additional rules that change the formula. In any case, that doesn't change that writing a formula this way is more universally understood than _ + _ = _ x _ = _ / _.

 

Here's an example of how you could quickly hack something like that together in JavaScript:

 

 

Hey! Okay, so I've played about with the sample you kindly offered and taken your time to write up, http://30dns.net/linustechtips. It seems all the figures are now coming out correctly! Matching the figures on my notepad using 1200 as an example.

 

My next task is to include a "split" when the formula1_output is too high. For example this figure here shows 247 x 5 runs however, when I enter this figure into the new Maths form it spits out "1216" which is higher than the machine can do (320).

 

400/Mtr maximum = 320
120/200/300/Mtr maximum = 250

 

Then once I've archived that = completed! I will then focus into styling it and making it into a two pages/forms like this (A) and this (B) to avoid 8 forms I got now.

Link to post
Share on other sites

10 hours ago, RayJohnson48 said:

which is higher than the machine can do (320).

There should be a Math.min function you can use for that, e.g.

const result = Math.min(formula_output, 320)
Quote

 

~edit: Swapped max for min. You want whichever value is smaller. So the value of formula_output if it is smaller than 320, or 320 otherwise.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

On 5/4/2023 at 1:58 PM, RayJohnson48 said:

Hey! Okay, so I've played about with the sample you kindly offered and taken your time to write up, http://30dns.net/linustechtips. It seems all the figures are now coming out correctly! Matching the figures on my notepad using 1200 as an example.

 

My next task is to include a "split" when the formula1_output is too high. For example this figure here shows 247 x 5 runs however, when I enter this figure into the new Maths form it spits out "1216" which is higher than the machine can do (320).

 

400/Mtr maximum = 320
120/200/300/Mtr maximum = 250

 

Then once I've archived that = completed! I will then focus into styling it and making it into a two pages/forms like this (A) and this (B) to avoid 8 forms I got now.

Just checking for clarity.  What is supposed to happen to the number when it's above that threshold?

 

Is it supposed to clamp it to the number like 320, or does it need to do something else (like split it into more runs)?  @Eigenvektor's solution would be for if you just clamp it to the highest value

3735928559 - Beware of the dead beef

Link to post
Share on other sites

2 hours ago, wanderingfool2 said:

Just checking for clarity.  What is supposed to happen to the number when it's above that threshold?

 

Is it supposed to clamp it to the number like 320, or does it need to do something else (like split it into more runs)?  @Eigenvektor's solution would be for if you just clamp it to the highest value

Hey! 

 

Thanks for the response! It's got to split it into multiple runs 🙂

 

Say for example I need 6060 meters of fin. However, the machine can only do a maximum of 320 per single run for 400/Mtr (÷2.5) and maximum 250 for 300/Mtr (÷3.33). This is the height limit of here (Imgur link).

 

So if I enter 6060 into sample8.html it needs to spit out "247 x5" as the answer instead of "1216" as shown in this example here.

 

Another example would be:
Needing 5520 meters of fin 5520÷2 = 2760 ÷ 2.5 + 4 = 1108 which is too high again as the machines limit is 320 (@400/2.5) so we'd split that into 4.

 

So that would become:
5520 ÷ 2 = 2760 ÷ 4 (runs) = 690 ÷ 2.5 + 4 = 280

So I'd run the machine @ 280 5 times. (run>unload>run>unload>run>unload>run>unload>run>unload) giving me the required 5520 meters.

 

Which would result into entering 5520 into sample8.html  giving me "280 x5" as the answer instead of "1108"

Link to post
Share on other sites

3 hours ago, RayJohnson48 said:

Which would result into entering 5520 into sample8.html  giving me "280 x5" as the answer instead of "1108"

Shouldn't that be 4 runs?

4 x 280 = 1120

 

1108 / 280 = 3.9, if you use ceil again you'll end up with 4. Or am I missing something?

 

In any case you should be able to solve that with an additional if-clause. Something along the line of

if (result > limit) {

  runs = Math.ceil(result / limit);
  result = limit;
} else {
  runs = 1;
}

 

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

  • 3 weeks later...
On 5/6/2023 at 10:09 PM, Eigenvektor said:

Shouldn't that be 4 runs?

4 x 280 = 1120

 

1108 / 280 = 3.9, if you use ceil again you'll end up with 4. Or am I missing something?

 

In any case you should be able to solve that with an additional if-clause. Something along the line of

if (result > limit) {

  runs = Math.ceil(result / limit);
  result = limit;
} else {
  runs = 1;
}

 

Unfortunately, I couldn't get the splitting to work and officially gave up, ha. 

 

I appreciate your time and help and helping me get this working for the single runs though!

 

It's been working wonders and saving roughly 2 hours 15 minutes per day, 5 days a week speeding up my work flow, even got in my bosses good books! lol.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×