Jump to content



Photo
- - - - -

CPLua - What is wrong with this while loop?

CPLua Classpad While

  • Please log in to reply
3 replies to this topic

#1 MrMagoo

MrMagoo

    Newbie

  • Members
  • Pip
  • 3 posts
  • Gender:Male

  • Calculators:
    Casio: fx-700p, fx-4200p, fx-7700GB, fx9750g, Algebra FX 2.0 Plus, Classpad 330, fx-9750gII, fx-3650p, fx-82ms, fx-911ex, fx-991 ES plus 2nd ed., fx-991 ms 2nd ed., fc-200v,
    HP: HP-11C, HP-20s, HP-35s, HP-9g, HP-48g, HP-48gx, HP-49g+, HP-50g, HP-39g+, HP-39gs, HP-39gII
    Texas: Ti-81, Ti-73, Ti-15, Ti-36X Pro, Ti-82 Stats, Ti-82 Stats.fr, Ti-83+, Ti-84+, Ti-89 Titanium, Voyage 200
    BrtC: BrtC-CG121, BrtC Fc-12

Posted 15 April 2021 - 10:08 PM

  Fellows,

 

  At first, sorry for my bad English - I'm Brazilian, so a Portuguese native speaker.

  I have a few calculators and sometimes I like to make some benchmark tests with them. Maybe the "Calculator Olympic Games", if you wish.

  I use to figure tests that everyone can play.

  But, this time, I tried something different and excluded the non-programable calculators.

  My intentions were to analyze the native programming language and also a similar algorithm to them, in order to compare their performance (maybe it is interesting to post the results and the codes in another post).

  Well, we all know that the Classpad does not have a native good programming environment when you have performance in mind (even more when you are dealing with numerical analys with a lot of loops). In this way, myself and all the other calculators agreed that we will allow CP330 to use CPLua in substitution of natural Casio Basic.

  The benchmark used is to perform Simpson Rule as presented in this link for the Ti-81 (https://www.ticalc.o...math/simpson.81) PS: on line 5, the correct code is a capital i instead of one. The corrected instruction is :(B - A) / N -> I

  The code was minor adjusted according the possible commands on each calculator.

 

  So, they all have to perform the following definite integral:

 

e^(x^3), from 0 to 6, with n=1000

 

  It was based on the test presented here: https://www.youtube....h?v=DHRsvSTGiBc

 

  The CPLua code used is:

 

--start

function y(x)

  return math.exp(x^3)

end

 

n = 1000

a = 0 + 0.

b = 6 + 0.

i = (b-a)/n

x = a + 0.

s = 0.0

while ( x < b ) do

  s = s+y(x)

  x = x+i

  s = 4*y(x)+s

  x = x+i

  s = s+y(x)

end

s = s*i/3

print(s)

 

  Ok. All the calculators performed well and provided the same answer (obviously, with their respective performance time).

  But CPLua in Classpad did not.

  CPLua ignores when x = 6, when it should stop. It iterates one more time, as if the instruction were ( x <= b ).

 

  I tried everything that I figured that it could be and did not find anything. I tried CPLua 0.8 and 0.10a.

 

So, I changed the code, reduced the d to 10 and requested an x print before the while end. Just to investigate, the code became:

 

n = 10

a = 0 + 0.

b = 6 + 0.

i = (b-a)/n

x = a + 0.

s = 0.0

while ( x < b ) do

  s = s+y(x)

  x = x+i

  s = 4*y(x)+s

  x = x+i

  s = s+y(x)

  print(x)

end

s = s*i/3

print(s)

 

The output is:

1.2

2.4

3.6

4.8

6

7.2

2.51522462964201e+161

Done.

 

The 7.2 there is totaly wrong. It is not suposed to be there.

If I change b=5, so it works. If I change b to 6.000001 it works

But if I change b=6.00001 it does not work!!!!

 

Does anybody has a clue about what is going on?

 

Thanks a lot!!!!


Edited by MrMagoo, 15 April 2021 - 10:29 PM.


#2 MrMagoo

MrMagoo

    Newbie

  • Members
  • Pip
  • 3 posts
  • Gender:Male

  • Calculators:
    Casio: fx-700p, fx-4200p, fx-7700GB, fx9750g, Algebra FX 2.0 Plus, Classpad 330, fx-9750gII, fx-3650p, fx-82ms, fx-911ex, fx-991 ES plus 2nd ed., fx-991 ms 2nd ed., fc-200v,
    HP: HP-11C, HP-20s, HP-35s, HP-9g, HP-48g, HP-48gx, HP-49g+, HP-50g, HP-39g+, HP-39gs, HP-39gII
    Texas: Ti-81, Ti-73, Ti-15, Ti-36X Pro, Ti-82 Stats, Ti-82 Stats.fr, Ti-83+, Ti-84+, Ti-89 Titanium, Voyage 200
    BrtC: BrtC-CG121, BrtC Fc-12

Posted 16 April 2021 - 09:29 AM

Going a little bit further.

 

I implemented a function to round the x and the b value in the while condition. The function is (found somewhere, but I forgot to get the link):

 

function round( num, numDecimalPlaces )

  local mult = 10^( numDecimalPlaces or 0 )

  return math.floor( num * mult + 0.5 ) / mult

end

 

And the while condition became:

 

while ( round( x ) < round ( b ) ) do

 

It solved the problem but increased 25% in processing time.

 

Does anybody has a clue how to solve it without this 'round' function?



#3 piu58

piu58

    Casio Freak

  • Members
  • PipPipPipPip
  • 133 posts
  • Gender:Male

  • Calculators:
    Casio fx-9860G, Casio fx-CG20

Posted 16 April 2021 - 04:32 PM

I suppose the problem arises from

i = (b-a)/n

This has one number after the decimal point. It may  have, however, a infinite quantity of numbers in binary form.

 

Most pocket computers work in decimal. I don't have a classpad, but may be it works in binary (as most of the programming languages). There may be rounding problems.



#4 MrMagoo

MrMagoo

    Newbie

  • Members
  • Pip
  • 3 posts
  • Gender:Male

  • Calculators:
    Casio: fx-700p, fx-4200p, fx-7700GB, fx9750g, Algebra FX 2.0 Plus, Classpad 330, fx-9750gII, fx-3650p, fx-82ms, fx-911ex, fx-991 ES plus 2nd ed., fx-991 ms 2nd ed., fc-200v,
    HP: HP-11C, HP-20s, HP-35s, HP-9g, HP-48g, HP-48gx, HP-49g+, HP-50g, HP-39g+, HP-39gs, HP-39gII
    Texas: Ti-81, Ti-73, Ti-15, Ti-36X Pro, Ti-82 Stats, Ti-82 Stats.fr, Ti-83+, Ti-84+, Ti-89 Titanium, Voyage 200
    BrtC: BrtC-CG121, BrtC Fc-12

Posted 17 April 2021 - 02:28 PM

Hi Piu 58 - thanks for the reply and sugestion - I also figured that it could be the problem and that is the reason that I inserted the round() function.

 

Going further, you are right - when a = 0, b = 6, n = 10, the value of i became i = 0.6

 

0.6 to base 2 is 0.610 = 0.10011001100110011001100110011...2  (it is recurring, as a repeating decimal, or in this way, a repeating binary? sorry for the bad english)

 

When we truncate the binary to the digits presented above and get back to base 10, we get:

 

0.100110011001100110011001100112 = 0.5999999996274709701510

 

 

For sure, anything that tries to represent 0.6 in binary and is truncated, will be lower than 0.6.

 

If we accumulate x with this i lower than 0.6 for 10 times, x will be something lower than 6.

 

buuuuuuut, when I asked to print (x), the CPLua presents x = 6 (in this way 10 times 0.6 - see the output presented in the first post) and, in this way, it is representing x as 6 in base 10. I am assuming that when I asked for 6, CP330 and CPLua recognized x as 6, and give back to me a '6', so it printed x=6. I was expecting the same behavior in the while condition.

 

The algorithm in CP330 Casio Basic performs well, and stops when x=6. Just in CPLua.

 

So why did CPLua do not stop when x was 6? Does anybody knows why it is happening with CPLua?

Also, any suggestions how to solve it without the round() function in CPLua?







Also tagged with one or more of these keywords: CPLua, Classpad, While

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users