More maths stuff.

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

More maths stuff.

Post by Shaun_B »

Another quick example:

Code: Select all

10 dim pc(99): rem will be used to calculate percentages later
20 let pc = 100: rem used to fill array
30 for i = 0 to 99: rem fill array loop
40 let pc(i) = i/pc: rem enter multiplier for each array position
50 next i: rem end loop
60 input "enter the number"; a: rem a is for the number and b for the percentage
70 input "now enter the percentage of that number you want to work out";b
80 print
90 print "{left}"; b ;"{left}% of"; a ;"is"; a*pc(b)
Filling the array may take a little while, and using this method to work out percentages at least gets rid of the potential for a division by zero error (ie, human input error usually), as 0% of single number will be 0. The array is essentially being used like a look-up table. Also, it doesn't calculate above 99%, nor will it do 22.25% for instance.

True story: The dev next to me was using division to work out percentages on a recent project. Apart from the rounding difference across PHP and Javascript that we were using, division wasn't always going to work. For instance, 99 / 33 is 3, and yet 33% of 99 is 32.67. You probably all know this, but percentages have a certain symmetry to them in that 33% of 99 is the same as 99% of 33 :-) Mathematics is the reason that I want to improve my mathematics.

Regards,

Shaun.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
User avatar
e5frog
Member
Member
Posts: 84
Joined: Sun Apr 13, 2014 8:04 pm
Location: Sweden
Contact:

Re: More maths stuff.

Post by e5frog »

Is this a general BASIC tip or for C64's version? I don't think I have ever used "LET", it's a waste of space and time.

Code: Select all

60 input "enter the number"; a:rem a is for the number and b for the percentage
70 input "now enter the percentage of that number you want to work out";b
75 if b<0then100
80 print
90 print "{left}"; b ;"{left}% of"; a ;"is"; a*b/100:print:goto60
100 print"invalid percentage":print:goto60
A number 0 or 0 percentage will just result in 0, this can also calculate 0.1 or other decimal percentage.
I don't see the problem you have "solved"...

What does 99/33 have to do with 33% of 99?, 33% of 99 is 99*0.33 or 99*33/100, i you calculate 99/33 you'll get 3 because 99 is 300% of 33.
My other hobby: Image
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: More maths stuff.

Post by Shaun_B »

e5frog wrote: Is this a general BASIC tip or for C64's version? I don't think I have ever used "LET", it's a waste of space and time.
Without context, we are only data.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
dudz
Member
Member
Posts: 140
Joined: Tue Jun 17, 2014 5:40 am
Contact:

Re: More maths stuff.

Post by dudz »

I don't see the problem you have "solved"...
instead of giving an error on 0%, it now gives an error on 100% ? =)
Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 6 guests