Page 1 of 1

More maths stuff.

Posted: Thu Jun 12, 2014 1:28 am
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.

Re: More maths stuff.

Posted: Thu Jan 08, 2015 7:42 pm
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.

Re: More maths stuff.

Posted: Thu Jan 08, 2015 10:11 pm
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.

Re: More maths stuff.

Posted: Sat Jan 10, 2015 4:17 pm
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% ? =)