Jump to content



Photo
- - - - -

Shortening Programs


  • Please log in to reply
16 replies to this topic

#1 somebody1234

somebody1234

    Casio Addict

  • Members
  • PipPipPip
  • 51 posts

  • Calculators:
    Casio fxcg10
    Casio fx-9860GII
    Casio Classpad 400

Posted 03 February 2014 - 09:27 AM

Does anyone have any tips on how to make programs shorter?

#2 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 04 February 2014 - 02:59 AM

Are you talking about BASIC programs?

#3 somebody1234

somebody1234

    Casio Addict

  • Members
  • PipPipPip
  • 51 posts

  • Calculators:
    Casio fxcg10
    Casio fx-9860GII
    Casio Classpad 400

Posted 05 February 2014 - 07:23 AM

yes.

#4 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 05 February 2014 - 05:38 PM

One thing is that most statements don't need an ending parentheses, for instance: RandInt(1,7) and RandInt(1,7 do the same thing (the second one will not throw an error).

#5 somebody1234

somebody1234

    Casio Addict

  • Members
  • PipPipPip
  • 51 posts

  • Calculators:
    Casio fxcg10
    Casio fx-9860GII
    Casio Classpad 400

Posted 08 February 2014 - 04:31 AM

Anything else?

#6 Casimo

Casimo

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 641 posts
  • Gender:Not Telling

Posted 08 February 2014 - 10:10 AM

Show some code, then it's easier to help.
This works, too: {1,2,3->List 1

#7 somebody1234

somebody1234

    Casio Addict

  • Members
  • PipPipPip
  • 51 posts

  • Calculators:
    Casio fxcg10
    Casio fx-9860GII
    Casio Classpad 400

Posted 08 February 2014 - 11:08 PM

Basically anything before an arrow/the end of a line?

#8 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 09 February 2014 - 03:49 AM

Yeah, you have to experiment, I am not sure that all of the commands are that way.

#9 TeamFX

TeamFX

    Casio Freak

  • Members
  • PipPipPipPip
  • 131 posts
  • Gender:Male

Posted 09 February 2014 - 05:10 PM

Omitting parentheses is a bad habit. It reduces readability and may produce bugs that cannot be easily spotted. The largest amount of code reduction is normally achieved by just optimizing your algorithms. However, this requires that you know quite a few Casio Basic tricks and know a lot of built-in commands that might be useful and could save a lot of code (and normally, these are also a lot faster).

Here is a good Casio Basic OS 2.0 topic that I just found: http://community.cas...icks-from-cfxm/

#10 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 09 February 2014 - 11:12 PM

Omitting parentheses is a bad habit.


The TI community does it often enough...

#11 somebody1234

somebody1234

    Casio Addict

  • Members
  • PipPipPip
  • 51 posts

  • Calculators:
    Casio fxcg10
    Casio fx-9860GII
    Casio Classpad 400

Posted 10 February 2014 - 09:32 AM

Does anyone know/can anyone find any of these tips and tricks?

#12 TeamFX

TeamFX

    Casio Freak

  • Members
  • PipPipPipPip
  • 131 posts
  • Gender:Male

Posted 10 February 2014 - 01:08 PM

The TI community does it often enough...

I actually meant that omitting the closing parenthesis is a bad habit.

Does anyone know/can anyone find any of these tips and tricks?

Most of these websites are long gone but Caspro's site is still online: http://www.spiderpixel.co.uk/caspro/

The getkey conversion stuff is one of my favorites:

C2) If you want to convert Getkey codes for keys 1-9 into the actual
number that was pressed then the long way to do it is:
Getkey->A
A=72=>1->G:A=62=>2->G:A=52=>3->G
A=73=>4->G:A=63=>5->G:A=53=>6->G
A=74=>7->G:A=64=>8->G:A=54=>9->G

That takes 84 steps. The short way to do it is:
.1Getkey
2-Ans+31Frac Ans->G

which only takes 15 steps. Or to also detect the number 0 then use:
.1Getkey
2-Ans+31Frac Ans+2(Ans=7.1->G

Also in reverse to convert numbers 1-9 into Getkey codes use:
?->N
(N-1)/3 ;don't use reciprocal or get rounding errors
72+Ans-31Frac Ans->G

If you want to take into account the number 0 as well then use:
?->N
(N-1)/3 ;don't use reciprocal or get rounding errors
72+Ans-31Frac Ans-11(N=0->G

C3) To convert Getkey codes for letters A~Z into numbers 1~26 use:
.1Getkey
6-10Frac Ans->R
8-Int Ans+5R+R(R<3->G



#13 Krtyski

Krtyski

    Casio Freak

  • Members
  • PipPipPipPip
  • 132 posts
  • Gender:Male
  • Location:Tokyo, Japan
  • Interests:programming, smooth Jazz and 4-wheel driving.

  • Calculators:
    FX-502P, FX-602P, FX-603P,
    fx-4000P, fx-7000G,
    fx-4500P, fx-4800P
    fx-5800P,
    CFX-9850G,
    CFX-9850GC PLUS
    fx-9860G,
    fx-9860G AU,
    fx-9860G Slim
    fx-9860GII SD,
    fx-9860GII-2,
    fx-9860GII-2 SD,
    fx-CG20, fx-CG50,
    fx-CP400
    fx-9860GIII
    fx-9750GIII
    fx-7400GIII

Posted 11 February 2014 - 12:55 PM

How's about this?


ex1)

If C
Then Locate 1,1,"True"
IfEnd


Instead of above;

C=>Locate 1,1,"True"




ex2)

For example, given integer N, you want to map to cyclic numnber A, such like 1->2->3->1->2->......., you don't need any If statement, just write;

1+N-3Int(N/3)->A

simple isn't it?
Any integer can be classified in its residue system.
This is useful for example when you want to select menu item using arrow keys, cursor comes to very bottom of menu items, then it goes to top of the menu.



ex3)

If you want to get how many digits of given integer N, you do not need to devide by 10^n and do mamy calculation, just write;

1+Int(log(N))

This can be apllied to following case;

Locate 1,1,N
Locate 2+Int(log(N)),1,"digits"

When N is 123, you will get
123 digits
on the display.

When N is 98765, you will get
98765 digits
on the display.

The string "digits" comes at proper column with one space.

Edited by Krtyski, 12 February 2014 - 12:27 AM.


#14 somebody1234

somebody1234

    Casio Addict

  • Members
  • PipPipPip
  • 51 posts

  • Calculators:
    Casio fxcg10
    Casio fx-9860GII
    Casio Classpad 400

Posted 15 February 2014 - 02:32 AM

I know that.
You can also use the StrLen( command which is easier for me.
I use Frac(N/X)*X for cyclic numbers where N is the number and X is the base.

#15 3298

3298

    Casio Addict

  • Members
  • PipPipPip
  • 79 posts
  • Gender:Male
  • Location:Germany

  • Calculators:
    fx-9750G Plus
    Algebra FX 2.0 (ROM 1.03,broken)
    HP 50G

Posted 15 February 2014 - 10:10 AM

Be careful, Frac tends to create rounding errors. Int doesn't.

#16 Krtyski

Krtyski

    Casio Freak

  • Members
  • PipPipPipPip
  • 132 posts
  • Gender:Male
  • Location:Tokyo, Japan
  • Interests:programming, smooth Jazz and 4-wheel driving.

  • Calculators:
    FX-502P, FX-602P, FX-603P,
    fx-4000P, fx-7000G,
    fx-4500P, fx-4800P
    fx-5800P,
    CFX-9850G,
    CFX-9850GC PLUS
    fx-9860G,
    fx-9860G AU,
    fx-9860G Slim
    fx-9860GII SD,
    fx-9860GII-2,
    fx-9860GII-2 SD,
    fx-CG20, fx-CG50,
    fx-CP400
    fx-9860GIII
    fx-9750GIII
    fx-7400GIII

Posted 15 February 2014 - 12:31 PM

somebody1234:

As you said Frac(N/X)*X looks better because of fewer steps.


3298:

Thanks, I should take it into account.When internal precision is 15 digits, Frac(1.00000000000001) gives you 0 not 1x10^-14.Is this the case?

Edited by Krtyski, 15 February 2014 - 12:50 PM.


#17 3298

3298

    Casio Addict

  • Members
  • PipPipPip
  • 79 posts
  • Gender:Male
  • Location:Germany

  • Calculators:
    fx-9750G Plus
    Algebra FX 2.0 (ROM 1.03,broken)
    HP 50G

Posted 16 February 2014 - 11:22 AM

Yes. And here is an example where X*Frac (N/X) fails: X=1234567, N = 1234568. This should obviously be 1, but it's 0.9999999999839453. N-X*Int (N/X) correctly results in 1.
But going back to the original topic: You can sometimes save a few bytes by omitting multiplication symbols when the symbol after it is not a digit. (There may be other cases where it errors, but I only know about the case where you try A*2; in that case, just reverse it so it becomes 2*A, which can be optimized into 2A.) The manual says something about this implicit multiplication having higher priority than normal multiplication, so this may lead to different results. (If A is 1.75, then Int 2A is 3 because the implicit multiplication has higher priority than Int, but Int 2*A is 3.5 because the normal multiplication has lower priority.) This may lead to more or less necessary parentheses, depending on the formula, but I believe in most cases it's possible to get away with less parentheses, just reorder the formula a bit. (Let's pretend the 2 in the Int 2*A from above was a variable instead which could have a fractional part, and we want the 3.5 without the multiplication symbol, don't write (Int 2)A, but write AInt 2. Though if the 2 shall stay a literal number, just optimize the Int away, since 2 is already an integer number.)
Another similar thing: The fraction sign entered via a b/c can replace the division symbol not only for numbers, but also for variables, and with its different priority it could also help you avoid parentheses. Try this: N-XInt N_X (where _ is the fraction symbol, of course.) With the division symbol you would need parentheses around N/X to make it work correctly, but the fraction symbol works without them. Even if you omit the ")" at the end, you save a byte for the "(".




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users