How to perform basic arithmetic operations

The place to discuss anything and everything about running your LiveCode on Android

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
SheltonCruz
Posts: 1
Joined: Mon Feb 26, 2024 5:34 am
Contact:

How to perform basic arithmetic operations

Post by SheltonCruz » Mon Feb 26, 2024 5:41 am

I'm trying to create a simple calculator app with LiveCode, but I'm stuck on how to implement the basic arithmetic operations. I have created a stack with four buttons for addition, subtraction, multiplication, and division, and two fields for entering and displaying the numbers. I have also written some scripts for each button, but they don't seem to work properly. Here is an example of my script for the addition button:

on mouseUp
put field "number1" into x
put field "number2" into y
put x + y into field "result"
end mouseUp

When I click the button, nothing happens in the result field. I have checked the syntax and the variable names, but I can't find any errors. Can anyone please help me figure out what I'm doing wrong and how to fix it?

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: How to perform basic arithmetic operations

Post by richmond62 » Mon Feb 26, 2024 9:13 am

Dunno: works perfectly round these parts . . .
-
SShot 2024-02-26 at 10.12.03.png
-
Either there is something else wrong, or you are just 'trailing your coat' for a completely different reason.
Attachments
example.livecode.zip
(994 Bytes) Downloaded 19 times

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to perform basic arithmetic operations

Post by SparkOut » Mon Feb 26, 2024 10:19 am

Things to check (and have the script do some of the error checking for you):

Field names match the script references

Field contents are a number
(For example, not a blank line that's been pushed out of view at the top of the field because of a number sitting in view at the bottom)
Make the script check

Code: Select all

if x is a number and y is a number then
  put x + y into field "result"
else
  answer "x is" && x &&  "and y is" && "These are not both valid numbers"
end if

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to perform basic arithmetic operations

Post by stam » Mon Feb 26, 2024 12:22 pm

SparkOut wrote:
Mon Feb 26, 2024 10:19 am
Make the script check

Code: Select all

if x is a number and y is a number then
 
Alternatively, or perhaps complementing this, especially since this is an app designed as numerical calculator (presumably), the fields themselves should not allow any data entry that isn't a number. In the field's script, only allow keyDown if the the key pressed is a number or something like a comma or period:

Code: Select all

on keyDown pKeyName
    if pKeyName is in "0123456789,." then pass keyDown
end keyDown
The field then will only allow digits or "." or "," to be entered (edit as needed). With this, the text of the field can only ever contain numbers.
Of course, safest is to do both this and what Sparkout says, if you want the calculator logic to apply to inputs other than the field where you control the input...

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to perform basic arithmetic operations

Post by dunbarx » Mon Feb 26, 2024 3:21 pm

What everyone is saying is that there is something structural wrong with your stack, and probably not particularly with your code. For a new user, such traps live there, as opposed to your thinking.

Here is a simple stack that does what you say, at least with addition. Can you find what is different in your own stack?

Craig
simpleArithmetic.livecode.zip
(1013 Bytes) Downloaded 17 times
Last edited by dunbarx on Mon Feb 26, 2024 5:17 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to perform basic arithmetic operations

Post by dunbarx » Mon Feb 26, 2024 3:23 pm

I see Richmond has uploaded what looks like, apart from the backGround color, what I did.

Craig

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Thu Jun 29, 2006 4:16 pm

Re: How to perform basic arithmetic operations

Post by Randy Hengst » Mon Feb 26, 2024 4:49 pm

Given that I’ve made this mistake myself, double-check to make sure you have only one field each with the names “number1”, “number2” “result”

take care,
Randy

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to perform basic arithmetic operations

Post by dunbarx » Mon Feb 26, 2024 6:24 pm

hi.

Once we get this little problem straightened out, and with entry validation others have mentioned, how are you handling the display of the number of allowed decimal points? Might not this need to be adjustable by the user? Will you need special handling for very large numbers?

Is this getting less and less simple?

Craig

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to perform basic arithmetic operations

Post by SparkOut » Mon Feb 26, 2024 8:11 pm

stam wrote:
Mon Feb 26, 2024 12:22 pm
The field then will only allow digits or "." or "," to be entered (edit as needed). With this, the text of the field can only ever contain numbers.
Of course, safest is to do both this and what Sparkout says, if you want the calculator logic to apply to inputs other than the field where you control the input...
This is very much in my wheelhouse. I am big on data validation, but input validation/restrictions even more. If you need to input only numbers, then using a dial or slider can be even better than using a field even with limited inputs. Or using a radio button or checkbox group to choose from a limited set of choices.
But this is moving still further away from the OP's basic task.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to perform basic arithmetic operations

Post by dunbarx » Mon Feb 26, 2024 9:06 pm

Sparkout.
If you need to input only numbers, then using a dial or slider can be even better than using a field even with limited inputs
How can you input numbers for a calculator using a slider?? How would you enter "27.25 + 55.19" ?

Craig

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to perform basic arithmetic operations

Post by stam » Mon Feb 26, 2024 9:16 pm

Actually a slider by default will produce float/decimal numbers
Still a pain tho, and not something I would recommend unless you want unhappy users!

Since this is a calculator it makes sense to have buttons that will also click when you type the number (and decimal point), not sure I’ve ever seen a calculator with sliders!
But I guess there’s always a first time lol

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to perform basic arithmetic operations

Post by dunbarx » Mon Feb 26, 2024 10:06 pm

Stam, Sparkout.

I love and use sliders.

But not when three, four or seven significant figures AND with a decimal point located somewhere in the mix, must be selected with that sort of gadget. A keypad is really nice...

Craig

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to perform basic arithmetic operations

Post by SparkOut » Mon Feb 26, 2024 10:16 pm

I should have forced myself to carry on phonetyping and say "but it depends on the user interface" and "try to make the UX intuitive and simple/sensible".
Slider values can be rounded or constrained quite simply.
For a calculator UI, uh... use buttons with a big numeric (or delete, function,etc) icon that populates a field that the user doesn't get to edit directly. (Your keypad)
But the point is "try to prevent erroneous input by making sensible UI choices" (and validate what does become the input data)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to perform basic arithmetic operations

Post by dunbarx » Tue Feb 27, 2024 3:04 pm

Sparkout.

And I should have realized that your idea did not require that, say, if one wanted to add 35.7 to 14.22, the slider would NOT have to find those exact values in its travels, but rather select individual digits in succession. My point was not well taken.

But even done the "right" way, though doable, is MUCH more tedious than simply typing, as you point out.

And done that right way, how would the user select the current slider value? Hit the enter key?

Craig

Post Reply

Return to “Android Deployment”