Go Down

Topic: Every so often I am stunned by a post. (Read 4881 times) previous topic - next topic


Nick Gammon


For me, the most peculiar thing about C is that the missing condition in
Code: [Select]
for(;;) evaluates to true.


Yeah good point. Although if it didn't, it would be pretty useless. I suppose the logic is: Have a starting statement, or nothing. Have a condition, or no condition (in which case keep going). Have an end-of-loop statement, or nothing.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

GoForSmoke


"C code for I want to be fairly obtuse"
Code: [Select]
int indx = 0; 
while(! indx [outstr] == 0) {
  indx[outstr] = indx[instr];
  indx = indx + 1;
}



LOL!

So, what's your point?
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

retrolefty


lol its really both


It's not unlike a soldering iron, a useful tool if used properly, but it can definitly burn you if your not careful.

Lefty

cyberteque

How do this kind of recursion in C?

Code: [Select]
Procedure Tree; {(x, y: integer; dir: real; level: integer)}
Var   xnew, ynew: integer;
Begin
  If level > 0 Then
    Begin
      xnew := round(level * Scale * cos(dir)) + x;
      ynew := round(asp * level * Scale * sin(dir)) + y;
      PenSize(level, level);
      MoveTo(x, y);
      LineTo(xnew, ynew);
      tree(xnew, ynew, dir + inc + Randomize(2), level - 1);
      tree(xnew, ynew, dir - inc + Randomize(2), level - 1);
    End
End;{Tree}


This draws tree's!
Quite nice ones as well!
In 3D I have 1 on the X/Z plane and another on the Y/Z plane, other wise they disappear into a vertical line.

I use this kind of recursion with linked lists a lot.


AWOL

Recursion is simple, but it can be a RAM-hog, so beware.
Code: [Select]
unsigned long factorial (unsigned long n)
{
  return (n == 0) ? 1 : factorial (n - 1)  * n;
}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Jantje

Yust write in C like.
The only methods I see problem with are:
      PenSize(level, level);
      MoveTo(x, y);
      LineTo(xnew, ynew);
Because there is no pure c alternative. If you uses Visual studio there are but I wouldn't know any alternative for Arduino.
Bets regards
Jantje

How do this kind of recursion in C?

Code: [Select]
Procedure Tree; {(x, y: integer; dir: real; level: integer)}
Var   xnew, ynew: integer;
Begin
  If level > 0 Then
    Begin
      xnew := round(level * Scale * cos(dir)) + x;
      ynew := round(asp * level * Scale * sin(dir)) + y;
      PenSize(level, level);
      MoveTo(x, y);
      LineTo(xnew, ynew);
      tree(xnew, ynew, dir + inc + Randomize(2), level - 1);
      tree(xnew, ynew, dir - inc + Randomize(2), level - 1);
    End
End;{Tree}


This draws tree's!
Quite nice ones as well!
In 3D I have 1 on the X/Z plane and another on the Y/Z plane, other wise they disappear into a vertical line.

I use this kind of recursion with linked lists a lot.


Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

cyberteque

Quote
Recursion is simple, but it can be a RAM-hog, so beware.


Oh hell yeah!
Ages ago I wrote a "Galaxy" builder, around the galactic centre it randomly places stars, then puts planets around the stars, moons around the planets.
A friend of mine was messing around with the build dialogue.
I wasn't really paying much attention at first, till I noticed he was punching in really big numbers for star density, that got multiplied by (100 * random(100))!!

He says, "gee, this is getting slower!"

This was on a Mac IIci, with only 8M of RAM!

cyberteque

With nootropics TVout library

MoveTo(x, y);
LineTo(xnew, ynew);

turns into

TV.draw_line(x,y,x,y);
TV.draw_line(x,y,xnew,ynew);

GoForSmoke

When you call a function it saves the return address on the stack and any variables local to the function go on the stack so when a function calls itself over and over you better have more than 2k for everything!
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

cyberteque

Back when I was writing business software for Apple II, how much RAM you think we had left out of 48K?

I mainly use my Mega's for stuff like this.


GoForSmoke

I cut my teeth on less than that but it's in range for 1980 or so. The main thing is to constrain how far the code should need to go. For small machines I can suggest Forth-79, it's amazing how much you can do with it in tight space.

With some systems it seemed I could write small useful programs with an executable 100's of bytes long and then by the mid-80's on the preferred setups it was seeming like the easy minimum was 8K without taking extra steps to cut the code size down. The more memory we got, the much more got used for extras that count less.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Jantje

I started off on a commodore 64K.
That was plenty of memory in those days.
Can you understand how puzzled I am having 8 GIG of memory now and I still feel like more :smiley-eek:
The recursive code uses 2 integers that makes up 8 bytes plus 2 bytes for the return address makes 10 in total. In 1 K of memory that is still a recursion of 102 deep.
Best regards
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

cyberteque

I know what you mean!

Some of my code I've run under linux now I have like 10 times the RAM my Mac+ had...

So tell me, how does

Procedure (value:integer);

differ in C from

Procedure Calc(var:value:integer);

In the first case, "value" gets pushed onto the stack, in the second, the address for "value" gets pushed onto the stack and you don't have to worry about making "Calc" a function?
ie

result := Function Calc(value);

The other thing that I'm still coming to grips with is records!

How do I do rcords here in Arduino?

AWOL

Quote
How do I do rcords here in Arduino?

Do you mean "struct"s ?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up